Testing functions with properties using Jest and Sinon - node.js

am trying to write tests to test streams on my app, dealing with fs library with its createWriteStream function, so the stub i created is as follows:
writeStreamStub = sinon.stub()
onceStreamEventStub = sinon.stub()
endStreamStub = sinon.stub()
onStreamStub = sinon.stub()
createStreamStub = sinon.stub(fs, 'createWriteStream').returns({
write: writeStreamStub,
once: onceStreamEventStub,
end: endStreamStub,
on: onStreamStub
})
So now I can test for whether the functions are called and the returned functions are also called. But I am using the --coverage flag and the code of the callbacks of the returned functions is not covered, the write method is called inside a process.nextTick and I have no idea how to go about this. Is it possible to cover the whole code and the code inside the callbacks, and if so, how do I go about it. Thanks in advance.
N.B. The variables are globaly declared

If there's no cogent reason to use both sinon and jest, I'd recommend just using one library. If you decide to go with jest, here's a simple example. Assume you have a class like
const fs = require('fs');
module.exports = class FileWriter {
constructor() {
this.writer = fs.createWriteStream('./testfile.txt');
}
writeFile() {
process.nextTick(() => {
this.writeContent('hello world');
});
}
writeContent(content) {
this.writer.write(content);
this.writer.end();
}
};
and in your unit-test you want to mock the behaviour of all the used fs-functions (createWriteStream, writer, end in this case) and just check if they are called with the correct arguments. You could do this with something like this:
const fs = require('fs');
const FileWriter = require('./FileWriter');
// use this to have mocks for all of fs' functions (you could use jest.fn() instead as well)
jest.mock('fs');
describe('FileWriter', () => {
it('should write file with correct args', async () => {
const writeStub = jest.fn().mockReturnValue(true);
const endStub = jest.fn().mockReturnValue(true);
const writeStreamStub = fs.createWriteStream.mockReturnValue({
write: writeStub,
end: endStub,
});
const fileWriter = new FileWriter();
fileWriter.writeFile();
await waitForNextTick();
expect(writeStreamStub).toBeCalledWith('./testfile.txt');
expect(writeStub).toBeCalledWith('hello world');
expect(endStub).toHaveBeenCalled();
});
});
function waitForNextTick() {
return new Promise(resolve => process.nextTick(resolve));
}

Related

Why can't I properly stub the twilio library with sinon?

In my code, I have:
function handleMessage() {
const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
let recordings = twilio.recordings(foundConference.RecordingSid);
console.log('recordings', recordings);
return recordings.remove();
}
And in my stub, I have:
const sinon = require('sinon');
const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
exports.twilioRecordings = () => {
console.log('about to stub', twilio.recordings);
sinon.stub(twilio, 'recordings').returns('here');
console.log('finished stub', twilio.recordings);
return;
};
However, it doesn't actually create a stubbed function. It's still using the original recordings function. What am I doing wrong?
Twilio npm package returns a function which creates a new object on every call, it's not a singleton. So your stubbed twilio instance is scoped to the test only.
Also twilio.recordings (as all other properties though) is defined through the getter function in prototype, so they are read only:
Object.defineProperty(Twilio.prototype,
'recordings', {
get: function() {
return this.api.account.recordings;
}
});
So, stubbing actual twilio instance have no effect. Except if you change the instance's prototype, but I don't think it worth doing for just unit testing.
I'd suggest you to refactor your code to put twilio initialization into separate method:
function getTwilio() {
return require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
}
Next your hangleMessage will look like:
function handleMessage() {
const twilio = this.getTwilio();
const recordings = twilio.recordings(...);
...
}
And next, in your test you can stub getTwilio() to return stub:
const twilioStub = {
recordings: sinon.stub(),
remove: sinon.stub()
}
sinon.stub(myAwesomeModule, 'getTwilio').returns(twilioStub);
You also can consider using mock-require package:
const sinon = require('sinon');
const mock = require('mock-require');
mock('twilio', () => ({
recordings: sinon.stub(),
}));
Here is a question about how-to mock dependencies, there might be other helpful libraries to stub required module.
Hope it helps.

How to mock an external module's function with Jest

Can Jest's mocking handle functions from modules I didn't write?
node-yelp-api-v3 has Yelp.searchBusiness(String) but my attempts to use Jest's mocking functionality are unsuccessful. The Jest examples seem to assume that I'm mocking a module I have in the project. From the documentation I'm also unclear how to mock a specific function in a module.
Neither of these are working:
jest.mock('Yelp.searchBusiness', () => {
return jest.fn(() => [{<stubbed_json>}])
})
or
jest.mock('Yelp', () => {
return jest.fn(() => [{<stubbed_json>}])
})
I'm currently using sinon but would like to use just Jest. This Sinon approach works:
var chai = require('chai')
var should = chai.should()
var agent = require('supertest').agent(require('../../app'))
const Yelp = require('node-yelp-api-v3')
var sinon = require('sinon')
var sandbox
describe('router', function(){
beforeEach(function(){
sandbox = sinon.sandbox.create()
stub = sandbox.stub(Yelp.prototype, 'searchBusiness')
})
afterEach(function(){
sandbox.restore()
})
it ('should render index at /', (done) => {
/* this get invokes Yelp.searchBusiness */
agent
.get('/')
.end(function(err, res) {
res.status.should.equal(200)
res.text.should.contain('open_gyro_outline_500.jpeg')
done()
})
})
})
Mocking external modules is explained here.
If the module you are mocking is a Node module (e.g.: lodash), the mock should be placed in the __mocks__ directory adjacent to node_modules (unless you configured roots to point to a folder other than the project root) and will be automatically mocked. There's no need to explicitly call jest.mock('module_name').
For your exact case this would mean you need to create a folder __mocks__ with a file node-yelp-api-v3.js in it. In that file you create a mock object from the original module using genMockFromModule and override the method you want to mock.
// __mocks__/node-yelp-api-v3.js
const yelp = jest.genMockFromModule('node-yelp-api-v3')
function searchBusiness() {
return [{<stubbed_json>}]
}
yelp.searchBusiness = searchBusiness
module.exports = yelp
Additionally you could also wrap the searchBusiness in jest.fn if you want to call assertions like searchBusiness.mock.calls.length for this method later.
You can also do this:
jest.mock('Yelp', () => ({
searchBusiness: jest.fn(() => [{<stubbed_json>}])
})
And then you'll be able to call things like expect(Yelp.searchBusiness).toHaveBeenCalled() etc.

Node.js - load and execute multiple functions using Promise.all()

I am developing a "plugins" concept whereby I have a series of files, each containing a single function (a plugin). I would like to automatically load and execute these using promise.all().
Problem: each plugin function does not execute.
Here is my example plugin plugins/example.js:
"use strict";
exports = function() {
return new Promise(function(resolve, reject) {
console.log("Plugin running....");
setTimeout(resolve, 200, 'example plugin succeeded!');
});
};
From my app.js I then load all plugins using the require-all NPM module:
const plugins = require('require-all')(__dirname + '/plugins');
I then try to execute all as part of my promise chain:
return Promise.all([plugins]);
No logging takes place from the function. Interestingly when I log the contents of plugins, I see and empty object:
{
"example": {}
}
Can anyone advise why the example function is not being called?
One way of doing it would be something like following. Lets say that there is a plugins directory with files like pluginA.js, pluginB.js, ..., pluginZ.js. As you stated in your question, exported value from those plugins is always a function that will return a promise. I would create plugins/index.js that would export everything from those plugins like:
// plugins/index.js
'use strict'
const pluginA = require('./pluginA')
const pluginB = require('./pluginB')
...
const pluginZ = require('./pluginZ')
module.exports = [ pluginA, pluginB, ..., pluginZ ]
So then you could use this as following:
// foo.js
'use strict'
const _ = require('lodash')
const plugins = require('./plugins')
Promise.all(_.map(plugins, (fn) => fn()))
.then((data) => console.log(data))
.catch((err) => console.log(err))
The RequireAll plugin returns an object containing the plugin name as a key, and the import as the value, so you'll have to get the values and then call those functions to get the promises, and then you'd have an array of promises
const plugins = require('require-all')(__dirname + '/plugins');
var promises = Object.keys(plugins).map(function(key) {
return plugins[key]();
});
Promise.all(promises).then(function() {
// all done
}).catch(function(err) {
// fail
});

How can I mock a function/property of a node module exported as a function using sinon?

I have a service module that is exported as a function. I need to pass a couple of things into it, like a configuration object so it does need to retain this structure. I am trying to stub out a function from the service but can't figure it out. In my app, I have a function that makes an API call that is problematic during testing so I'd like to stub it. (I understand I'd have to write my test differently to handle the async issue)
// myService.js
module.exports = function(config) {
function foo() {
returns 'bar';
}
return {
foo: foo
};
};
// test.js
var config = require('../../config');
var request = require('supertest');
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
var myService = require('./myService.js')(config);
describe('Simple test', function(done) {
it('should expect "something else", function(done) {
var stub = sinon.stub(myService, 'foo').returns('something else');
request(server) // this object is passed into my test. I'm using Express
.get('/testRoute')
.expect(200)
.expect(function(res) {
expect(res.body).to.equal('something else');
stub.restore();
})
.end(done);
});
});
* /testRoute I set up as a simple GET route that simply returns the value from myService.foo()
The above is not working, and I believe it has to do with the way my service is exporting. If I write the service as below, the stub works fine.
module.exports = {
test: function() {
return 'something';
}
};
But again, I need to be able to pass in information to the module so I would like to keep my modules in the original structure above. Is there a way to stub a function from a module that exports in that manner? I was also looking into proxyquire but not sure if that is the answer.
The reason why your test stub does not work is that the foo function is created every time the module initializer is called. As you discovered, when you have a static method on the module, then you are able to stub.
There are a variety of solutions to this problem-- but the simplest is to expose the method statically.
// myService.js
module.exports = function(config) {
return {
foo: foo
};
};
var foo = module.exports.foo = function foo() {
return 'bar'
}
It's ugly, but works.
What if the foo function has a closure to variables within the service (which is why it lives within the service initializer). Then unfortunately these need to be explicitly passed in.
// myService.js
module.exports = function(config) {
return {
foo: foo
};
};
var foo = module.exports.foo = function(config) {
return function foo() {
return config.bar;
}
}
Now you can safely stub the module.
However, how you are stubbing should be considered unsafe. Only if your test works perfectly does the stub get cleaned up. You should always stub within the before and after (or beforeEach and afterEach) fixtures, such as:
// We are not configuring the module, so the call with config is not needed
var myService = require('./myService.js');
describe('Simple test', function(done) {
beforeEach(function () {
// First example, above
this.myStub = sinon.stub(myService, foo).returns('something else');
// Second example, above
this.myStub = sinon.stub(myService, foo).returns(function () {
returns 'something else';
});
});
afterEach(function () {
this.myStub.restore();
});
it('should expect "something else", function(done) {
request(server) // this object is passed into my test. I'm using Express
.get('/testRoute')
.expect(200)
.expect(function(res) {
expect(res.body).to.equal('something else');
})
.end(done);
});
});
There are other options to be able to stub dependencies using dependency injection. I recommend you look at https://github.com/vkarpov15/wagner-core or my own https://github.com/CaptEmulation/service-builder

Sinon.js and testing with events and new

i'm trying to mock a node.js application, but it doesn't work as expected.
I have a node.js Module called GpioPlugin with following method:
function listenEvents(eventId, opts) {
if(!opts.pin) {
throw new Error("option 'pin' is missing");
}
var listenPort = new onOff(opts.pin, 'in', 'both', {persistentWatch: true});
listenPort.watch(function(err, value) {
process.emit(eventId+'', value);
});
}
if(typeof exports !== 'undefined') {
exports.listenEvents = listenEvents;
}
and now i want to write a test using sinon for this method, but i don't know how... What would be the best way to test this?
This tree parts would be fine, if they get tested:
Error (no problem)
generation of onOff (how?)
event with correct params
If it's not already, you're going to want to put onOff into a module so that your test can inject a stub in.
var sinon = require("sinon");
var process = require("process");
var onOffModule = require(PATH_TO_ONOFF); //See note
var gpio = require(PATH_TO_GPIO);
var onOffStub;
var fakeListenPort;
beforeEach(function () {
//Stub the process.emit method
sinon.stub(process, "emit");
//Constructor for a mock object to be returned by calls to our stubbed onOff function
fakeListenPort = {
this.watch = function(callback) {
this.callback = callback; //Expose the callback passed into the watch function
};
};
//Create stub for our onOff;
onOffStub = sinon.stub(onOffModule, "onOff", function () {
return fakeListenPort;
});
});
//Omitted restoring the stubs after each test
describe('the GpioPlugin module', function () {
it('example test', function () {
gpio.listenEvents("evtId", OPTS);
assert(onOffStub.callCount === 1); //Make sure the stub was called
//You can check that it was called with proper arguments here
fakeListenPort.callback(null, "Value"); //Trigger the callback passed to listenPort.watch
assert(process.emit.calledWith("evtId", "Value")); //Check that process.emit was called with the right values
});
});
Note: The exact mechanics of replacing onOff with a stub may vary depending how you require it.
Things get a little more complicated if you require onOff directly, rather than requiring a module that includes onOff. In that case I think you might need to look into something like proxyquire.

Resources