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!

8538 questions
463
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,659
  • 2
  • 13
  • 4
433
votes
15 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
  • 166,356
  • 226
  • 505
  • 746
376
votes
4 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
  • 10,622
  • 12
  • 56
  • 83
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
271
votes
15 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
  • 193,756
  • 157
  • 432
  • 708
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
235
votes
11 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
  • 11,254
  • 13
  • 51
  • 75
225
votes
5 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.
  • 4,033
  • 4
  • 25
  • 37
214
votes
17 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
  • 69,002
  • 70
  • 275
  • 438
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
180
votes
12 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
  • 70,399
  • 25
  • 169
  • 233
178
votes
10 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
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
1
2 3
99 100