Mocked Jest callback method not called - node.js

I'm testing the NodeJS function/module below using jestjs
const fs = require("fs");
const path = require("path");
function read(filename, callback) {
fs.readFile(path.join(__dirname, filename), { encoding: "utf-8" }, callback);
}
module.exports = {
read,
};
The first test is:
test("callback data returned is correct", (done) => {
function callback(err, data) {
try {
expect(data).toBe("1");
done();
} catch (err) {
done(err);
}
}
read("./test", callback);
});
and is succesful (I have an actual real test file with the contents being 1.
The second one is a bit trickier. Since I'm providing a mockCallback function to read, why isn't the callback called?
const { read } = require("./callback");
describe("callback testing", () => {
test("callback is called", (done) => {
const mockCallback = jest.fn((err, data) => data);
read("./test", mockCallback);
expect(mockCallback).toHaveBeenCalled();
done();
});
});
The test fails and while inspecting the mock function it seems it wasn't called.
I also tried adding the following in order to mock fs.readFile, without much success:
const mock = require("mock-fs");
const fs = require("fs");
jest.mock("fs");
beforeEach(() => {
mock({
test: "1",
});
});
I'd like a solution that mocks the least of the dependant methods and an explanation as to why isn't the callback run.

You should mock fs.readFile method and trigger the callback in your test case.
E.g.
callback.js:
const fs = require('fs');
const path = require('path');
function read(filename, callback) {
fs.readFile(path.join(__dirname, filename), { encoding: 'utf-8' }, callback);
}
module.exports = {
read,
};
callback.test.js:
const { read } = require('./callback');
const fs = require('fs');
describe('callback testing', () => {
test('callback is called', () => {
const mockCallback = jest.fn();
const mockData = 'mock file data';
const mockReadFile = jest.spyOn(fs, 'readFile').mockImplementationOnce((filename, options, callback) => {
callback(null, mockData);
});
read('./test', mockCallback);
expect(mockCallback).toHaveBeenCalled();
expect(mockReadFile).toBeCalledWith(expect.any(String), { encoding: 'utf-8' }, mockCallback);
mockReadFile.mockRestore();
});
});
unit test result:
PASS examples/65361608/callback.test.js
callback testing
✓ callback is called (3 ms)
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
callback.js | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 4.992 s, estimated 5 s

Related

Jest Mock FS File Stream

I am very new to jest and mocking, I have a fs module read stream, which has 2 events on and data, i am trying to mock below code
ReadFile.js
const csv = require('csv-parser');
let storeData=[];
csvFileReader() {
fs.createReadStream(path.resolve(__dirname, "./abc.csv"))
.pipe(csv())
.on('data', async (row) => {
storeData[0] = row.postcode;
})
.on('end', () => {
console.log('Done')
});
}
ReadFileTest.js
import ReadFile from './readFile.js';
const fs = require('fs');
jest.mock('fs');
describe('Load File', () => {
const readFile= new ReadFile();
test('Test data handler', async () => {
const mockPipeOn = { on: jest.fn().mockImplementation(function(this, event, handler) {
if (event === 'data') {
jest.fn.mockReturnValueOnce("Reading Data")
}
if (event === 'end') {
jest.fn.mockReturnValueOnce("Completed Reading Data")
}
return this;
}), };
const mockReadStream = { pipe: jest.fn().mockReturnValueOnce(mockPipeOn) };
const createReadStream = jest.fn().mockReturnValueOnce(mockReadStream);
await readFile.csvFileReader();
});
});
I am getting error on 'this' key word its not going into 'data' and 'end' error handler
You should use mockFn.mockReturnThis() to return the context.
E.g.
ReadFile.js:
import fs from 'fs';
import path from 'path';
import csv from 'csv-parser';
class ReadFile {
csvFileReader() {
fs.createReadStream(path.resolve(__dirname, './abc.csv'))
.pipe(csv())
.on('data', async (row) => {
console.log('Storing Data');
})
.on('end', () => {
console.log('Done');
});
}
}
export default ReadFile;
ReadFile.test.js
import ReadFile from './ReadFile';
import fs from 'fs';
jest.mock('fs');
describe('67216891', () => {
const readFile = new ReadFile();
it('should store', () => {
const mReadStream = {
pipe: jest.fn().mockReturnThis(),
on: jest.fn().mockImplementation(function (event, handler) {
handler();
return this;
}),
};
fs.createReadStream.mockReturnValueOnce(mReadStream);
readFile.csvFileReader();
expect(fs.createReadStream).toBeCalledTimes(1);
expect(mReadStream.pipe).toBeCalledTimes(1);
expect(mReadStream.on).toBeCalledWith('data', expect.any(Function));
expect(mReadStream.on).toBeCalledWith('end', expect.any(Function));
});
});
test result:
PASS examples/67216891/ReadFile.test.js (8.891 s)
67216891
✓ should store (20 ms)
console.log
Storing Data
at ReadFile.<anonymous> (examples/67216891/ReadFile.js:10:17)
console.log
Done
at examples/67216891/ReadFile.js:13:17
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
ReadFile.js | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 10.159 s

How to mockup a constant defined in another file using sinon and rewire?

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 |
----------|---------|----------|---------|---------|-------------------

How to test SSM getParameter method in jasmine?

How do I test something like this?
const ssmParameterData = await ssm.getParameter(params, async (error, data) => {
if (error) throw error;
return data;
}).promise();
I have tried just mocking the method
spyOn(ssm, 'getParameter').and.returnValue(ssmParams);
and I get error like
TypeError: Cannot read property 'promise' of undefined
Here is the unit test solution:
index.js:
const AWS = require('aws-sdk');
const ssm = new AWS.SSM();
async function main(params) {
const ssmParameterData = await ssm
.getParameter(params, (error, data) => {
if (error) throw error;
return data;
})
.promise();
return ssmParameterData;
}
module.exports = { ssm, main };
index.test.js:
const { ssm, main } = require('./');
describe('60138152', () => {
it('should pass', async () => {
const data = 'fake data';
const getParameterRequestStub = { promise: jasmine.createSpy('promise') };
const getParameterStub = spyOn(ssm, 'getParameter').and.callFake((params, callback) => {
callback(null, data);
getParameterRequestStub.promise.and.resolveTo(data);
return getParameterRequestStub;
});
await expectAsync(main('test params')).toBeResolvedTo('fake data');
expect(getParameterStub).toHaveBeenCalledWith('test params', jasmine.any(Function));
expect(getParameterRequestStub.promise).toHaveBeenCalledTimes(1);
});
it('should throw error', async () => {
const mError = new Error('network');
const getParameterRequestStub = { promise: jasmine.createSpy('promise') };
const getParameterStub = spyOn(ssm, 'getParameter').and.callFake((params, callback) => {
callback(mError);
getParameterRequestStub.promise.and.rejectWith(mError);
return getParameterRequestStub;
});
await expectAsync(main('test params')).toBeRejectedWithError('network');
expect(getParameterStub).toHaveBeenCalledWith('test params', jasmine.any(Function));
});
});
unit test results with 100% coverage:
Executing 2 defined specs...
Running in random order... (seed: 87758)
Test Suites & Specs:
(node:93291) ExperimentalWarning: The fs.promises API is experimental
1. 60138152
✔ should pass (10ms)
✔ should throw error (1ms)
>> Done!
Summary:
👊 Passed
Suites: 1 of 1
Specs: 2 of 2
Expects: 5 (0 failures)
Finished in 0.034 seconds
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
source code: https://github.com/mrdulin/jasmine-examples/tree/master/src/stackoverflow/60138152

Mocking dynamodb scan using jest

For the source code(dynamodb.js):
const AWS = require("aws-sdk");
const Promise = require("bluebird");
const client = new AWS.DynamoDB.DocumentClient();
module.exports.db = (method, params) => {
console.log("access dynamodb ");
return Promise.fromCallback(cb => client[method](params, cb));
};
Using the test that looks like this(dont want't to mock Promise.fromCallback):
describe("test", () => {
const realAWS = require("aws-sdk");
let fakePromise;
let fakeDynamo;
let dbClient;
beforeAll(function() {
fakePromise = jest.fn();
fakeDynamo = {
get: (params, cb) => {
fakePromise(params, cb);
}
};
realAWS.DynamoDB.DocumentClient = jest.fn(() => fakeDynamo);
dbClient = require("../dynamodb");
});
test.only("Test successed", done => {
let result = dbClient.db("get", null);
console.log("access dynamodb ");
expect(fakePromise).toHaveBeenCalled();
});
});
but when tun the test ,the error was heppend:
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
if anyone can help me? Thanks!
You need to mock the implementation of the client.get method as a node callback style. So that Promise.fromCallback method will covert callback style to a promise.
Besides, you need to trigger the callback in your test case. That's why you got a timeout error.
E.g.
dynamodb.js:
const AWS = require('aws-sdk');
const Promise = require('bluebird');
const client = new AWS.DynamoDB.DocumentClient();
module.exports.db = (method, params) => {
console.log('access dynamodb');
return Promise.fromCallback((cb) => client[method](params, cb));
};
dynamodb.test.js:
const { db } = require('./dynamodb');
const AWS = require('aws-sdk');
jest.mock('aws-sdk', () => {
const mClient = { get: jest.fn() };
const mDynamoDB = {
DocumentClient: jest.fn(() => mClient),
};
return { DynamoDB: mDynamoDB };
});
const mockClient = new AWS.DynamoDB.DocumentClient();
describe('54360588', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', async () => {
mockClient.get.mockImplementationOnce((err, callback) => {
callback(null, 'fake data');
});
const actual = await db('get', null);
expect(actual).toBe('fake data');
expect(mockClient.get).toBeCalledWith(null, expect.any(Function));
});
});
unit test result with coverage report:
PASS src/stackoverflow/54360588/dynamodb.test.js (10.74s)
54360588
✓ should pass (28ms)
console.log src/stackoverflow/54360588/dynamodb.js:177
access dynamodb
-------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
dynamodb.js | 100 | 100 | 100 | 100 | |
-------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 12.636s

nodejs - sinon stub not being called

For some reason I am having a little trouble getting this simple test to run correctly with a similar set-up I have used multiple times before.
Perhaps a fresh pair of eyes could help me understand why my method generateReport is not being invoked and none of my stubs are being triggered with the intended arguments?
BYE nor GOOD are ever logged and the tests are returning the error: AssertError: expected stub to be called with arguments
My index file:
const errorHandler = require('./lib/handlers/error-handler')
const transformRequest = require('./lib/handlers/request-converter')
const convert = require('./lib/convert')
exports.generateReport = function generateReport(req, res) {
console.log('HELLO')
const objectToPopulateTemplate = transformRequest(req.body)
convert(objectToPopulateTemplate, function (e, data) {
if (e) {
console.log('BYE')
const error = errorHandler(e)
return res.send(error.httpCode).json(error)
}
console.log('GOOD')
res
.set('Content-Type', 'application/pdf')
.set('Content-Disposition', `attachment; filename=velocity_report_${new Date()}.pdf`)
.set('Content-Length', data.length)
.status(200)
.end(data)
})
}
My test file:
const proxyquire = require('proxyquire')
const assert = require('assert')
const sinon = require('sinon')
const fakeData = require('./data/sample-request.json')
describe('report-exporter', () => {
describe('generateReport', () => {
const fakeError = new Error('Undefined is not a function')
let res, resSendStub, resStatusStub, resEndStub, resSetStub, resJsonStub, req, convertStub, generate
before(() => {
resSendStub = sinon.stub()
resJsonStub = sinon.stub()
resStatusStub = sinon.stub()
resEndStub = sinon.stub()
resSetStub = sinon.stub()
convertStub = sinon.stub()
res = {
send: function(errorCode) {
return resSendStub(errorCode)
},
json: function(object) {
return resJsonStub(object)
},
set: function(arg1, arg2) {
return resSetStub(arg1, arg2)
},
status: function(code) {
return resStatusStub(code)
},
end: function(data) {
return resEndStub(data)
}
}
req = {
body: {}
}
generate = proxyquire('./../index', {
'./lib/convert': function() {
return convertStub
}
})
})
it('Should return an error response', () => {
convertStub.throws(fakeError)
generate.generateReport(req, res)
sinon.assert.calledWith(resSendStub, '500')
})
})
})
It looks like you are proxyquireing your ./lib/convert incorrectly. Original convert is called with objectToPopulateTemplate and a callback function (e, data). And that's the callback who is responsible for error handling and sending a response.
The stubbed convert function though doesn't care about about the provided callback at all, so the callback never gets called.
Most likely what you want is to pass the parameters to convertStub and deal with them later:
'./lib/convert': function(objectToPopulateTemplate, cb) {
return convertStub(objectToPopulateTemplate, cb);
}
and then
it('Should return an error response', () => {
generate.generateReport(req, res);
const cb = convertStub.getCall(0).args[1];
// simulate `convert` to fail
cb(fakeError);
sinon.assert.calledWith(resSendStub, '500')
})
Here is the unit test solution:
index.js:
const errorHandler = require("./error-handler");
const transformRequest = require("./request-converter");
const convert = require("./convert");
exports.generateReport = function generateReport(req, res) {
console.log("HELLO");
const objectToPopulateTemplate = transformRequest(req.body);
convert(objectToPopulateTemplate, function(e, data) {
if (e) {
console.log("BYE");
const error = errorHandler(e);
return res.send(error.httpCode).json(error);
}
console.log("GOOD");
res
.set("Content-Type", "application/pdf")
.set(
"Content-Disposition",
`attachment; filename=velocity_report_${new Date()}.pdf`
)
.set("Content-Length", data.length)
.status(200)
.end(data);
});
};
error-handler.js:
module.exports = function(error) {
return error;
};
request-converter.js:
module.exports = function transformRequest(body) {
return body;
};
convert.js:
module.exports = function convert(body, callback) {
callback();
};
index.spec.js:
const sinon = require("sinon");
const proxyquire = require("proxyquire");
describe("report-exporter", () => {
describe("generateReport", () => {
afterEach(() => {
sinon.restore();
});
const fakeError = new Error("Undefined is not a function");
fakeError.httpCode = 500;
it("Should return an error response", () => {
const logSpy = sinon.spy(console, "log");
const mReq = { body: {} };
const mRes = { send: sinon.stub().returnsThis(), json: sinon.stub() };
const convertStub = sinon.stub();
const errorHandlerStub = sinon.stub().returns(fakeError);
const transformRequestStub = sinon.stub().returns(mReq.body);
const generate = proxyquire("./", {
"./convert": convertStub,
"./error-handler": errorHandlerStub,
"./request-converter": transformRequestStub
});
generate.generateReport(mReq, mRes);
convertStub.yield(fakeError, null);
sinon.assert.calledWith(transformRequestStub);
sinon.assert.calledWith(convertStub, {}, sinon.match.func);
sinon.assert.calledWith(errorHandlerStub, fakeError);
sinon.assert.calledWith(logSpy.firstCall, "HELLO");
sinon.assert.calledWith(logSpy.secondCall, "BYE");
});
});
});
Unit test result with coverage report:
report-exporter
generateReport
HELLO
BYE
✓ Should return an error response (94ms)
1 passing (98ms)
----------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------------|----------|----------|----------|----------|-------------------|
All files | 87.5 | 50 | 62.5 | 87.5 | |
convert.js | 50 | 100 | 0 | 50 | 2 |
error-handler.js | 50 | 100 | 0 | 50 | 2 |
index.js | 84.62 | 50 | 100 | 84.62 | 14,15 |
index.spec.js | 100 | 100 | 100 | 100 | |
request-converter.js | 50 | 100 | 0 | 50 | 2 |
----------------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/47352972

Resources