Questions tagged [mocha.js]

Mocha.js is a feature-rich JavaScript test framework running on Node.js and the browser.

Mocha is a feature-rich test framework running on and the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

There is also a (completely separate) "mocha" library for Ruby, which is about mocking for tests. Searches for "mocha" will find both things, so check your results for and to get the one you are looking for!

7879 questions
431
votes
8 answers

How to increase timeout for a single test case in mocha

I'm submitting a network request in a test case, but this sometimes takes longer than 2 seconds (the default timeout). How do I increase the timeout for a single test case?
Mahendra S
  • 4,339
  • 2
  • 11
  • 4
350
votes
12 answers

How to run a single test with Mocha?

I use Mocha to test my JavaScript stuff. My test file contains 5 tests. Is that possible to run a specific test (or set of tests) rather than all the tests in the file?
Misha Moroshko
  • 148,413
  • 200
  • 467
  • 700
324
votes
3 answers

Code coverage with Mocha

I am using Mocha for testing my NodeJS application. I am not able to figure out how to use its code coverage feature. I tried googling it but did not find any proper tutorial. Please help.
tusharmath
  • 9,120
  • 11
  • 52
  • 70
290
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
  • 13,045
  • 22
  • 84
  • 131
253
votes
5 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
  • 4,699
  • 6
  • 25
  • 43
240
votes
14 answers

How to specify test directory for mocha?

Mocha tries to find test files under test by default, how do I specify another dir, e.g. server-test?
Freewind
  • 177,284
  • 143
  • 381
  • 649
216
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
  • 10,835
  • 13
  • 39
  • 53
207
votes
9 answers

How to access and test an internal (non-exports) function in a node.js module?

I'm trying to figure out on how to test internal (i.e. not exported) functions in nodejs (preferably with mocha or jasmine). And i have no idea! Let say I have a module like that: function exported(i) { return notExported(i) + 1; } function…
xavier.seignard
  • 10,084
  • 12
  • 44
  • 71
184
votes
4 answers

Change default timeout for mocha

If we have a unit test file my-spec.js and running with mocha: mocha my-spec.js The default timeout will be 2000 ms. It can be overwritten for partial test with a command line parameter: mocha my-spec.js --timeout 5000 Is it possible to change…
lm.
  • 3,439
  • 4
  • 21
  • 33
172
votes
2 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,335
  • 6
  • 19
  • 21
166
votes
14 answers

How to programmatically skip a test in mocha?

I have a code where certain tests will always fail in CI environment. I would like to disable them based on an environment condition. How to programmatically skip a test in mocha during the runtime execution?
Gajus
  • 55,791
  • 58
  • 236
  • 384
163
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
  • 59,357
  • 43
  • 187
  • 234
158
votes
9 answers

Invariant Violation: Could not find "store" in either the context or props of "Connect(SportsDatabase)"

Full code here: https://gist.github.com/js08/0ec3d70dfda76d7e9fb4 Hi, I have an application where it shows different templates for desktop and mobile on basis of build environment. I am successfully able to develop it where I need to hide the…
user6015171
153
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,575
  • 2
  • 10
  • 7
153
votes
8 answers

How can I mock the imports of an ES6 module?

I have the following ES6 modules: File network.js export function getDataFromServer() { return ... } File widget.js import { getDataFromServer } from 'network.js'; export class Widget() { constructor() { getDataFromServer("dataForWidget") …
Kos
  • 64,918
  • 23
  • 156
  • 223
1
2 3
99 100