Multiple unit tests on the same data accessible in a callback - javascript

I implemented a parser to extract data from an Excel file and return an object with a certain structure. To make the code work asynchronously, I used a simple callback scheme for the further processing of the extracted data:
parse(inputFile, callback) {
const workbook = new Excel.Workbook();
workbook.xlsx.readFile(inputFile).then((workbook) => {
// Parsing
callback(result);
});
}
Now I want to write a unit test for this routine using Mocha and Chai. Of course I can simply put the expect()statements in the callback function:
const ExcelParser = require('ExcelParser');
var chai = require("chai");
const expect = chai.expect;
describe('Unit::Test parsing', () => {
const excelParser = new ExcelParser();
it('should parse the data correctly', (done) => {
excelParser.parse('sheet.xlsx', (data) => {
expect(data).to.have.property('mainContent');
expect(data['mainContent']).to.be.an('array');
});
done();
});
});
This works fine, but I want to write more tests for the returned data object in separate it() blocks. By doing it this way, the parsing would be repeated for every single subtest.
I tried putting the call of parse() in a before() block and define a callback method that stores the result object in a global variable, but obviously the parsing is not finished by the time the tests are run.
What is the best way to do this, if it is possible at all? Would it help if the parse() method returned a Promise rather than working with callbacks?

You should be able to achieve this by parsing in the before block passing done. Then test in the it statements as normal,
const ExcelParser = require('ExcelParser');
var chai = require("chai");
const expect = chai.expect;
describe('Unit::Test parsing', () => {
const excelParser = new ExcelParser();
let data = null;
before((done) => {
excelParser.parse('sheet.xlsx', (result) => {
data = result;
done();
});
})
it('should parse the data correctly', () => {
expect(data).to.have.property('mainContent');
expect(data['mainContent']).to.be.an('array');
});
it('should have prop foo', () => {
expect(data).to.have.property('foo');
});
...
});
Assuming excelParser.parse returned a promise, you wouldn't need passing done to before:
before(() => {
return excelParser.parse('sheet.xlsx')
.then((result) => {
data = result;
});
})

Related

Mock implementation of another module not working in jest

I am unit testing (using jest) a function named "getTripDetails" (inside file trip.js) that calls another file "getTrace.js" from different module (which exports a function as shown below).
I want to mock the call of function "getTrace" while testing "getTripDetails" function.
file: trips.js
const gpsTrace = require("./gpsTrace");
getTripDetails = async(req, res)=>{
let gpsTraceRes = await gpsTrace(req.body, req.adToken)
//more code...
return {status:200};
}
file: getTrace.js
module.exports = async(payload, token) =>{
try {
//code
} catch (e) {
error(e)
throw new Error(e)
}
}
This is what i tried after reading the docs.
file: test.js
let ctrl = require("./trips");
describe("API -- testing", function () {
it("Trip details", async function () {
jest.mock('./gpsTrace');
const gpsTrace = require('./gpsTrace');
gpsTrace.mockImplementation(() => {});
gpsTrace();
await ctrl.getTripDetails({},{});
expect(response.status).to.eql(200);
});
});
It did not get mocked, instead it was calling the original implementation.
Any suggesstions?
You were pretty close! Here are the updated files with comments describing the changes:
gpsTrace.js
Added a console.log message. We won't see this in the test if the mock works successfully.
module.exports = async (payload, token) => {
try {
//code
console.log("You won't see me in the Jest test because of the mock implementation")
} catch (e) {
error(e)
throw new Error(e)
}
}
trips.js
You needed to export your code to be used in other modules. Seeing as you're calling ctrl.getTripDetails() in the test, it makes sense to export your getTripDetails() on an object at the bottom of the file.
const gpsTrace = require("./gpsTrace");
const getTripDetails = async (req, res) =>{
let gpsTraceRes = await gpsTrace(req.body, req.adToken)
//more code...
return { status:200 };
}
module.exports = {
getTripDetails,
}
gpsTrace.test.js
Make sure to import your modules at the top of the file. Remember that ctrl.getTripDetails({}, {}) calls gpsTrace internally, so no need to call it twice in your test. You also needed to save the response returned from getTripDetails into a variable to be able to compare it: const response = await ctrl.getTripDetails({}, {});.
// make sure your require statements go at the top of the module
const gpsTrace = require('./gpsTrace');
let ctrl = require("./trips");
jest.mock('./gpsTrace');
gpsTrace.mockImplementation(() => {});
describe("API -- testing", function () {
it("Trip details", async function () {
// ctrl.getTripDeals() calls your gpsTrace function internally, so no need to call it twice
// gpsTrace(); <-- can be removed
// you needed to save the returned response into a variable to be able to test it.
const response = await ctrl.getTripDetails({}, {});
expect(response.status).toEqual(200);
});
});
Result
After running the test it now successfully passes. Notice that we DO NOT see the console.log message in the gpsTrace function, which indicates our mockedImplementation of the function is working in the test script. 👍

How do I go about testing anonymous functions in sinon?

I'm new to node.js. I have an abstracted piece of logic that I'd like to write a unit test for. I have this piece of code, in let's say foo.js, which looks like:
'use strict';
const Bar = require('bar.js');
const abstractOutThisLogic = (functionInBar) => async (...args) =>
functionInBar(...args).catch((err) => {
throw new Error(err.message, {
statusCode: err.status
});
});
module.exports = {
create: abstractOutThisLogic(Bar.createThisThing),
delete: abstractOutThisLogic(Bar.deleteThisThing)
};
It's basically an abstracted piece of logic to invoke functions in bar.js. Instead of having different functions for createThisThing and deleteThisThing, I want to have one abstracted function which removes all the duplicate code that these functions might have.
The functions in bar.js look like this:
exports.createThisThing = async (payload) => {
const resp = await externalApi.createThisThing(payload);
return resp;
};
exports.deleteThisThing = async (payload) => {
const resp = await externalApi.deleteThisThing(payload);
return resp;
};
Now, I'm trying to write a unit test to test whether these functions in bar.js are invoked correctly (and how many times it was called etc) by the abstracted function in foo.js.
My unit test in foo.test.js looks like:
it('is trying to test this anonymous function in foo.js', async () => {
const payload = {
id: 'foo',
value: 'baz'
}
const barFnSpy = Sinon.spy(Bar.createThisThing);
await Foo.create(payload);
assert(barFnSpy.calledWith(payload));
});
It appears to me that sinon is having a hard time stubbing/spying on this anonymous function call in foo.js. How can I go about testing the functionality of this abstracted function in foo.js through sinon?
You should test your abstractOutThisLogic function and your Bar.createThisThing function separately.
Pass stub functions to your abstractOutThisLogic function which behave predictably, and you can assert things like how many times the stub was called, the details of the error that gets thrown if the wrapped function throws, etc.
describe("abstracted logic", () => {
it("should handle errors", () => {
const stub = sinon.stub().throws()
expect(abstractOutThisLogic(stub)).toThrow()
})
})
Then write a separate set of tests asserting behavior of the Bar functions, without being wrapped by abstractOutThisLogic.

Loading existing HTML file with JSDOM for frontend unit testing

I'm new to unit testing, and I'm aware my tests may not be valuable or following a specific best practice, but I'm focused on getting this working, which will allow me to test my frontend code using JSDOM.
const { JSDOM } = require('jsdom');
const { describe, it, beforeEach } = require('mocha');
const { expect } = require('chai');
let checkboxes;
const options = {
contentType: 'text/html',
};
describe('component.js', () => {
beforeEach(() => {
JSDOM.fromFile('/Users/johnsoct/Dropbox/Development/andybeverlyschool/dist/individual.html', options).then((dom) => {
checkboxes = dom.window.document.querySelectorAll('.checkbox');
});
});
describe('checkboxes', () => {
it('Checkboxes should be an array', () => {
expect(checkboxes).to.be.a('array');
});
});
});
I'm getting the error "AssertionError: expected undefined to be an array". I'm simply using the array test as a test to ensure I have JSDOM functioning correctly. There are no other errors occurring. Any help would be much appreciated!
fromFile is an async function, meaning that by the time your beforeEach() has finished and the tests start running, it is (probably) still loading the file.
Mocha handles async code in two ways: either return a promise or pass in a callback. So either return the promise from fromFile or do this:
beforeEach(function(done) {
JSDOM.fromFile(myFile)
.then((dom) => {
checkboxes = dom.window.document.querySelectorAll('.checkbox');
})
.then(done, done);
});
The promise version looks like this:
beforeEach(function() {
return JSDOM.fromFile(myFile)
.then((dom) => {
checkboxes = dom.window.document.querySelectorAll('.checkbox');
});
});

How to assert stubbed fetch more than once

Using proxyquire, sinon, and mocha.
I am able to stub fetch on the first call of fetch. But on the second fetch call, which is recursive, I am not able to assert it. From the output, it looks like the assertion may run before the test finishes. You will see this with second fetch console out after assertion.
index.js
var fetch = require('node-fetch');
function a() {
console.log('function a runs');
fetch('https://www.google.com')
.then((e) => {
console.log('first fetch');
b();
})
.catch((e)=> {
console.log('error')
});
}
function b() {
fetch('https://www.google.com')
.then((e) => {
console.log('second fetch');
})
.catch((e)=> {
console.log('error')
});
}
a()
test:
describe('fetch test demo', ()=> {
it('fetch should of called twice', (done)=> {
fetchStub = sinon.stub();
fetchStub2 = sinon.stub();
fetch = sinon.stub();
fetchStub.returns(Promise.resolve('hello'));
fetchStub2.returns(Promise.resolve('hi'));
var promises = [ fetchStub, fetchStub2 ]
fetch.returns(Promise.all(promises));
proxy('../index', {
'node-fetch': fetch
});
fetch.should.have.been.callCount(2);
done()
});
});
fetch test demo
function a runs
1) fetch should of called twice
first fetch
second fetch
lifx alert test
- fetch should of called three times
when rain change is over 50%
- should run fetch twice
0 passing (78ms)
2 pending
1 failing
1) fetch test demo fetch should of called twice:
expected stub to have been called exactly twice, but it was called once
stub(https://www.google.com) => [Promise] { } at a (/home/one/github/lifx-weather/foobar.js:5:3)
AssertionError: expected stub to have been called exactly twice, but it was called once
stub(https://www.google.com) => [Promise] { } at a (foobar.js:5:3)
at Context.it (test/bar.js:22:28)
Updated version
#dman, since you updated your test case I owe you an updated answer. Although rephrased, the scenario is still unorthodox - it seems like you want to ignore in a sense the 'law of gravity' even though you know it's right there in front of you.
I'll try to be as descriptive as possible. You have two functions which are doing async stuff by design. a() calls b() sequentially - by the way this is not recursion. Both functions do not notify their callers upon completion / failure, i.e. they are treated as fire-and-forget.
Now, let's have a look at your test scenario. You create 3 stubs. Two of them resolve to a string and one combining their execution using Promise.all(). Next, you proxy the 'node-fetch' module
proxy('./updated', {
'node-fetch': fetch
});
using the stub that returns the combined execution of stubs 1 & 2. Now, if you print out the resolved value of fetch in either function, you will see that instead of a string it's an array of stubs.
function a () {
console.log('function a runs');
fetch('http://localhost')
.then((e) => {
console.log('first fetch', e);
b();
})
.catch((e) => {
console.log('error');
});
}
Which I guess is not the intended output. But let's move over as this is not killing your test anyway. Next, you have added the assertion together with the done() statement.
fetch.should.have.been.callCount(2);
done();
The issue here is that whether you are using done() or not, the effect would be exactly the same. You are executing your scenario in sync mode. Of course in this case, the assertion will always fail. But the important thing here is to understand why.
So, let's rewrite your scenario to mimic the async nature of the behavior you want to validate.
'use strict';
const chai = require('chai');
const sinon = require('sinon');
const SinonChai = require('sinon-chai');
chai.use(SinonChai);
chai.should();
const proxy = require('proxyquire');
describe('fetch test demo', () => {
it('fetch should of called twice', (done) => {
var fetchStub = sinon.stub();
var fetchStub2 = sinon.stub();
var fetch = sinon.stub();
fetchStub.returns(Promise.resolve('hello'));
fetchStub2.returns(Promise.resolve('hi'));
var promises = [fetchStub, fetchStub2];
fetch.returns(Promise.all(promises));
proxy('./updated', {
'node-fetch': fetch
});
setTimeout(() => {
fetch.should.have.been.callCount(2);
done();
}, 10);
});
});
As you can see, the only change made was wrapping the assertion within a timer block. Nothing much - just wait for 10ms and then assert. Now the test passes as expected. Why?
Well, to me it's pretty straightforward. You want to test 2 sequentially executed async functions and still run your assertions in sync mode. That sounds cool, but it's not gonna happen :) So you have 2 options:
Have your functions notify callers upon completion and then run your assertions in truly async mode
Mimic the async nature of things using unorthodox techniques
Reply based on original test scenario
It can be done. I've re-factored your provided files a bit so that
can be executed.
index.js
const fetch = require('node-fetch');
const sendAlert = require('./alerts').sendAlert;
module.exports.init = function () {
return new Promise((resolve, reject) => {
fetch('https://localhost')
.then(function () {
sendAlert().then(() => {
resolve();
}).catch(
e => reject(e)
);
})
.catch(e => {
reject(e);
});
});
};
alerts.js
const fetch = require('node-fetch');
module.exports.sendAlert = function () {
return new Promise((resolve, reject) => {
fetch('https://localhost')
.then(function () {
resolve();
}).catch((e) => {
reject(e);
});
});
};
test.js
'use strict';
const chai = require('chai');
const sinon = require('sinon');
const SinonChai = require('sinon-chai');
chai.use(SinonChai);
chai.should();
const proxy = require('proxyquire');
describe.only('lifx alert test', () => {
it('fetch should of called twice', (done) => {
var body = {
'hourly': {
data: [{
time: 1493413200,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 297.17
}]
}
};
var response = {
json: () => {
return body;
}
};
const fetchStub = sinon.stub();
fetchStub.returns(Promise.resolve(response));
fetchStub['#global'] = true;
var stubs = {
'node-fetch': fetchStub
};
const p1 = proxy('./index', stubs);
p1.init().then(() => {
try {
fetchStub.should.have.been.calledTwice;
done();
} catch (e) {
done(e);
}
}).catch((e) => done(e));
});
});
What you're trying to do though is a bit unorthodox when it comes to
good unit testing practices. Although proxyquire supports this
mode of stubbing through a feature called global overrides, it is
explained here why should anyone think twice before going down
this path.
In order to make your example pass the test, you just need to add an
extra attribute to the Sinon stub called #global and set it to
true. This flag overrides the require() caching mechanism and
uses the provided stub no matter which module is called from.
So, although what you're asking can be done I will have to agree with
the users that commented your question, that this should not be
adopted as a proper way of structuring your tests.
Here is also a alternative way to do this using Promise.all().
Note: this won't work if using fetch's json method and you need to pass data in the resolve() for logic on data. It will only pass in the stubs when resolved. However, it will assert the number of times called.
describe('fetch test demo', () => {
it('fetch should of called twice', () => {
let fetchStub = sinon.stub();
let fetchStub2 = sinon.stub();
let fetch = sinon.stub();
fetchStub.returns(Promise.resolve('hello'));
fetchStub2.returns(Promise.resolve('hi'));
var promises = [ fetchStub, fetchStub2 ]
var promise = Promise.all(promises);
fetch.returns(promise);
proxy('../foobar', { 'node-fetch': fetch });
return promise.then(() => {
fetch.should.have.callCount(2);
});
});
});
I have found another way to get things done.
May be this could work for someone.
describe('Parent', () => {
let array: any = [];
before(async () => {
array = await someAsyncDataFetchFunction();
asyncTests();
});
it('Dummy test to run before()',async () => {
expect(0).to.equal(0); // You can use this test to getting confirm whether data fetch is completed or not.
});
function asyncTests() {
array.forEach((currentValue: any) => {
describe('Child', async () => {
it('Test '+ currentValue ,() => {
expect(currentValue).to.equal(true);
})
})
});
}
});
That's how I achieved the assertion on every element of the array. (Array data is being fetch asynchronously).

Chai spies and async calls not call once

I m building an application in which I need to test some callback behaviours inside of an express callback resolution.
Actually, it looks like :
const callbackRender = (httpResponse, response) => {
console.log(httpResponse) // logs the good httpResponse object
if (httpResponse.content.content) response.send(httpResponse.content.content)
else response.render(httpResponse.content.page)
}
const callback = (injector, route) => {
return (request, response) => {
const ctrl = injector.get(route.controller)
const result = ctrl[route.controllerMethod](new HttpRequest())
if (result.then) {
return result.then(res => callbackRender(res, response))
} else {
callbackRender(result, response)
}
}
}
The two failing tests look like :
it('should call the callback render method when httpResponse is a promise', (done) => {
const mock = sinon.mock(injector)
const ctrl = new UserControllerMock()
const routes = routeParser.parseRoutes()
mock.expects('get').returns(ctrl)
const spy = chai.spy.on(callbackRender)
callback(injector, routes[3])(request, response).then((res) => {
expect(spy).to.have.been.called.once
mock.verify()
mock.restore()
done()
})
})
it('should call the callback render method when httpResponse is not a promise', () => {
const mock = sinon.mock(injector)
const ctrl = new UserControllerMock()
const routes = routeParser.parseRoutes()
mock.expects('get').returns(ctrl)
const spy = chai.spy.on(callbackRender)
callback(injector, routes[1])(request, response)
expect(spy).to.have.been.called.once
mock.verify()
mock.restore()
})
It seems that chai-spies isn't able to detect that my callbackRender function is called in the callback method.
The fact is that, when I log my method, I pass inside of it each time I need it to do.
Does anybody has an idea ?
EDIT : The request / response definition in beforeEach
beforeEach(() => {
request = {
body: {},
params: {},
query: {}
}
response = {
send: () => {
},
render: () => {
}
}});
Spies/stubs/mocks can only work if they can replace the original function (with a wrapped version), or if they get passed explicitly (which isn't the case in your code).
In your case, callbackRender isn't replaced (it can't be, due to the const but also because it has no "parent" object in which it can be replaced), so any code that will call it (like callback) will call the original function, not the spy.
A solution depends on how exactly your code is structured.
If callback and callbackRender are located in a separate module together, you might be able to use rewire to "replace" callbackRender with a spy.
However, one caveat is that rewire also can't replace const variables, so your code would have to change.

Categories