Category: JavaScript

0

Stubbing a class without invoking a constructor

You can cre­ate Stub instances using sinon.createStubInstance. const sinon = require(‘sinon’); const Foo = require(‘../Foo.js’); const fooS­tub = sinon.createStubInstance(Foo); fooStub.foo { bar() { returns ‘bar’; } } expect(fooStub.foo.bar()).to.equal(‘bar’);

0

How to read a file as a string in nodejs javascript?

Below code reads the file con­tent in asyn­chro­nous man­ner and the con­tent is returned in the string for­mat and buffered for­mat respec­tively. let fs = require(“fs-extra”); let path = require(“path”); fs.readFile(path.join(__dirname, “filePath”), “utf8”,(data, err)…

0

How to run cli/unix commands from nodejs scripts?

There are two ways to do this: child_process.exec(command[, options][, call­back]) // ls.js const { exec } = require(‘child_process’); exec(‘ls –lh /usr’, (error, std­out, stderr) => { if (error) { console.error(‘exec error: ${error}‘); return; } console.log(‘stdout:…

0

Call and retry last Allocation failed

Solu­tion: node –max–old-space-size=4096 <file­name> You need to tell node to over­write default heap size in v8. To see all the avail­able v8 flags run the fol­low­ing com­mand: node –v8-options –Fur­ther details on chromium bug