I'm trying to test that certain function is called in a callback, however I don't understand how to wrap the outer function. I'm using mocha as my testing suite, with chai as my assertion library, and sinon for my fakes.
fileToTest.js
const externalController = require('./externalController');
const processData = function processData( (req, res) {
externalController.getAllTablesFromDb( (errors, results) => {
// call f1 if there are errors while retrieving from db
if (errors) {
res.f1();
} else {
res.f2(results);
}
)};
};
module.exports.processData = processData;
In the end I need to verify that res.f1 will be called if there are errors from getAllTablesFromDb, and res.f2 will be called if there are no errors.
As can be seen from this snippet externalController.getAllTablesFromDb is a function that takes a callback, which in this case I have created using an arrow function.
Can someone explain how I can force the callback down error or success for getAllTablesFromDb so that I can use a spy or a mock to verify f1 or f2 was called?
var errorSpy = sinon.spy(res, "f1");
var successSpy = sinon.spy(res, "f2");
// your function call
// error
expect(errorSpy.calledOnce);
expect(successSpy.notCalled);
// without errors
expect(errorSpy.notCalled);
expect(successSpy.calledOnce);
One possible solution is to extract the callback then force that down the desired path of failure or success. This extraction can be done using a npm package called proxyquire. The callback is extracted by removing the require line from the beginning of the file then:
const proxyquire = require('proxyquire');
const externalController = proxyquire('path to externalController',
'path to externalController dependency', {
functionToReplace: (callback) => { return callback }
}
});
const extractedCallback = externalController.getAllTablesFromDb(error, results);
Then you can call extractedCallback with the parameters you want.
extractedCallback(myArg1, myArg2);
and set a spy on res.f1 and res.f2
sinon.spy(res, 'f1');
And do any assertion logic you need to.
Related
I am writing uni-test and tring to isolate my code as much as possible. I am using Mocha and chai for writing test and ES5 Syntax.
The problem is, I am not able to find any solution to stub constructor.
Q.reject(new Error(obj.someFunction());
In above example, I know how to test promise, how to stub my inner function, but how I can stub Error constructor? And how I can check callWithExactly(), etc.
oStubSomeFunction = sinon.stub(obj, "someFunction")
I am using for normal function. I didn't find any relevant example in ChaiDocumentation
If you really want to stub the Error constructor you can use the Node.js global namespace:
code.js
exports.func = function() {
return new Error('error message');
}
code.test.js
const { func } = require('./code');
const sinon = require('sinon');
const assert = require('assert');
describe('func', function () {
it('should create an Error', function () {
const errorStub = sinon.stub(global, 'Error'); // <= stub the global Error
errorStub.callsFake(function(message) { // <= use callsFake to create a mock constructor
this.myMessage = 'stubbed: ' + message;
});
const result = func();
errorStub.restore(); // <= restore Error before doing anything else
assert(result.myMessage === 'stubbed: error message'); // Success!
sinon.assert.calledWithExactly(errorStub, 'error message'); // Success!
});
});
If you do this you'll want to restore Error before doing absolutely anything else...even assertions work using Error so the original Error needs to be in place in case an assertion fails.
It's a lot safer to just spy on Error:
const { func } = require('./code');
const sinon = require('sinon');
describe('func', function () {
it('should create an Error', function () {
const errorSpy = sinon.spy(global, 'Error'); // <= spy on the global Error
func();
sinon.assert.calledWithExactly(errorSpy, 'error message'); // Success!
errorSpy.restore();
});
});
...and ultimately there is a limit to how isolated code under test can get. For something as low-level as Error you might want to look at leaving it completely intact and testing for a thrown Error using something like Chai's .throw assertion.
I am calling following code which imports a search query function
var match_all = require('./match_all');
var data=match_all.func();
console.log('uo yoyo');
console.log(data);
Match_all is as follows:
var string_of_da;
//a function which queries for all pid and syscalls
var string = function(){
var client = new elasticsearch.Client({
.....
})
//queries to ES
function getmeres(client,syscall){
client.search({
........
},function (error,response) {
.......
showdocs(d)
}
});
}
function showdocs(d){
var da = d["hits"]["hits"].map(function(i){
return i['_source'];
});
string_of_da = JSON.stringify(da,null,'\t');
}
return string_of_da;
};
module.exports.func = string;
module.exports.string = string_of_da;
Now when I execute my earlier js program, it queries properly, but before match_all.func() is called and returns string, console.log is called. So my output looks like
uo yoyo
undefined
...elasticsearch search logs...
what is going wrong here?
because you are using call backs .call back work asynchronous. so thats why console.log is printing first.in asynchronous it will not wait till he get a response .Callback is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. Node makes heavy use of callbacks. All the APIs of Node are written in such a way that they support callbacks.
I have a saga:
export function* loadData() {
const requestURL = 'http://www.web.address/thing';
try {
const data = yield call(getRequest, requestURL);
yield put(dataLoaded(data));
} catch (err) {
yield put(dataError(err));
}
}
I'm trying to test it like this:
describe('loadData saga', () => {
describe('200 response', () => {
const getReq = jest.fn();
const requestURL = 'mock.url.com';
const generator = cloneableGenerator(loadData)();
it('sends the request to the correct url', () => {
expect(generator.next(getReq, requestURL).value).toEqual(call(getReq, requestURL));
});
});
This test fails because the generator doesn't seem to heed what I pass into the next function, and I don't know why. That is to say, the test receives http://www.web.address/thing as the url, and the getRequest function, instead of what I've tried to pass in in the .next() function.
This also did not work:
generator.next(getReq(requestURL))
What am I misunderstanding?
You you shouldn't actually be passing any args into next() here, as your saga has no previous yields and doesn't pass any dynamic data from previous yield's to the call - it has everything it needs already in scope
So in your test you would check next().value is as expected using the non-mocked values
import {getRequest} from 'same/place/as/saga/loads/getRequest';
const requestURL = 'http://www.web.address/thing';
expect(generator.next().value).toEqual(call(getRequest, requestURL));
The problem you have is you are attempting to mock and pass in stubs which the saga has no way of injecting them in this case
Remember that a yield will stop execution of the generator until the next iteration so you are not actually running a request here, only stopping and receiving back an object of instructions of how to call getRequest with what args to pass.
Using .next(someData) after your above test will however allow you to pass in mock data as a simulation of the response of your request
const mockData = {};
expect(generator.next(mockData).value).toEqual(put(dataLoaded(mockData)))
Think of args to next() as allowing mocking the return value of the previous yield
I have a node.js app using express 4 and this is my controller:
var service = require('./category.service');
module.exports = {
findAll: (request, response) => {
service.findAll().then((categories) => {
response.status(200).send(categories);
}, (error) => {
response.status(error.statusCode || 500).json(error);
});
}
};
It calls my service which returns a promise. Everything works but I am having trouble when trying to unit test it.
Basically, I would like to make sure that based on what my service returns, I flush the response with the right status code and body.
So with mocha and sinon it looks something like:
it('Should call service to find all the categories', (done) => {
// Arrange
var expectedCategories = ['foo', 'bar'];
var findAllStub = sandbox.stub(service, 'findAll');
findAllStub.resolves(expectedCategories);
var response = {
status: () => { return response; },
send: () => {}
};
sandbox.spy(response, 'status');
sandbox.spy(response, 'send');
// Act
controller.findAll({}, response);
// Assert
expect(findAllStub.called).to.be.ok;
expect(findAllStub.callCount).to.equal(1);
expect(response.status).to.be.calledWith(200); // not working
expect(response.send).to.be.called; // not working
done();
});
I have tested my similar scenarios when the function I am testing returns itself a promise since I can hook my assertions in the then.
I also have tried to wrap controller.findAll with a Promise and resolve it from the response.send but it didn't work neither.
You should move your assert section into the res.send method to make sure all async tasks are done before the assertions:
var response = {
status: () => { return response; },
send: () => {
try {
// Assert
expect(findAllStub.called).to.be.ok;
expect(findAllStub.callCount).to.equal(1);
expect(response.status).to.be.calledWith(200); // not working
// expect(response.send).to.be.called; // not needed anymore
done();
} catch (err) {
done(err);
}
},
};
The idea here is to have the promise which service.findAll() returns accessible inside the test's code without calling the service. As far as I can see sinon-as-promised which you probably use does not allow to do so. So I just used a native Promise (hope your node version is not too old for it).
const aPromise = Promise.resolve(expectedCategories);
var findAllStub = sandbox.stub(service, 'findAll');
findAllStub.returns(aPromise);
// response = { .... }
controller.findAll({}, response);
aPromise.then(() => {
expect(response.status).to.be.calledWith(200);
expect(response.send).to.be.called;
});
When code is difficult to test it can indicate that there could be different design possibilities to explore, which promote easy testing. What jumps out is that service is enclosed in your module, and the dependency is not exposed at all. I feel like the goal shouldn't be to find a way to test your code AS IS but to find an optimal design.
IMO The goal is to find a way to expose service so that your test can provide a stubbed implementation, so that the logic of findAll can be tested in isolation, synchronously.
One way to do this is to use a library like mockery or rewire. Both are fairly easy to use, (in my experience mockery starts to degrade and become very difficult to maintain as your test suite and number of modules grow) They would allow you to patch the var service = require('./category.service'); by providing your own service object with its own findAll defined.
Another way is to rearchitect your code to expose the service to the caller, in some way. This would allow your caller (the unit test) to provide its own service stub.
One easy way to do this would be to export a function contstructor instead of an object.
module.exports = (userService) => {
// default to the required service
this.service = userService || service;
this.findAll = (request, response) => {
this.service.findAll().then((categories) => {
response.status(200).send(categories);
}, (error) => {
response.status(error.statusCode || 500).json(error);
});
}
};
var ServiceConstructor = require('yourmodule');
var service = new ServiceConstructor();
Now the test can create a stub for service and provide it to the ServiceConstructor to exercise the findAll method. Removing the need for an asynchronous test altogether.
In the file I would like to test, I have the following code:
var httpGet = Promise.promisify(require("request").get);
httpGet(endpoint, {
auth: {bearer: req.body.access_token},
json: true
})
.then(...)
Now, in my tests, I want to make sure that httpGet was called once, and make sure the parameters are valid. Before being promisified, my test looked like this:
beforeEach(function () {
request.get = sinon.stub()
.yields(null, null, {error: "test error", error_description: "fake google error."});
});
afterEach(function () {
expect(request.get).to.have.been.calledOnce();
var requestArgs = request.get.args[0];
var uri = requestArgs[0];
expect(uri).to.equal(endpoint);
//...
});
Unfortunately this no longer works when request.get is promisified. I tried stubbing request.getAsync instead (since bluebird appends "Async" to promisified functions), but that does not work either. Any ideas?
Promise.promisify doesn't modify the object, it simply takes a function and returns a new function, it is completely unaware that the function even belongs to "request".
"Async" suffixed methods are added to the object when using promisify All
Promise.promisifyAll(require("request"));
request.getAsync = sinon.stub()
.yields(null, null, {error: "test error", error_description: "fake google error."});
expect(request.getAsync).to.have.been.calledOnce();
Just for future reference I've solved this a bit differently, and I think a little cleaner. This is typescript, but basically the same thing.
fileBeingTested.ts
import * as Bluebird from 'bluebird';
import * as needsPromise from 'needs-promise';
const methodAsync = Bluebird.promisify(needsPromise.method);
export function whatever() {
methodAsync().then(...).catch(...);
}
test.spec.ts
import * as needsPromise from 'needs-promise';
import * as sinon form 'sinon';
const methodStub = sinon.stub(needsPromise, method);
import { whatever } from './fileBeingTested';
Then you use the methodStub to manage what calls happen. You can ignore that it's being promisified and just manage it's normal behavior. for example if you need it to error.
methodStub.callsFake((arg, callback) => {
callback({ error: 'Error' }, []);
});
The promisified version will throw the error and you'll get it in the catch.
Anyone coming across this. I have small utility func
function stubCBForPromisify(stub) {
let cbFn = function() {
let args = [...arguments];
args.shift();
return stub(...args);
};
return cbFn.bind(cbFn, () => ({}));
}
In test
var getStub = sinon.stub().yields(null, {error: "test error", error_description: "fake google error."})
sinon.stub(require("request"), 'get', stubCBForPromisify(getStub))
expect(getStub).to.have.been.calledOnce();
I was running into trouble testing this using tape and proxyquire. I'm not sure what pattern/framework people are using that allowed them to modify the required'd request object directly as shown in the accepted answer. In my case, in the file I want to test I require('jsonFile'), then call bluebird.promisifyAll(jsonFile). Under normal conditions this creates a readFileAsync method that I want to stub. However, if during testing I try to use proxyquire to pass in a stub, the call to promisifyAll overwrites my stub.
I was able to fix this by also stubbing promisifyAll to be a no-op. As shown this might be too coarse if you rely on some of the async methods to be created as-is.
core.js:
var jsonFile = require('jsonfile');
var Promise = require('bluebird');
Promise.promisifyAll(jsonFile);
exports.getFile = function(path) {
// I want to stub this method during tests. It is
// created by promisifyAll
return jsonFile.readFileAsync(path);
}
core-test.js:
var proxyquire = require('proxyquire');
var tape = require('tape');
var sinon = require('sinon');
require('sinon-as-promised');
tape('stub readFileAsync', function(t) {
var core = proxyquire('./core', {
'jsonfile': {
readFileAsync: sinon.stub().resolves({})
},
'bluebird': { promisifyAll: function() {} }
});
// Now core.getFile() will use my stubbed function, and it
// won't be overwritten by promisifyAll.
});