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
5
votes
0 answers

Stubbing a javascript method with a super call

I'm trying to test an ES6 class method that calls a class method in its parent. For example: Polygon.js class Polygon { verifyDimensions() { this.allSidesValid(); } } export default Polygon; Square.js import Polygon from…
stakolee
  • 893
  • 1
  • 7
  • 20
4
votes
1 answer

Cypress - how to properly detect for JS errors in a page

I am in the process of writing a spec file with N it(s), where each it will visit a specific page of the application and will return the number of errors/warning in the applications. I've posted also here:…
SimoneB
  • 158
  • 2
  • 12
4
votes
2 answers

How to mock or stub 'instanceof' in Chai | Sinon | Mocha

I have a condition where i need to mock or stub data. var array =['add','divide'] var add =listMehtod[0]; if(add instanceof Calculator){ // how to test this ? // some logic } basically i have to write some test case for…
jsduniya
  • 2,464
  • 7
  • 30
  • 45
4
votes
1 answer

sinon is not mocking import function

I have the following code. Sinon failed to mock doSomething() and printing actual string instead of 'hello' //file.js import { doSomething } from 'my-npm-package'; module.exports = () => doSomething(); This is the test file: //file.spec.js import…
aiiwa
  • 591
  • 7
  • 27
4
votes
2 answers

How to stub mongoose methods with multiple arguments in Sinon?

I am using Mongoose in Node.js and here is my DAO method. function findPostsByCategoryId(categoryId, first, second) { var sortingOrd = { 'createdAt': -1 }; return Post.find({ 'categoryId': categoryId…
joler-botol
  • 442
  • 1
  • 7
  • 22
4
votes
1 answer

Vue.JS Unit Testing - Original method is still being called after using sinon.stub()

I am using Sinon to stub API calls when unit testing my components (written with TypeScript and vue-class-component). After adding the stub to the unit test, the original method is still being called (not returning the stubbed value). it('should set…
cfly24
  • 1,882
  • 3
  • 22
  • 56
4
votes
1 answer

Stubbing pg-promise using sinon and mocha

Suppose I have a the following module, as database.js const initOptions = {} const pgp = require('pg-promise')(initOptions) const config = require('../../config') const db = pgp({ host: config.database.host, port: config.database.port, …
MadsRC
  • 197
  • 1
  • 12
4
votes
0 answers

Sinon calledWith and calledWithMatch fail for objects EDIT: Sinon spy on object constructor

I am new to unit testing and am using Mocha, Sinon and Chai to test NodeJs code. The problem is that my expectation for stub.calledWith() always fails when it is an object, even though the test error shows two objects which are syntactically…
JoeWemyss
  • 607
  • 1
  • 10
  • 28
4
votes
1 answer

Unit testing Cloud Functions for Firebase: what's the "right way" to test/mock `transaction`s with sinon.js

Man, this firebase unit testing is really kicking my butt. I've gone through the documentation and read through the examples that they provide, and have gotten some of my more basic Firebase functions unit tested, but I keep running into problems…
tim
  • 1,708
  • 14
  • 16
4
votes
2 answers

How to verify that a constructor was called using sinon

I need to assert whether a constructor was called using sinon. Below is how I can create a spy. let nodeStub: any; nodeStub = this.createStubInstance("node"); But how can I verify that this constructor was called with the relevant parameters? Below…
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
4
votes
1 answer

How to spy on a sub-function of a named export in ES6

I want to spy on a sub-function that is exported as a named export, but, it seems like we cannot spy on it. Let's say I have two functions called add and multiply in operations.js and export them as named exports: const add = (a, b) => { return a…
Supasate
  • 1,564
  • 1
  • 16
  • 22
4
votes
1 answer

Is it possible to check if a function arguments bound correctly using Sinon.JS

Let say we have a function that returns a function and bounds arguments to it: function A(x, y){ return function(x, y){...}.bind(this, x, y); } And now we want to know if function A binds arguments correctly: var resultedFunction = A(); var spy…
Arsey
  • 281
  • 3
  • 10
4
votes
1 answer

angular testing with sinon, mocha, chai

I want test my angular app with mocha,sinon and chai. Especially I interest in submit function. How to create mock or stub for LoginResoure to test this function. Thanks! (function () { 'use strict'; class LoginController { …
CrueLHamsteR
  • 99
  • 1
  • 5
3
votes
1 answer

Cypress: assert argument of stubbed function with Regex

I have a stubbed method that is having the following structure printed in the Cypress console: myMethod('start', Object{5}) I know that the object has a key, segmentB -> when console logging it in the stub, I see it but I do not want to start making…
Shushiro
  • 577
  • 1
  • 9
  • 32
3
votes
1 answer

How to unit test calculation in code in nodejs?

I have below code where I'm adding few parameters. Now I want to check whether this addition is getting performed correctly or not. Below is my code, "use strict"; const account = require('account'); async function main() { try { //calling an…
ABCD
  • 730
  • 1
  • 13
  • 31
1
2
3
17 18