Skip to content
Advertisement

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

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

Advertisement