I am unit testing a method defined in a module, which itself calls a dependency defined in a second module. I want to set a spy for the method-under-test’s call to the dependency, using the Sinon package. How do I do that? I’ve seen Sinon’s page on mocking the dependency of a module (see here), and have been able to
Tag: sinon
Compare arrays of Errors in Chai
I have a validator method that returns an array with Errors. I want to create a unit test that compares this errors, but I can’t use expect(fn).to.throw since I don’t throw the errors, just return them. This is my approach but I get AssertionError: expected [ Array(2) ] to have the same members as [ Array(2) ] Previously we used
Mocking methods of a JavaScript object created within a function
I’ve written a JavaScript function that creates an object from a require()’d library, and then uses it. That seems to be causing me trouble when I try to write tests for it because I don’t seem to have a good way to gain control over that object and create mocks of its methods to test the behavior of my function.
Why do these sinon stubs resolve to undefined?
I’ve written a unit test for the following code and stubbed the browser methods (read: web-extension API) using Sinon (more specifically: sinon-chrome, a dated but still functioning library for my use case). The unit test: The tests fail due to both the stubbed methods returning undefined, but the Sinon docs state quite clearly that stub.resolves(value): Causes the stub to return
Stubbing uuid with sinon
So I’m updating dependencies on my project and I’ve run into a snag… My unit tests were working perfectly with the below stub. However in the latest version of UUID, this seemingly has broken. Any suggestions on how to fix it? These are simplistic extracts from the code to illustrate the method I’m using to stub the functionality of uuid
How to return based on actual arguments being sent?
Let’s say we have a function with two arguments. It’s being called many times with different arguments on every call. So, its impossible to stub it with withArgs option. I want to write a stub function which will check the actual arguments being passed (args1, args2) and return a static response with a switch case. Something on the following lines:
Using sinon how do I stub or fake the property of a callback
Using sinon how do I stub/fake a return value for this.lastID in the db.run function. I can fake the callback using this code: But it will fail as this.lastID is undefined. How do I overcome this? Thanks! Answer You can use stub.callsArgOn() to pass in a context value that will used as this in the callback. You would stub it
Using sinon to mock a date object
Here is my code: I need to get that startTime in my unit test. From knowing unit testing in a java perspective, I would approach this problem as How can I mock the new date constructor? and then How can I mock the toLocaleTimeString function call?. However, I’m not sure that that’s the way to approach this problem in javascript.
How do I stub a chain of methods in Sinon?
I know how to use stub to replace one function. But now I have a line in my function I want to test that I need to stub that looks like this So there is a chain of function here and I’m unsure what I need to do. How do I stub “find” to return something that I can use