Questions tagged [sinon-chai]

Sinon–Chai provides a set of custom assertions for using the Sinon.JS spy, stub, and mocking framework with the Chai assertion library. You get all the benefits of Chai with all the powerful tools of Sinon.JS.

Extends with assertions for the mocking framework.

Github page

268 questions
3
votes
1 answer

Sinon how to stub method for unit testing a Async function

I am trying to write a unit test for an async function using mocha and sinon.js Below is my test case describe('getOperations', function () { let customObj, store, someObj beforeEach(function () { someObj = { id:…
ashish2199
  • 393
  • 3
  • 13
3
votes
1 answer

How to test a helper functions used in controller in Sinon.js

I am using sinon.js to test my API. I would like to test the order from my helper functions that are being called. controller.js exports.controllerFunction = async (req, res) => { const function1Results = await function1(paramm); const…
Darron
  • 323
  • 4
  • 14
3
votes
0 answers

How to stub Mongoose methods with Sinon to test DAO?

Here is my Node unit test-case for a DAO method that returns a list of Category objects. Category is the name of my model. However, when I run this code, it is getting stuck. describe('findAllCategories', function () { it('should find all…
joler-botol
  • 442
  • 1
  • 7
  • 22
3
votes
1 answer

Sinon stubbing giving 'is not a function' error

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…
Jay
  • 2,656
  • 1
  • 16
  • 24
3
votes
1 answer

Stubbing Fetch Call - response.json will not invoke

I am trying to stub a fetch call with sinon and sinon-stub-promise. I'm pretty close...but I am not sure why it doesn't invoke the response.json() method I created. function toJSON(response) { console.log(response); return…
dman
  • 10,406
  • 18
  • 102
  • 201
3
votes
1 answer

Stubbing library constructor calls with sinon

Up until now I have been using sinon to stub out function calls on objects included in my nodeJS code. For example I use the request library, and so in my tests I can stub out http calls like: var request = require('request'); //Somewhere further…
mindparse
  • 6,115
  • 27
  • 90
  • 191
3
votes
1 answer

Mocking a method which is called using an arrow function as a parameter

How can I use the Sinon package to stub/mock a method call where one of the parameters I have to mock is called using an arrow function? eg let objWithMethod = { method : function(x) {}; }; function SUT() { // use case let x = 'some value'; …
Derek
  • 4,575
  • 3
  • 22
  • 36
3
votes
3 answers

How do I expect a function to be called with specific args with sinon, mocha, and chai in nodejs?

I have been having a problem trying to make sure Q.ninvoke is called with the args I am passing in. I am new to testing with Sinon, Mocha and Chai. I have been trying everything I have found online for 2 days now and I still cant get my test pass.…
Logan Fisher
  • 43
  • 1
  • 1
  • 4
3
votes
1 answer

How can I test an object with properties with random values?

I'm writting a unit test and I'm mocking an an object (client) which has a _request method which expects an object and a callback function. The object param has a couple of properties with random values: var clientMock = sandbox.mock(client); //…
roboli
  • 1,418
  • 20
  • 24
2
votes
0 answers

How can I test an endpoint that has downloadable content using sinon?

I have the following segment of code: it('Should return a csv document', function (done) { const findOneFake = sinon.fake.resolves(dummyIndicador); sinon.replace(Indicador, 'findOne', findOneFake); …
Miguel V
  • 606
  • 2
  • 6
  • 18
2
votes
0 answers

Chai problem mocking API response - Function is not stubbed

I am trying to test an API response. I don't know if the way that i am mocking req, res is the best way, but my code is: it('should failed when missing auth header', async () => { const req = {} as functions.https.Request; const res: any = { …
rafaelpolonio
  • 47
  • 1
  • 5
2
votes
1 answer

Using sinon how to avoid testing nested function?

I'm using mocha/chai/sino and I'm new with the three of them. const a = () => { b(); } const b = () => { console.log('here'); } In this example I just want to test that b is been called when calling a without executing b. Something…
Frank
  • 23
  • 4
2
votes
0 answers

Which Cypress chainer should I use to test an alias was called with matching arguments

I am using Cypress to do E2E tests. I am using the Sinon.Chai chainer be.calledWithMatch to test that an alias was called with matching arguments: cy.get('@myAlias') .should('be.calledWithMatch', { param: 'value', otherParam: 'other…
neiya
  • 2,657
  • 4
  • 23
  • 32
2
votes
4 answers

NodeJS: How to assert if event callback function was called using sinon

How can I test if a callback function from a event listener is called? For example, I have the following code where app.js initializes the application through the init.js controller. The main.js file has a class which extends and Event Emitter,…
saurjk
  • 973
  • 3
  • 13
  • 27
2
votes
1 answer

Promise.reject in .then() returning undefined

I've currently got an ES6 class with a constructor and two methods. I'm a tad confused as to why using Promise.reject(ex) within the .then() is resolving undefined. If someone wouldn't mind explaining what I'm doing wrong that would be much…
Matt Kent
  • 1,145
  • 1
  • 11
  • 26
1 2
3
17 18