Skip to content

Tag: sinon

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): […]

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 to mock a date object

Here is my code: var startTime = new Date(startDateTime).toLocaleTimeString(“en-US”, options); I need to get that startTime in my unit test. From knowing unit testing in a java perspective, I would …

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