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
31
votes
1 answer

Sinon.JS - How can I get arguments from a stub?

I am trying to use Sinon to test a JS component which looks a bit like this... import Bootbox from "../helpers/bootbox"; import Guard from "../helpers/guard"; import UrlHelper from "../helpers/url-helper"; export default class DeleteButton { …
Keith Jackson
  • 3,078
  • 4
  • 38
  • 66
21
votes
1 answer

Invalid Chai property when calling calledOnce

I'm having problems writing tests in JavaScript with Sinon and Chai. I'm trying to check if a function is called on a spy and get I get "Error: Invalid Chai property: calledOnce" I'm doing the same thing in another project with the same test…
user1716970
  • 743
  • 1
  • 8
  • 19
19
votes
2 answers

Has been called with object assertion

Function I'm spying on, receives object as an argument. I need to assert that the function been called with certain properties of the object. e.g: my SUT has: function kaboom() { fn({ foo: 'foo', bar: 'bar', zap:…
iLemming
  • 34,477
  • 60
  • 195
  • 309
13
votes
3 answers

Sinon-chai calledWith(new Error()) and with exact message

I need to test this function : //user.js function getUser(req, res, next){ helper.get_user(param1, param2, (err, file) => { if (err) return next(err); } This is my test function : it ("failed - helper.get_user throws error",…
masterG
  • 168
  • 3
  • 10
12
votes
2 answers

How to mock an axios request using sinon modules

There seems to be so many different ways to do this, but I am trying to use just sinon, sinon-test, chai/mocha, axios, httpmock modules. I am not able to successfully mock a GET call made using axios. I want to be able to mock the response from that…
rick200
  • 125
  • 1
  • 2
  • 7
9
votes
1 answer

wait for sinon stubbed promise to resolve before making assertion on sinon spy

I have a middleware function which checks a session token to see if the user is an admin user. The function does not return anything if all checks pass but simply calls next(). How can I wait for the internal asynchronous Promise (adminPromise) to…
8
votes
1 answer

How to mock npm module with sinon/mocha

I'm trying to test a function that calls the module cors. I want to test that cors would be called. For that, I'd have to stub/mock it. Here is the function cors.js const cors = require("cors"); const setCors = () => cors({origin:…
Ndx
  • 517
  • 2
  • 12
  • 29
8
votes
1 answer

Testing Image onload using jsdom, sinon, mocha and chai

Can anyone help me with testing of following function function onload(cb){ const image = 'http://placehold.it/350x150' const img = new Image() img.src = image img.onload = () => { cb() } } In my test file Image is available via jsdom.…
ritz078
  • 2,193
  • 3
  • 22
  • 24
7
votes
1 answer

nodejs: How to stub AWS s3 getobject with sinon

I am trying to write unit tests but am having trouble stubbing the AWS S3 getobject method. Here is my code: describe('testExecuteSuccess()', function () { it('Test execution with id is successful.', async () => { let id = "test_id"; …
deeformvp
  • 157
  • 1
  • 2
  • 11
7
votes
1 answer

Sinon Stub not working for exported function

Sinon doesn't seem to be stubbing a method from an imported file. Is it to do with exporting consts? I see "Received ORIGINAL MESSAGE" in the console.log. Main.js import * as otherActions from 'filters/actions/Other.actions'; describe('filter…
Sebastian Patten
  • 7,157
  • 4
  • 45
  • 51
7
votes
1 answer

How to test axios requests parameters with sinon / chai

I am trying to test the parameters of an axios call using sinon / chai / mocha, to confirm the existence of certain parameters (and ideally that they are valid dates with moment). Example code (in class myclass) fetch() { axios.get('/test', {…
SmurfTheWeb
  • 93
  • 1
  • 11
6
votes
1 answer

Sinon js AssertError: expected stub to be called once but was called 0 times

I have been learning Sinon JS for unit testing, and I am trying to get this example code working. I have a simple "external" library created: class MyLib { simpleMethod () { return 'some response'; } static handler() { const…
Zach Gollwitzer
  • 2,114
  • 2
  • 16
  • 26
6
votes
2 answers

How to unit test localStorage using sinon

I am trying to test localStorage using sinon. Basically I am very new to unit testing so this might be very basic. Update I managed to come up with this but now its giving me a new error Should wrap property of object Test describe('Initial State',…
Umair Sarfraz
  • 5,284
  • 4
  • 22
  • 38
5
votes
1 answer

Sinon stubbing helper method defined in same file

So I have a file, user-database, that looks something like this : export function foo(id: number): Promise { return new Promise((resolve, reject) => { findSomething(id) .then((data) => { //do something with data …
TovrikTheThird
  • 471
  • 2
  • 7
  • 20
5
votes
3 answers

how to use sinon js with Express js unit testing

Hi i want to do unit testing on my express js code i want to mock data so after searching multiple websites and blogs i found this library but i am not clear how can i use this library for mocking or data. My testing code is var request =…
Arpit Kumar
  • 2,179
  • 5
  • 28
  • 53
1
2 3
17 18