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
336
votes
7 answers

Mocha / Chai expect.to.throw not catching thrown errors

I'm having issues getting Chai's expect.to.throw to work in a test for my node.js app. The test keeps failing on the thrown error, but If I wrap the test case in try and catch and assert on the caught error, it works. Does expect.to.throw not work…
doremi
  • 14,921
  • 30
  • 93
  • 148
277
votes
7 answers

chai test array equality doesn't work as expected

Why does the following fail? expect([0,0]).to.equal([0,0]); and what is the right way to test that?
kannix
  • 5,107
  • 6
  • 28
  • 47
237
votes
7 answers

In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded

In my node application I'm using mocha to test my code. While calling many asynchronous functions using mocha, I'm getting timeout error (Error: timeout of 2000ms exceeded.). How can I resolve this? var module = require('../lib/myModule'); var…
sachin
  • 13,605
  • 14
  • 42
  • 55
202
votes
3 answers

What is the difference between “assert”, “expect”, and “should” in Chai?

What is the difference between assert, expect, and should? When to use what? assert.equal(3, '3', '== coerces values to strings'); var foo = 'bar'; expect(foo).to.equal('bar'); foo.should.equal('bar');
Manu
  • 3,979
  • 7
  • 21
  • 25
172
votes
4 answers

How do I properly test promises with mocha and chai?

The following test is behaving oddly: it('Should return the exchange rates for btc_ltc', function(done) { var pair = 'btc_ltc'; shapeshift.getRate(pair) .then(function(data){ expect(data.pair).to.equal(pair); …
chovy
  • 72,281
  • 52
  • 227
  • 295
169
votes
8 answers

Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai

So, I'm testing a component that relies on an event-emitter. To do so I came up with a solution using Promises with Mocha+Chai: it('should transition with the correct event', (done) => { const cFSM = new CharacterFSM({}, emitter, transitions); …
Jzop
  • 1,735
  • 2
  • 11
  • 8
135
votes
3 answers

When should you use render and shallow in Enzyme / React tests?

prior to posting this question, I tried to search in sqa stackexchange but I found no post about shallow and render there, so I hope someone can help me out here. When should I use shallow and render in testing react components? Based on the airbnb…
Cyval
  • 2,429
  • 4
  • 16
  • 27
135
votes
10 answers

Mocha API Testing: getting 'TypeError: app.address is not a function'

My Issue I've coded a very simple CRUD API and I've started recently coding also some tests using chai and chai-http but I'm having an issue when running my tests with $ mocha. When I run the tests I get the following error on the shell: TypeError:…
charliebrownie
  • 5,777
  • 10
  • 35
  • 55
108
votes
3 answers

Testing for errors thrown in Mocha

I'm hoping to find some help with this problem. I'm trying to write tests for an application I am writing. I have distilled the problem in to the following sample code. I want to test that an error was thrown. I'm using Testacular as a test…
Chris Neitzer
  • 1,183
  • 2
  • 7
  • 7
105
votes
9 answers

Chai: how to test for undefined with 'should' syntax

Building on this tutorial testing an angularjs app with chai, I want to add a test for an undefined value using the "should" style. This fails: it ('cannot play outside the board', function() { scope.play(10).should.be.undefined; }); with error…
thebenedict
  • 2,539
  • 3
  • 20
  • 29
90
votes
6 answers

Nice way to get rid of no-unused-expressions linter error with chai

In my Chai tests I often find myself wanting to use their assertions that are something like .to.be.empty, .to.be.true e.t.c., because I find them to be cleaner to read than .to.be.length(1) or .to.be.equal(true). However, this breaks my linter (I'm…
Ben Hare
  • 4,365
  • 5
  • 27
  • 44
84
votes
1 answer

Mocha not exiting after test

I'm starting with tests in Node. Using mocha, chai and nock (to intercept external HTTP api calls). I have written 3 tests, all of them are a pass, however, when I added the 3rd test, mocha stopped exiting after running the tests, with no error or…
Fede E.
  • 2,118
  • 4
  • 23
  • 39
82
votes
3 answers

How to unit test a method of react component?

I am trying to unit test my reactjs component: import React from 'react'; import Modal from 'react-modal'; import store from '../../../store' import lodash from 'lodash' export class AddToOrder extends React.Component { constructor(props)…
bier hier
  • 20,970
  • 42
  • 97
  • 166
81
votes
15 answers

Verify that an exception is thrown using Mocha / Chai and async/await

I'm struggling to work out the best way to verify that a promise is rejected in a Mocha test while using async/await. Here's an example that works, but I dislike that should.be.rejectedWith returns a promise that needs to be returned from the test…
plexer
  • 4,542
  • 2
  • 23
  • 27
78
votes
9 answers

How can I check that two objects have the same set of property names?

I am using node, mocha, and chai for my application. I want to test that my returned results data property is the same "type of object" as one of my model objects (Very similar to chai's instance). I just want to confirm that the two objects have…
dan27
  • 1,425
  • 3
  • 14
  • 13
1
2 3
99 100