I'm trying to mock the crypto module of nodejs with the jestjs framework.
Here is the piece of code that I want to mock:
app.js:
const hash = crypto
.createHmac('sha256', API_TOKEN)
.update(JSON.stringify(body))
.digest('hex');
if(hash === signature) {
//verified successfully. Implement next logic
}
Here, I want the .digest function to give the value contained in the signature variable.
Following is the mock code:
jest.mock('crypto', () => {
return {
createHmac: jest.fn(() => ({
update: jest.fn(),
digest: jest.fn(() => '123') //here '123' is placeholder for 'signature value
}))
};
});
However, when running the test, the main files throws an error like this:
TypeError: Cannot read property 'update' of undefined
What am I missing here for jest mock?
You should use mockFn.mockReturnThis() to obtain method chain call.
E.g.
app.js:
const crypto = require('crypto');
function main() {
const API_TOKEN = 'API_TOKEN';
const signature = '123';
const body = {};
const hash = crypto
.createHmac('sha256', API_TOKEN)
.update(JSON.stringify(body))
.digest('hex');
if (hash === signature) {
console.log('verified successfully. Implement next logic');
}
}
module.exports = main;
app.spec.js:
const main = require('./app');
const crypto = require('crypto');
jest.mock('crypto', () => {
return {
createHmac: jest.fn().mockReturnThis(),
update: jest.fn().mockReturnThis(),
digest: jest.fn(() => '123'),
};
});
describe('64386858', () => {
it('should pass', () => {
const logSpy = jest.spyOn(console, 'log');
main();
expect(crypto.createHmac).toBeCalledWith('sha256', 'API_TOKEN');
expect(crypto.update).toBeCalledWith(JSON.stringify({}));
expect(crypto.digest).toBeCalledWith('hex');
expect(logSpy).toBeCalledWith('verified successfully. Implement next logic');
logSpy.mockRestore();
});
});
unit test result:
PASS src/stackoverflow/64386858/app.spec.js
64386858
✓ should pass (17ms)
console.log node_modules/jest-environment-jsdom/node_modules/jest-mock/build/index.js:866
verified successfully. Implement next logic
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 50 | 100 | 100 | |
app.js | 100 | 50 | 100 | 100 | 13 |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.087s, estimated 11s
Related
I am new to jest, sorry if this is a trivial question but I went through the official jest docs and I am not able to find a solution to the problem.
I am developing a very simple app in nodejs that consumes data from a websocket and propagates it downstream to a set of consumers via zeromq.
The code is the following:
app.js:
const initializer = require("./dependencyInitializer");
const sock = initializer.zmqSock();
const ws = initializer.wsClient();
ws.on('update', data => {
sock.send([data.topic, JSON.stringify(data)]);
});
The websocket client is a class from a third party library extending from EventEmitter.
I would like to create a test asserting that the sock.send function is called exactly once inside the handler of the 'update' event.
This is my approach:
app.spec.js:
const ws = require("./app");
const initializer = require("./dependencyInitializer");
jest.mock("./dependencyInitializer", () => {
return {
wsClient: jest.fn(() => {
const EventEmitter = require("events")
const emitter = new EventEmitter()
return emitter;
}),
zmqSock: jest.fn(() => {
return {
send: jest.fn()
}
})
}
});
describe('on message received from websocket',() => {
it('should pass it to zmq', () => {
const data = {result: "ok"};
expect(initializer.wsClient).toHaveBeenCalledTimes(1);
expect(initializer.zmqSock).toHaveBeenCalledTimes(1);
const _sock = initializer.zmqSock();
const _ws = initializer.wsClient();
_ws.emit("update", data);
expect(_sock.send).toHaveBeenCalledTimes(1);
});
});
The test fails with the following:
on message received from websocket › should pass it to zmq
expect(jest.fn()).toHaveBeenCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
28 | const _ws = initializer.wsClient();
29 | _ws.emit("update", data);
> 30 | expect(_sock.send).toHaveBeenCalledTimes(1);
| ^
31 | });
32 | });
I am not sure if I am on the right path, I would like to understand what would be the best approach to develop a test like this.
Thanks
After mocking by jest.mock(), when the .wsClient() and .zmqSock() methods are called in the app.js file and app.spec.js file, the sock and ws objects in app.js are different with the app.spec.js.
{
wsClient: jest.fn(() => {
const EventEmitter = require("events")
const emitter = new EventEmitter()
return emitter;
})
}
Every time you call .wsClient(), it will create a new object.
Emitter can only listen for events from its own emit. The solution is to create the mock emitter and sock objects in the mock factory.
app.js:
const initializer = require('./dependencyInitializer');
const sock = initializer.zmqSock();
const ws = initializer.wsClient();
ws.on('update', (data) => {
sock.send([data.topic, JSON.stringify(data)]);
});
module.exports = { sock, ws };
app.test.js:
const app = require('./app');
const initializer = require('./dependencyInitializer');
jest.mock(
'./dependencyInitializer',
() => {
const EventEmitter = require('events');
const emitter = new EventEmitter();
const mSock = { send: jest.fn() };
return {
wsClient: jest.fn(() => emitter),
zmqSock: jest.fn(() => mSock),
};
},
{ virtual: true }
);
describe('on message received from websocket', () => {
it('should pass it to zmq', () => {
const data = { result: 'ok' };
expect(initializer.wsClient).toHaveBeenCalledTimes(1);
expect(initializer.zmqSock).toHaveBeenCalledTimes(1);
const _sock = initializer.zmqSock();
const _ws = initializer.wsClient();
// check if they have same reference
expect(app.sock).toBe(_sock);
expect(app.ws).toBe(_ws);
_ws.emit('update', data);
expect(_sock.send).toHaveBeenCalledTimes(1);
});
});
test result:
PASS examples/70024105/app.test.js (9.279 s)
on message received from websocket
✓ should pass it to zmq (2 ms)
----------|---------|----------|---------|---------|-------------------
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: 10.112 s
I am trying to stub a call to the aws parameter store (PS). But even though I added the stub in multiple ways it always makes the actual call to aws PS.
the method I am trying to test
function getParamsFromParamterStore() {
return ssm.getParametersByPath(query).promise();
}
One of the stub method I tried
var ssm = new AWS.SSM();
stub1 = sinon.stub(ssm, 'getParametersByPath').returns({promise: () => {}});
moduleName.__get__('getParamsFromParamterStore')();
But this actually makes the call to PS.
Note: since this is a private function (not exported) I am using rewire to access it.
Here is the unit test solution:
index.js:
const AWS = require('aws-sdk');
const ssm = new AWS.SSM();
function getParamsFromParamterStore(query) {
return ssm.getParametersByPath(query).promise();
}
index.test.js:
const rewire = require('rewire');
const sinon = require('sinon');
const { expect } = require('chai');
const mod = rewire('./');
describe('60447015', () => {
it('should pass', async () => {
const ssmMock = { getParametersByPath: sinon.stub().returnsThis(), promise: sinon.stub().resolves('mock data') };
const awsMock = {
SSM: ssmMock,
};
mod.__set__('ssm', awsMock.SSM);
const actual = await mod.__get__('getParamsFromParamterStore')('query');
expect(actual).to.be.eq('mock data');
sinon.assert.calledWithExactly(ssmMock.getParametersByPath, 'query');
sinon.assert.calledOnce(ssmMock.promise);
});
});
Unit test results with 100% coverage:
60447015
✓ should pass
1 passing (30ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
My unit test failing for following code.when I run test i see below result with unknown error.I am not getting how to test second parameter paylod in console.log method.
Test console log()
-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files | 66.67 | 100 | 0 | 66.67 | |
logger.js | 66.67 | 100 | 0 | 66.67 | 6 |
-----------|----------|----------|----------|----------|-------------------|
npm ERR! Test failed. See above for more details.
//logger.js
'use strict';
const log = (message, payload) => {
console.log(message, JSON.stringify(payload, null, 2));
};
module.exports = { log };
// log.test.js
'use strict';
const chai = require('chai');
const sinon = require('sinon');
chai.use(require('sinon-chai'));
const { expect } = chai;
const log = require('../../src/logger');
describe('Test console log()', () => {
it('should log value in console', () => {
this.stub = sinon.stub(console, 'log');
log('test invoked', { option: 1 });
});
});
Looks like you just need to destructure your require for logger.js:
'use strict';
const sinon = require('sinon');
const { log } = require('../../src/logger'); // <= destructuring assignment
describe('Test console log()', () => {
it('should log value in console', () => {
const stub = sinon.stub(console, 'log');
log('test invoked', { option: 1 });
stub.restore();
sinon.assert.calledWith(stub, 'test invoked', JSON.stringify({ option: 1 }, null, 2)); // Success!
});
});
I am trying to unit test the below listEntities function by mocking runQuery and createQuery functions. Maybe I should just give up and do an integration test with an emulator. Anyway, here is my code
Implementation:
const Datastore = require('#google-cloud/datastore');
const datastore = Datastore();
const runQueryDS = (query) => datastore.runQuery(query);
const createQueryDS = (kind) => datastore.createQuery(kind);
export const listEntities = (kind, runQuery = runQueryDS, createQuery = createQueryDS) => {
console.log('listEntities');
const query = createQuery(kind);
runQuery(query).then((results) => results[0]);
};
Test:
import { listEntities } from './datastore.api';
describe('datastore api', () => {
describe('listEntities', () => {
test('should return list of items', () => {
console.log('begin test');
const kind = 'TestRun';
const createdQuery = 'createdQuery';
const expectedResult = ['returnedFromQuery'];
const returnedFromExecutedQuery = [expectedResult];
const createQuery = jest.fn().mockImplementation(() => (createdQuery));
const runQuery = jest.fn().mockImplementation(() => (returnedFromExecutedQuery));
const result = listEntities(kind, runQuery, createQuery);
expect(result).toEqual(expectedResult);
});
});
});
This is the error I get
FAIL app/datastore.api.test.js
● Test suite failed to run
Cannot find module './datastore_client_config' from 'datastore_client.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
at Object.<anonymous> (node_modules/#google-cloud/datastore/src/v1/datastore_client.js:30:18)
Thank you!
Unit testing should be completed first, followed by integration testing. Unit tests are easier to write than integration tests, and have good isolation, do not rely on external services, have no side effects, and can run in different environments.
Here is the unit test solution:
index.js:
const Datastore = require('#google-cloud/datastore');
const datastore = Datastore();
const runQueryDS = (query) => datastore.runQuery(query);
const createQueryDS = (kind) => datastore.createQuery(kind);
const listEntities = (kind, runQuery = runQueryDS, createQuery = createQueryDS) => {
console.log('listEntities');
const query = createQuery(kind);
return runQuery(query).then((results) => results[0]);
};
module.exports = { listEntities };
index.test.js:
const { listEntities } = require('./');
const Datastore = require('#google-cloud/datastore');
jest.mock('#google-cloud/datastore', () => {
const mDatasotre = {
runQuery: jest.fn(),
createQuery: jest.fn(),
};
return jest.fn(() => mDatasotre);
});
describe('47128513', () => {
describe('#listEntities', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should list entities', async () => {
const mDatastore = Datastore();
mDatastore.createQuery.mockReturnValueOnce('fake query');
mDatastore.runQuery.mockResolvedValueOnce([{ id: 1 }]);
const actual = await listEntities('kind');
expect(actual).toEqual({ id: 1 });
expect(mDatastore.createQuery).toBeCalledWith('kind');
expect(mDatastore.runQuery).toBeCalledWith('fake query');
});
});
});
unit test result with coverage report:
PASS src/stackoverflow/47128513/index.test.js (12.111s)
47128513
#listEntities
✓ should list entities (12ms)
console.log src/stackoverflow/47128513/index.js:355
listEntities
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 13.865s, estimated 15s
source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/47128513
What would be the proper way to unit test this getall function using mocha/chai? I'm having a hard time understanding what to expect with Promise.all.
const Promise = require('bluebird');
const someApiService = require('./someapiservice');
const _ = require('underscore');
function getall(arr) {
let promises = _.map(arr, function(item) {
return someApiService(item.id);
});
return Promise.all(promises);
}
We should stub standalone function someApiService use link seams. This is the CommonJS version, so we will be using proxyquire to construct our seams. Additionally, I use sinon.js to create the stub for it.
E.g.
getall.js:
const Promise = require('bluebird');
const someApiService = require('./someapiservice');
const _ = require('underscore');
function getall(arr) {
let promises = _.map(arr, function (item) {
return someApiService(item.id);
});
return Promise.all(promises);
}
module.exports = getall;
someapiservice.js:
module.exports = function someApiService(id) {
return Promise.resolve('real data');
};
getall.test.js:
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const { expect } = require('chai');
describe('45337461', () => {
it('should pass', async () => {
const someapiserviceStub = sinon.stub().callsFake((id) => {
return Promise.resolve(`fake data with id: ${id}`);
});
const getall = proxyquire('./getall', {
'./someapiservice': someapiserviceStub,
});
const actual = await getall([{ id: 1 }, { id: 2 }, { id: 3 }]);
expect(actual).to.deep.equal(['fake data with id: 1', 'fake data with id: 2', 'fake data with id: 3']);
sinon.assert.calledThrice(someapiserviceStub);
});
});
unit test result:
45337461
✓ should pass (2745ms)
1 passing (3s)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 88.89 | 100 | 66.67 | 88.89 |
getall.js | 100 | 100 | 100 | 100 |
someapiservice.js | 50 | 100 | 0 | 50 | 2
-------------------|---------|----------|---------|---------|-------------------