How to mock an inner function in Sinon?
How to mock an inner function in Sinon?
Suppose I have two functions, foo is called inside a bar. I have a Meteor app, so I decided to use meteor mocha package along with sinon and chai and not jest
foo
bar
mocha
sinon
chai
jest
// foo.js
const foo = () => // call to a google maps api;
export default foo;
// bar.js
const bar = (x) =>
foo();
...
;
export default bar;
What is the correct approach of mocking foo in this case
foo
Currently I've came up with the following solution:
import foo from 'path/to/foo.js'
import bar from 'path/to/bar.js'
describe('my test suite', function()
it('should pass the test', function()
foo = spy();
bar(5);
assert(foo.calledOnce);
);
);
The following code works, but is that correct to redefine foo?
foo
UPDATE
Also, it's not possible to create a mock or stub this way, which makes me think that Sinon is not suitable for mocking standalone functions
1 Answer
1
Sinon works well on standalone JavaScript functions.
Sinon
Here is an example of how to wrap the default export of a module in a Sinon spy:
Sinon
import * as sinon from 'sinon';
import * as fooModule from 'path/to/foo.js'
import bar from 'path/to/bar.js'
describe('my test suite', function()
it('should pass the test', function()
const spy = sinon.spy(fooModule, 'default'); // wrap the function in a spy
bar(5);
assert(spy.calledOnce); // SUCCESS
spy.restore(); // restore the original function
);
);
Here is an example of how to replace the default export of a module with a Sinon stub:
Sinon
import * as sinon from 'sinon';
import * as fooModule from 'path/to/foo.js'
import bar from 'path/to/bar.js'
describe('my test suite', function()
it('should pass the test', function()
const stub = sinon.stub(fooModule, 'default').returns('something else'); // stub the function
bar(5); // foo() returns 'something else' within bar(5)
assert(stub.calledOnce); // SUCCESS
stub.restore(); // restore the original function
);
);
Does it pass if you change the line to
assert(stub.called);? (is the stub getting called more than once?)– brian-lives-outdoors
Sep 17 '18 at 12:43
assert(stub.called);
No, also returns false
– Ivan P
Sep 17 '18 at 13:32
Did you put the
spy and stub tests in the same file? If a function has already been wrapped in a spy/stub/mock/fake during a given test it can't be wrapped again until it has been restored. I updated my answer above to call restore to avoid that particular error.– brian-lives-outdoors
Sep 17 '18 at 17:17
spy
stub
restore
No, I'm using only 1 stub in the test. When logging the stub in the console I've noticed that it has
notCalled:true which is weird– Ivan P
Sep 17 '18 at 18:13
notCalled:true
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Thanks, stubbing this way works, however for some reason I get false when calling calledOnce on a stub
– Ivan P
Sep 17 '18 at 10:32