I would like to capture the parameter passed to a stub method. And then I can perform assertion on the properties of that parameter. For Java, it is Mockito's ArgumentCaptor. Is there any equivalent thing in javascript test framework?
Here's an example:
const assert = require('chai').assert;
const expect = require('chai').expect;
const sinon = require('sinon');
const obj = {
divideByFive(a) {
return a / 5;
},
testFunc(a, b) {
return this.divideByFive(a + b) + 23;
}
};
describe('obj.testFunc()', () => {
afterEach(() => {
// Restore to the original implementation
obj.divideByFive.restore();
});
it('should call divideByFive() with the right arguments', () => {
var spy = sinon.spy(obj, 'divideByFive');
obj.testFunc(42, 1337);
assert(spy.calledWith(1379));
});
it('should return the value returned by divideByFive(), increased by 23', () => {
sinon.stub(obj, 'divideByFive').returns(1234);
expect(obj.testFunc(42, 1337)).to.equal(1257);
});
});
You can use .calledWith() (provided by Sinon) to check if a spy/stub was called with particular arguments. You should consult the documentation for more options.
Here's a standalone Mocha test to check if a spy got called with an object that had particular properties set to particular values:
const assert = require('chai').assert;
const sinon = require('sinon');
const spy = sinon.spy();
// Call the spy with an object argument.
spy({ foo : 'bar', xxx : 'yyy' });
// Check the properties.
it('should have called spy with foo:bar', function() {
assert( spy.calledWithMatch({ foo : 'bar' }) );
});
it('should have called spy with xxx:yyy', function() {
assert( spy.calledWithMatch({ xxx : 'yyy' }) );
});
it('should have called spy with xxx:zzz (WILL FAIL)', function() {
assert( spy.calledWithMatch({ xxx : 'zzz' }) );
});
Related
I am testing fileA.js, which requires fileB.js
In fileA.test.js, I wish to mock all methods from fileB.js except one.
In fileA.test.js I have:
const common = require("../src/fileB");
jest.mock("../src/fileB");
There is one method I do not wish to be mocked. Is this sort of thing possible in nodeJS?
Thank you.
You can use jest.mock and jest.requireActual(moduleName) to partial mock the methods/functions of a module.
For example:
a.js:
const b = require('./b');
exports.main = function main() {
console.log(b.method1());
console.log(b.method2());
console.log(b.method3());
};
b.js:
module.exports = {
method1: function() {
return 'method 1';
},
method2: function() {
return 'method 2';
},
method3: function() {
return 'method 3';
}
};
Now, we will mock all methods of b.js except method3.
a.spec.js:
jest.mock('./b', () => {
const originalB = jest.requireActual('./b');
const partialMockedB = Object.keys(originalB).reduce((pre, methodName) => {
pre[methodName] = jest.fn();
return pre;
}, {});
return {
...partialMockedB,
method3: originalB.method3 // mock all methods of b except method3
};
});
const { main } = require('./a');
const b = require('./b');
describe('main', () => {
test('should correct', () => {
const logSpy = jest.spyOn(console, 'log');
b.method1.mockReturnValueOnce('mocked method 1');
b.method2.mockReturnValueOnce('mocked method 2');
main();
expect(logSpy.mock.calls[0]).toEqual(['mocked method 1']);
expect(logSpy.mock.calls[1]).toEqual(['mocked method 2']);
expect(logSpy.mock.calls[2]).toEqual(['method 3']);
});
});
Unit test result:
PASS src/stackoverflow/58561765/a.spec.js
main
✓ should correct (18ms)
console.log node_modules/jest-mock/build/index.js:860
mocked method 1
console.log node_modules/jest-mock/build/index.js:860
mocked method 2
console.log node_modules/jest-mock/build/index.js:860
method 3
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.65s
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58561765
first time really using sinon and I am having some issues with the mocking library.
All I am trying to do is stub/mock out a function from a dao class called myMethod. Unfortunatly, I am getting the error: myMethod is not a function, which makes me believe I am either putting the await/async keywords in the wrong spots of the test or I don't understand sinon stubbing 100%. Here is the code:
// index.js
async function doWork(sqlDao, task, from, to) {
...
results = await sqlDao.myMethod(from, to);
...
}
module.exports = {
_doWork: doWork,
TASK_NAME: TASK_NAME
};
// index.test.js
const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");
const { _doWork, TASK_NAME } = require("./index.js");
const SqlDao = require("./sqlDao.js");
.
.
.
it("given access_request task then return valid results", async () => {
const sqlDao = new SqlDao(1, 2, 3, 4);
const stub = sinon
.stub(sqlDao, "myMethod")
.withArgs(sinon.match.any, sinon.match.any)
.resolves([{ x: 1 }, { x: 2 }]);
const result = await _doWork(stub, TASK_NAME, new Date(), new Date());
console.log(result);
});
With error:
1) doWork
given task_name task then return valid results:
TypeError: sqlDao.myMethod is not a function
Your issue is that you're passing stub to _doWork instead of passing sqlDao.
A stub isn't the object that you just stubbed. It is still a sinon object that you use to define the behaviour of the stubbed method. When you're done with your tests, you use stub to restore stubbed object.
const theAnswer = {
give: () => 42
};
const stub = sinon.stub(theAnswer, 'give').returns('forty two');
// stubbed
console.log(theAnswer.give());
// restored
stub.restore();
console.log(theAnswer.give());
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/7.2.4/sinon.min.js"></script>
I have an issue with a unit test of a function which calls a class. It seems that it always calls the "official" class instance and not my mocked class. I'm not able to force my function to use my mocked instance...
There is a file with the function I want to test:
const myClass = require('./myClass');
const instance = new myClass();
module.exports.functionToTest = async function () {
// Some stuff...
const value = await instance.myMethod();
// Some stuff that define a result variable (partially with value).
return result;
}
There is a file with my class definition:
module.exports = class myClass {
async myMethod() {
const result = await someStuffWillResolveMaybeTrueOrFalse();
console.log('We used the original myMethod... Mocking has failed.');
return result;
}
}
There is a spec file:
const myFile = require('./myFile');
const myClass = require('./myClass');
describe('My test', async () => {
it('should mock myClass.myMethod in order to return false', () => {
const instance = new myClass();
instance.myMethod = jest.fn().mockResolvedValue(false);
const result = await myFile.functionToTest();
expect(result).toBeTruthy();
}
}
Unfortunately my test is passing (because myMethod return "true") and log "We used the original myMethod... Mocking has failed."
So I want to make my test always fail by mocking that myMethod to return false.
Can you help me? Thanks for your time.
Hm. I've found a solution.
See. A change in my file with the target function.
const myClass = require('./myClass');
// const instance = new myClass(); <== Not here...
module.exports.functionToTest = async function () {
const instance = new myClass(); // <== ...but there.
// Some stuff...
const value = await instance.myMethod();
// Some stuff that define a result variable (partially with value).
return result;
}
And my spec file :
const myFile = require('./myFile');
// I specify to Jest that I'll mock a file
jest.mock('./myClass');
const myClass = require('./myClass');
// I prepare the mock function. In that case a promise wich resolve 'false'
const mMock = jest.fn().mockResolvedValue(false);
// I mock the method 'myMethod' in 'myClass'
myClass.mockImplementation(() => {
return {
myMethod: mMock
};
});
// Then, I just take the test
describe('My test', async () => {
it('should mock myClass.myMethod in order to return false', () => {
const result = await myFile.functionToTest();
expect(result).toBeFalsy();
}
}
I'm trying to use sinon stub to replace a function that might take along time. But when I run the tests, the test code doesn't seem to be using the sinon stubs.
Here is the code I'm trying to test.
function takeTooLong() {
return returnSomething();
}
function returnSomething() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('ok')
}, 1500)
})
}
module.exports = {
takeTooLong,
returnSomething
}
and this is the test code.
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'))
const expect = chai.expect
chai.should()
const db = require('./database')
const sinon = require('sinon')
require('sinon-as-promised')
describe('Mock the DB connection', function () {
it('should use stubs for db connection for takeTooLong', function (done) {
const stubbed = sinon.stub(db, 'returnSomething').returns(new Promise((res) => res('kk')));
const result = db.takeTooLong()
result.then((res) => {
expect(res).to.equal('kk')
sinon.assert.calledOnce(stubbed);
stubbed.restore()
done()
}).catch((err) => done(err))
})
I get an assertion error
AssertionError: expected 'ok' to equal 'kk'
+ expected - actual
-ok
+kk
What am I doing wrong? Why isn't the stub being used ? The test framework in Mocha.
Sinon stubs the property of the object, not the function itself.
In your case you are exporting that function within an object.
module.exports = {
takeTooLong,
returnSomething
}
So in order to properly call the function from the object, you need to replace your function call with the reference to the export object like :
function takeTooLong() {
return module.exports.returnSomething();
}
Of course based on your code, you can always refactor it :
var exports = module.exports = {
takeTooLong: function() { return exports.returnSomething() }
returnSomething: function() { /* .. */ }
}
You might want to have a look at Proxyquire to stub/spy directly exported functions.
https://www.npmjs.com/package/proxyquire/
I'm trying to test if a specific method is called given certain conditions using mocha, chai and sinon. Here is the code:
function foo(in, opt) {
if(opt) { bar(); }
else { foobar(); }
}
function bar() {...}
function foobar() {...}
module.exports = {
foo: foo,
bar: bar,
foobar:foobar
};
Here is the code in my test file:
var x = require('./foo'),
sinon = require('sinon'),
chai = require('chai'),
expect = chai.expect,
should = chai.should(),
assert = require('assert');
describe('test 1', function () {
it('should call bar', function () {
var spy = sinon. spy(x.bar);
x.foo('bla', true);
spy.called.should.be.true;
});
});
When I do a console.log on the spy it says it wasn't called even thou with manual logging in the bar method I'm able to see it gets called. Any suggestions on what I might be doing wrong or how to go about it?
Thanks
You've created a spy, but the test code doesn't use it. Replace the original x.bar with your spy (don't forget to do cleanup!)
describe('test 1', function () {
before(() => {
let spy = sinon.spy(x.bar);
x.originalBar = x.bar; // save the original so that we can restore it later.
x.bar = spy; // this is where the magic happens!
});
it('should call bar', function () {
x.foo('bla', true);
x.bar.called.should.be.true; // x.bar is the spy!
});
after(() => {
x.bar = x.originalBar; // clean up!
});
});