Please consider the following scenario, but I could not solve it. A class are called under the router("/"). I would like to test router ("/") call and mock myFunc result.
class A {
myFunc = async () => {
await ...
return result
}
}
Controller file - C:
router.get("/", async (req, res) => {
var a = new A();
a.myFunc()
.then(result => {
res.json({"message": result});
}).catch(e => {
return []
})
});
C.test.js:
const request = require("supertest");
const app = require("../C");
jest.mock('A');
test("Test route /", async done => {
const myFuncMock = jest.fn().mockImplementation(() => [...]);
A.prototype.myFunc = myFuncMock;
await request(app)
.get("/")
.then(res => {
expect(res.status).toBe(200); // Not asserted
});
done()
});
I could not success to mock function results under the router.
Here is the integration test solution:
app.js:
const express = require('express');
const { Router } = require('express');
const A = require('./a');
const app = express();
const router = Router();
router.get('/', async (req, res) => {
const a = new A();
a.myFunc()
.then((result) => {
res.json({ message: result });
})
.catch((e) => {
return [];
});
});
app.use(router);
module.exports = app;
a.js:
class A {
myFunc = async () => {
const result = await 'real data';
return result;
};
}
module.exports = A;
app.test.js:
const app = require('./app');
const request = require('supertest');
const A = require('./a');
jest.mock('./a', () => {
const mA = { myFunc: jest.fn() };
return jest.fn(() => mA);
});
describe('61505692', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should pass', () => {
const mA = new A();
mA.myFunc.mockResolvedValueOnce('fake result');
return request(app)
.get('/')
.then((res) => {
expect(res.status).toBe(200);
});
});
});
integration test results with coverage report:
PASS stackoverflow/61505692/app.test.js (11.834s)
61505692
✓ should pass (34ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 92.31 | 100 | 75 | 91.67 |
app.js | 92.31 | 100 | 75 | 91.67 | 16
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 13.785s
Related
The following line in my unit test:
sandbox.stub(myAPI,'getPeople').returns([500, {errorCode: '500'}])
is giving me the following error:
TypeError: Cannot stub non-existent property getPeople
I'm trying to stub an error response from my API call to test my error handling.
The unit test:
const chai = require('chai');
const sinonChai = require('sinon-chai');
const sinon = require('sinon');
const { getPeopleHandler } = require('../services/handlers/get-people-handler');
const { expect } = chai;
const myAPI = require('../services/api');
chai.use(sinonChai);
describe('handle error', () => {
let req;
let res;
let sandbox;
describe('getPeopleHandler() with error', () => {
before(() => {
req = {
session: {}
};
res = {
render: () => ({})
};
sandbox = sinon.createSandbox();
});
beforeEach(() => {
sandbox.stub(myAPI,'getPeople').returns([500, {errorCode: '500'}]);
})
afterEach(() => {
sandbox.restore();
});
it('should render the error page', async () => {
sandbox.stub(res, 'render').returns({});
res.locals = {};
await getPeopleHandler(req, res);
expect(res.render).to.have.been.calledOnceWith('error.html');
});
});
});
api.js
const fetch = require('node-fetch');
const getPeople = (url) => {
console.log(`About to call API at ${url}`);
return new Promise((resolve, reject) => {
fetch(url, { method: 'GET' })
.then(res => Promise.all([res.status, res.json()]))
.then(([status, jsonData]) => {
resolve([status, jsonData]);
}).catch(error => {
console.log('e', error)
reject(error)
})
})
};
module.exports = getPeople;
get-people-handler.js
const callAPI = require('../../services/api');
const config = require('../../config/config');
const getPeopleHandler = async (req, res) => {
const url = config.getPeopledataUrl;
const response = await callAPI(url);
console.log(`API call status = ${response[0]}`);
if (response[0] === 200) {
res.locals.list = response[1];
res.render('people.html');
} else {
res.locals.error = response[0];
res.render('error.html');
}
};
module.exports = { getPeopleHandler };
You are stub the getPeople function because you use module.exports. If you want to export a object, you should do it like this:
module.exports = { getPeople };
sinon.stub(object, "method")
Replaces object.method with a stub function. An exception is thrown if the property is not already a function.
Below is a working example:
api.js:
const fetch = require('node-fetch');
const getPeople = (url) => {
console.log(`About to call API at ${url}`);
return new Promise((resolve, reject) => {
fetch(url, { method: 'GET' })
.then((res) => Promise.all([res.status, res.json()]))
.then(([status, jsonData]) => {
resolve([status, jsonData]);
})
.catch((error) => {
console.log('e', error);
reject(error);
});
});
};
module.exports = { getPeople };
config.js:
module.exports = {
getPeopledataUrl: 'http://localhost:3000/api/people',
};
get-people-handler.js:
const api = require('./api');
const config = require('./config');
const getPeopleHandler = async (req, res) => {
const url = config.getPeopledataUrl;
const response = await api.getPeople(url);
console.log(`API call status = ${response[0]}`);
if (response[0] === 200) {
res.locals.list = response[1];
res.render('people.html');
} else {
res.locals.error = response[0];
res.render('error.html');
}
};
module.exports = { getPeopleHandler };
get-people-handler.test.js:
const sinon = require('sinon');
const { getPeopleHandler } = require('./get-people-handler');
const myAPI = require('./api');
describe('handle error', () => {
let req;
let res;
let sandbox;
describe('getPeopleHandler() with error', () => {
before(() => {
req = {
session: {},
};
res = {
render: () => ({}),
};
sandbox = sinon.createSandbox();
});
beforeEach(() => {
sandbox.stub(myAPI, 'getPeople').returns([500, { errorCode: '500' }]);
});
afterEach(() => {
sandbox.restore();
});
it('should render the error page', async () => {
sandbox.stub(res, 'render').returns({});
res.locals = {};
await getPeopleHandler(req, res);
sinon.assert.calledWithExactly(res.render, 'error.html');
});
});
});
test result:
handle error
getPeopleHandler() with error
API call status = 500
✓ should render the error page
1 passing (7ms)
-----------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------------------|---------|----------|---------|---------|-------------------
All files | 60.87 | 50 | 16.67 | 60.87 |
api.js | 30 | 100 | 0 | 30 | 4-13
config.js | 100 | 100 | 100 | 100 |
get-people-handler.js | 83.33 | 50 | 100 | 83.33 | 9-10
-----------------------|---------|----------|---------|---------|-------------------
I am using fastify framework for my node.js application and sequelize as an ORM. I am using mocha, chai, and Sinon for unit testing. I have to unit test my controller function. The following is the sample controller function.
// controllers.js;
const service = require('../services');
exports.create = (req, reply) => {
const attributes = req.body;
service.create(attributes)
.then((result) => {
reply.code(201).send(result);
})
.catch((error) => {
reply.send(error);
});
};
and my services file is as follows,
// services.js;
const { Model } = require('../models');
function create(attributes) {
return Model.create(attributes);
}
module.exports = { create };
In the above code, I want to unit test only the 'create' function in controllers.js. The problem is, it should not call the database, since it is unit testing. But the Model.create in service.js file will make a call to the database. How can I unit test controller function only?
You should stub service.create method and create mocked req, reply objects.
E.g.
controller.js:
const service = require('./service');
exports.create = (req, reply) => {
const attributes = req.body;
service
.create(attributes)
.then((result) => {
reply.code(201).send(result);
})
.catch((error) => {
reply.send(error);
});
};
service.js:
const { Model } = require('./models');
function create(attributes) {
return Model.create(attributes);
}
module.exports = { create };
models.js:
const Model = {
create() {
console.log('real implementation');
},
};
module.exports = { Model };
controller.test.js:
const controller = require('./controller');
const service = require('./service');
const sinon = require('sinon');
const flushPromises = () => new Promise(setImmediate);
describe('62536251', () => {
afterEach(() => {
sinon.restore();
});
it('should create', async () => {
const mResult = 'success';
sinon.stub(service, 'create').resolves(mResult);
const mReq = { body: {} };
const mReply = { code: sinon.stub().returnsThis(), send: sinon.stub() };
controller.create(mReq, mReply);
await flushPromises();
sinon.assert.calledWith(mReply.code, 201);
sinon.assert.calledWith(mReply.send, 'success');
});
it('should handle error', async () => {
const mError = new Error('network');
sinon.stub(service, 'create').rejects(mError);
const mReq = { body: {} };
const mReply = { code: sinon.stub().returnsThis(), send: sinon.stub() };
controller.create(mReq, mReply);
await flushPromises();
sinon.assert.calledWith(mReply.send, mError);
});
});
unit test result with coverage report:
62536251
✓ should create
✓ should handle error
2 passing (13ms)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 83.33 | 100 | 60 | 83.33 |
controller.js | 100 | 100 | 100 | 100 |
models.js | 66.67 | 100 | 0 | 66.67 | 3
service.js | 66.67 | 100 | 0 | 66.67 | 4
---------------|---------|----------|---------|---------|-------------------
I'm a beginner with JS tests and I'm having issues when I try to mockup the value of a constant in the file I need to test.
I have the following file
// index.js
const { MultiAccounts } = require('../../some.js')
const MultiAccountInstance = new MultiAccounts();
...
const syncEvents = () => Promise.try(() => {
// ...
return MultiAccountInstance.all()
.then((accounts) => // ...); // ==> it throws the exception here Cannot read property 'then' of undefined
});
module.exports = syncEvents;
So, I will like to mockup the MultiAccountInstance constant.
I had been trying using Simon and rewire, but with the following script I'm having it throws the exception here Cannot read property 'then' of undefined exception in the script above.
//index.test.js
const rewire = require('rewire');
const indexRewired = rewire('.../../index/js');
describe('testing sync events', () => {
let fakeMultiAccountInstance, MultiAccountInstanceReverter;
let accounts;
beforeEach(() => {
accounts = [{id: 1}, {id: 2}];
fakeMultiAccountInstance = {};
fakeMultiAccountInstance.all = () => Promise.resolve(accounts);
MultiAccountInstanceReverter = indexRewired.__set__('MultiAccountInstance', fakeMultiAccountInstance);
});
afterEach(() => {
MultiAccountInstanceReverter();
});
it('testing', ()=> {
const spy = sinon.stub(fakeMultiAccountInstance, 'all');
return indexRewired().then((resp) => {
spy.restore();
expect(spy).to.have.been.calledWith({someParams: true});
});
})
});
How can I achieve this?. I also tried using stubs, but I'm having the error that the MultiAccountInstance.all is not a function
it's something like this
//index.test.js
const rewire = require('rewire');
const indexRewired = rewire('.../../index/js');
describe('testing sync events', () => {
let stubMultiAccountInstance, MultiAccountInstanceReverter;
let accounts;
beforeEach(() => {
accounts = [{id: 1}, {id: 2}];
stubMultiAccountInstance= sinon.stub().returns({
all: () => Promise.resolve(accounts), // also tried with sinon.stub().resolves(accounts)
});
MultiAccountInstanceReverter = indexRewired.__set__('MultiAccountInstance', stubMultiAccountInstance);
});
afterEach(() => {
stubMultiAccountInstance.reset();
MultiAccountInstanceReverter();
});
it('testing', ()=> {
return indexRewired().then((resp) => {
expect(stubMultiAccountInstance).to.have.been.calledWith({someParams: true});
});
})
});
Do you know what am I doing wrong?
Here is the unit test solution:
index.js:
const { MultiAccounts } = require('./some.js');
const Promise = require('bluebird');
let MultiAccountInstance = new MultiAccounts();
const syncEvents = () =>
Promise.try(() => {
return MultiAccountInstance.all().then((accounts) => console.log(accounts));
});
module.exports = syncEvents;
some.js:
function MultiAccounts() {
async function all() {}
return {
all,
};
}
module.exports = { MultiAccounts };
index.test.js:
const sinon = require('sinon');
const rewire = require('rewire');
const Promise = require('bluebird');
describe('61659908', () => {
afterEach(() => {
sinon.restore();
});
it('should pass', async () => {
const promiseTrySpy = sinon.spy(Promise, 'try');
const logSpy = sinon.spy(console, 'log');
const indexRewired = rewire('./');
const accounts = [{ id: 1 }, { id: 2 }];
const fakeMultiAccountInstance = {
all: sinon.stub().resolves(accounts),
};
indexRewired.__set__('MultiAccountInstance', fakeMultiAccountInstance);
await indexRewired();
sinon.assert.calledOnce(fakeMultiAccountInstance.all);
sinon.assert.calledWith(logSpy, [{ id: 1 }, { id: 2 }]);
sinon.assert.calledOnce(promiseTrySpy);
});
});
unit test results with coverage report:
61659908
[ { id: 1 }, { id: 2 } ]
✓ should pass (54ms)
1 passing (62ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 80 | 100 |
index.js | 100 | 100 | 100 | 100 |
some.js | 100 | 100 | 50 | 100 |
----------|---------|----------|---------|---------|-------------------
I have run into an issue that files and functions cannot be mocked, that is used in a handler of an API call. This call is simulated using superagent.
Here is the code of the test
// users.itest.js
const request = require('superagent');
const get = async url => request
.get(`${process.env.API_URL}${url}`);
describe('endpoint', () => {
it('GET', async () => {
jest.mock('../token-store', () => ({
getToken: jest.fn().mockReturnValue('token'),
}));
const { status, body } = await get('/api/users');
expect(status).toEqual(200);
expect(body).toHaveValidSchema(userSchema);
});
And here is the handler that is called by the '/api/users' endpoint
const someHandler = async (req, res) => {
const token = await tokenStore.getToken();
res.send(token);
};
I tried mocking it like shown, however, I couldn't find a solution.
Thanks.
You should use jest.mock() in the module scope, not function scope.
Here is the integration test solution:
app.js:
const express = require('express');
const tokenStore = require('./token-store');
const app = express();
const someHandler = async (req, res) => {
const token = await tokenStore.getToken();
res.send(token);
};
app.get('/api/users', someHandler);
module.exports = app;
token-store.js:
async function getToken() {
return 'real token';
}
module.exports = {
getToken,
};
users.test.js:
const request = require('superagent');
const app = require('./app');
const port = 3000;
process.env.API_URL = `http://localhost:${port}`;
const get = async (url) => request.get(`${process.env.API_URL}${url}`);
jest.mock('./token-store', () => ({
getToken: jest.fn().mockReturnValue('token'),
}));
describe('endpoint', () => {
let server;
beforeAll((done) => {
server = app.listen(port, () => {
console.info(`HTTP server is listening on http://localhost:${server.address().port}`);
done();
});
});
afterAll((done) => {
server.close(done);
});
it('GET', async () => {
const { status, text } = await get('/api/users');
expect(status).toEqual(200);
expect(text).toBe('token');
});
});
Integration test result with coverage report:
PASS src/stackoverflow/59426030/users.test.js (10.767s)
endpoint
✓ GET (74ms)
console.info src/stackoverflow/59426030/users.test.js:16
HTTP server is listening on http://localhost:3000
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
app.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 12.254s
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59426030
I need to test that my POST /login handler calling validServiceURL function inside itself.
I tried to solve this issue using spy from sinon, but didn't manage it.
POST /login handler:
const login = async (req, res) => {
try {
if (await validServiceURL(req.headers.host)) {
await checkUser(req, res);
} else {
res.redirect("/");
}
} catch (err) {
reditectToHomePageWithError(res, err.message);
}
};
Test code:
const chai = require("chai");
const sinon = require("sinon");
const request = require("supertest");
chai.use(require("sinon-chai"));
const expect = chai.expect;
const app = require("../../app");
const validServiceURL = require("../../lib/validServiceURL");
const sandbox = sinon.createSandbox();
describe("User", function() {
describe("POST login", function() {
it("should call validServiceURL with provided serviceURL", async () => {
let args = { username: user.username, password: password };
let serviceURL = "http://127.0.0.1:3030";
let spyValidServiceURL = sandbox.spy(validServiceURL);
await request(app)
.post("/login")
.type("form")
.send(args)
.set("host", serviceURL);
expect(spyValidServiceURL).to.have.been.calledWith(serviceURL);
sandbox.restore();
});
});
});
When I run this test I get error look like:
AssertionError: expected validServiceURL to have been called with arguments http://127.0.0.1:3030
sinon doesn't support stub/spy a standalone function directly. In your case, the standalone function is validServiceURL. For more info, see this issue.
Here is the solution using proxyquire module:
app.js:
const express = require("express");
const validServiceURL = require("./validServiceURL");
const app = express();
async function checkUser(req, res) {
console.log("checkUser");
}
function reditectToHomePageWithError(res, message) {
console.log(message);
}
const login = async (req, res) => {
try {
if (await validServiceURL(req.headers.host)) {
await checkUser(req, res);
} else {
res.redirect("/");
}
} catch (err) {
reditectToHomePageWithError(res, err.message);
}
};
app.post("/login", login);
module.exports = app;
validServiceURL.js:
async function validServiceURL() {}
module.exports = validServiceURL;
app.test.js:
const chai = require("chai");
const sinon = require("sinon");
const request = require("supertest");
const proxyquire = require("proxyquire");
chai.use(require("sinon-chai"));
const expect = chai.expect;
const sandbox = sinon.createSandbox();
describe("User", function() {
describe("POST login", function() {
it("should call validServiceURL with provided serviceURL", async () => {
let args = { username: "username", password: "password" };
let serviceURL = "http://127.0.0.1:3030";
let spyValidServiceURL = sandbox.spy();
const app = proxyquire("./app", {
"./validServiceURL": spyValidServiceURL,
});
await request(app)
.post("/login")
.type("form")
.send(args)
.set("host", serviceURL);
expect(spyValidServiceURL).to.have.been.calledWith(serviceURL);
sandbox.restore();
});
});
});
Integration test result with coverage:
User
POST login
✓ should call validServiceURL with provided serviceURL (468ms)
1 passing (474ms)
--------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files | 87.1 | 50 | 57.14 | 87.1 | |
app.js | 69.23 | 50 | 33.33 | 69.23 | 6,10,16,21 |
app.test.js | 100 | 100 | 100 | 100 | |
validServiceURL.js | 100 | 100 | 0 | 100 | |
--------------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/57908094