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
103
votes
3 answers

Async function in mocha before() is alway finished before it() spec?

I have a callback function in before() which is for cleaning database. Is everything in before() guaranteed to finish before it() starts? before(function(){ db.collection('user').remove({}, function(res){}); // is it guaranteed to finish before…
Nicolas S.Xu
  • 13,794
  • 31
  • 84
  • 129
98
votes
17 answers

Babel unexpected token import when running mocha tests

The solutions offered in other related questions, such as including the proper presets (es2015) in .babelrc, are already implemented in my project. I have two projects (lets call them A and B) which both use ES6 module syntax. In Project A, I'm…
ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82
97
votes
5 answers

What is the role of describe() in Mocha?

The documentation at the official Mocha site contains this example: describe('User', function(){ describe('#save()', function(){ it('should save without error', function(done){ var user = new User('Luna'); user.save(function(err){ …
mathlearner
  • 7,509
  • 31
  • 126
  • 189
96
votes
8 answers

joining tests from multiple files with mocha.js

I'm trying to join all the tests from multiple files in one file, something like this: describe('Controllers', function() { describe('messages.js', function() { require('./controllertests/messages').test(options); }) …
coiso
  • 7,151
  • 12
  • 44
  • 63
96
votes
10 answers

Mocha tests with extra options or parameters

I am writing test cases for my Node.js application using Mocha. The test cases need an API key as an extra input option or parameter. The API key is private, so I don't want to include it directly in the test files as everyone then can see it on…
afterglowlee
  • 11,670
  • 5
  • 22
  • 23
89
votes
3 answers

Is there a .mocha file where I can specify defaults such as --no-colors?

I'd like to set some defaults for mocha without having to type them each time. Does mocha look for a config file / dotfile anywhere, as jshint looks for .jshintrc and npm looks for package.json?
mcandre
  • 22,868
  • 20
  • 88
  • 147
87
votes
2 answers

How to test nodejs backend code with Karma (testacular)

How do I setup Karma to run my backend unit tests (written with Mocha)? If I add my backend test script to the files = [], it fails stating that require is undefined.
Sylvain
  • 19,099
  • 23
  • 96
  • 145
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
84
votes
9 answers

Overriding `tsconfig.json` for ts-node in mocha

Is it possible to override which tsconfig.json ts-node uses when called from mocha? My main tsconfig.json contains "module": "es2015", but I want to use "module": "commonjs" for ts-node only. I tried this mocha --compilers…
Ralph
  • 31,584
  • 38
  • 145
  • 282
84
votes
1 answer

In node package.json, invoke script from another script with extra parameter, in this case add mocha watcher

in node's package.json I would like to reuse a command that I already have in a 'script'. Here is the practical example instead of (note the extra -w on the watch script): "scripts": { "test" : "./node_modules/mocha/bin/mocha…
Dinis Cruz
  • 4,161
  • 2
  • 31
  • 49
83
votes
4 answers

Mocha beforeEach vs before execution

I ran into a problem recently that I can't explain. I have alot of code in these tests so I'm going to do my best to capture the idea here I have tests that look like: describe('main page', function() { beforeEach(function(done){ …
Tomo
  • 907
  • 1
  • 7
  • 8
82
votes
6 answers

Global `before` and `beforeEach` for mocha?

I'm using mocha for javascript unit-testing now. I have several test files, each file has a before and beforeEach, but they are exactly the same. How do I provide a global before and beforeEach for all of them (or some of them)?
Freewind
  • 193,756
  • 157
  • 432
  • 708
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
81
votes
11 answers

How can I solve "ReferenceError: expect is not defined" error message?

I am trying to test Javascript with mocha. I've this snippet of code: describe('Array', function() { describe('indexOf()', function() { it("dovrebbe tornare -1 quando l'elemento non è presente", function() { …
sensorario
  • 20,262
  • 30
  • 97
  • 159
81
votes
5 answers

What's the right way to enable the node debugger with mocha's --debug-brk switch?

I have some debugger statements in my module under test and want to run mocha with --debug-brk set and hit my breakpoint so that I can inspect the state of my module. Unfortunately, whenever I run mocha with this option, I end up with a blank cursor…
Howard Dierking
  • 1,204
  • 1
  • 11
  • 12