Questions tagged [chai]

Chai is a BDD/TDD assertion library for Node.js and browsers that can be used with any Javascript testing framework.

Chai is a / assertion library for and that can be used with any testing framework.

2953 questions
52
votes
7 answers

Test a rejection with Chai as promised

I want to test a function returning a promise. In this particular test, the promise is expected to be rejected with an Error object containing the classical message field (in this test, it is expected to equal "my error message") and a custom field…
Guid
  • 2,137
  • 2
  • 20
  • 33
50
votes
3 answers

How to get "should.be.false" syntax pass jslint?

I am writing JS UT for my NodeJS code. I am using Chai as the assertion library, and I prefer the should syntax. I also use jslint to check the JS file syntax, even the JS files for UT. Now I have a problem with jslint and Chai. In Chai, you can…
David S.
  • 10,578
  • 12
  • 62
  • 104
49
votes
1 answer

Reset "called" Count on Sinon Spy

How do I reset the "called" count on a Sinon spy before each test? Here's what I'm doing now: beforeEach(function() { this.spied = sinon.spy(Obj.prototype, 'spiedMethod'); }); afterEach(function() { Obj.prototype.spiedMethod.restore(); …
cantera
  • 24,479
  • 25
  • 95
  • 138
47
votes
5 answers

How to do an "or" in chai should

How do I do an or test with chai.should? e.g. something like total.should.equal(4).or.equal(5) or total.should.equal.any(4,5) What's the right syntax? I couldn't find anything in the documentation.
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
46
votes
2 answers

What is the difference between equal and eql in Chai Library

I have a question regarding Chai library for unit tests. I noticed a statement saying: equal: Asserts that the target is strictly (===) equal to the given value. eql: Asserts that the target is deeply equal to value. I'm confused about what the…
chanwcom
  • 4,420
  • 8
  • 37
  • 49
43
votes
6 answers

Trying ES6 style import gives 'Cannot use import statement outside a module'

I am trying to write a javascript test in intellij for which I need to import some dependancies and I want to use ES6 style import statements but getting error /usr/local/bin/node /workspace/rr-sample/node_modules/mocha/bin/_mocha --ui bdd…
driftwood
  • 2,051
  • 4
  • 21
  • 28
43
votes
16 answers

Is there a way to assert that a route has not been called in Cypress?

I am trying to assert that a route has not been called in Cypress. I thoroughly looked through the documentation and have found nothing. I am trying to do something like this: cy.get('@myRouteAlias').should('have.not.been.called'); I am currently…
Chris Bier
  • 14,183
  • 17
  • 67
  • 103
39
votes
2 answers

How can I test floating-point equality using chai?

We're using Chai's BDD API to write unit tests. How can we assert floating point equality? For example, if I try to make this assertion to check for a 66⅔% return value: expect(percentage).to.equal(2 / 3 * 100.0); I get this…
Mattie
  • 20,280
  • 7
  • 36
  • 54
37
votes
8 answers

Chai - Testing for values in array of objects

I am setting up my tests for the results to a REST endpoint that returns me an array of Mongo database objects. [{_id: 5, title: 'Blah', owner: 'Ted', description: 'something'...}, {_id: 70, title: 'GGG', owner: 'Ted', description:…
Jared Yach
  • 517
  • 1
  • 4
  • 10
36
votes
6 answers

Chai unittesting - expect(42).to.be.an('integer')

According to http://chaijs.com/api/bdd/#a, a/an can be used to check for the type of a variable. .a(type) @param{ String } type @param{ String } message _optional_ The a and an assertions are aliases that can be used either as language chains or…
soerface
  • 6,417
  • 6
  • 29
  • 50
35
votes
5 answers

How to unit test express Router routes

I'm new to Node and Express and I'm trying to unit test my routes/controllers. I've separated my routes from my controllers. How do I go about testing my routes? config/express.js var app = express(); // middleware, etc var router =…
cusejuice
  • 10,285
  • 26
  • 90
  • 145
34
votes
9 answers

Match partial objects in Chai assertions?

I am looking for the best way to match the following: expect([ { C1: 'xxx', C0: 'this causes it not to match.' } ]).to.deep.include.members([ { C1: 'xxx' } ]); The above doesn't work because C0 exists in the…
JayPrime2012
  • 2,672
  • 4
  • 28
  • 44
34
votes
3 answers

Mocha, Chai: Assert that Object is included in an Array of Objects

Chai has a nice way to assert if an Array includes a certain element expect([1,2,3]).to.include(2); What I would like is something similar, given an Array of Objects: expect([{a:1},{b:2}]).to.include({b:2}); Is this possible?
mck
  • 1,334
  • 2
  • 12
  • 20
33
votes
5 answers

Fail a test with Chai.js

In JUnit you can fail a test by doing: fail("Exception not thrown"); What's the best way to achieve the same using Chai.js?
Sionnach733
  • 4,686
  • 4
  • 36
  • 51
32
votes
7 answers

Protractor Check if Element Does Not Exist

I have a setting in my angular based website that turns a dropdown on and off. If it is off, then it does not show on the main page. With Protractor, I need to check to see if this element is not present when the switch is off. However, I should…
Sakamoto Kazuma
  • 2,573
  • 7
  • 34
  • 75