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
169
votes
9 answers

Cleaning up sinon stubs easily

Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks. I see sandboxing is an option but I do not see how you can use a sandbox for this beforeEach -> sinon.stub some, 'method' …
austinbv
  • 9,297
  • 6
  • 50
  • 82
158
votes
5 answers

eslint should be listed in the project's dependencies, not devDependencies

Either I don't understand dependencies vs. devDependencies in node 100% yet or eslint is just wrong here (not capable of analyzing this correctly): 3:1 error 'chai' should be listed in the project's dependencies, not devDependencies …
PositiveGuy
  • 17,621
  • 26
  • 79
  • 138
157
votes
11 answers

Unit testing of private functions with Mocha and Node.js

I am using Mocha in order to unit test an application written for Node.js. I wonder if it's possible to unit test functions that have not been exported in a module. Example: I have a lot of functions defined like this in foobar.js: function…
fstab
  • 4,801
  • 8
  • 34
  • 66
142
votes
20 answers

For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves

I have this test of nodejs when testing I get a error of done function not declared. Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. My test code is, I have the…
G B
  • 2,323
  • 3
  • 18
  • 32
140
votes
5 answers

Testing javascript with Mocha - how can I use console.log to debug a test?

I am using the javascript test-runner "Mocha". I have a test that is failing, so I would to debug it using console.log. But when the tests are run, there is no output (only the test results from Mocha). It seems like Mocha has captured and…
Nick Perkins
  • 8,034
  • 7
  • 40
  • 40
135
votes
6 answers

How do I programmatically shut down an instance of ExpressJS?

I'm trying to figure out how to shut down an instance of Express. Basically, I want the inverse of the .listen(port) call - how do I get an Express server to STOP listening, release the port, and shutdown cleanly? I know this seems like it might be…
drewww
  • 2,485
  • 4
  • 22
  • 24
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
130
votes
18 answers

Property 'toBeInTheDocument' does not exist on type 'Matchers'

I'm trying to write tests for my simple React App that creates a UI for a dog Shelter using API etc. I have imported the modules shown below and ran the following command npm install jest-dom react-testing-library --save-dev However, I'm getting…
mangokitty
  • 1,759
  • 3
  • 12
  • 17
125
votes
22 answers

Mocha breakpoints using Visual Studio Code

Is it possible to add breakpoints to Mocha tests using Visual Studio Code? Normally when debugging code, one needs to configure the launch.json, setting the program attribute to the Javascript file to execute. I am not sure how to do this for Mocha…
André Vermeulen
  • 1,774
  • 4
  • 17
  • 26
117
votes
6 answers

JavaScript Standard Style does not recognize Mocha

I have a Mocha test file that looks like this: var expect = require('chai').expect var muting = require('../muting') describe('muting', function () { describe('init()', function () { it('should inject an object into twitter', function () { …
urig
  • 16,016
  • 26
  • 115
  • 184
116
votes
2 answers

What is the difference between `before()` and `beforeEach()`?

What specifically is the difference between Mocha's before() and beforeEach()? (Same question for after() and afterEach().) I assume before() runs once per describe() block, and beforeEach() runs once per test (it() block). Is that true? And when…
ericsoco
  • 24,913
  • 29
  • 97
  • 127
114
votes
11 answers

Cannot find module 'ts-node/register'

I want to use mocha to test my TypeScript/Angular2 project. I tried to use ts-node as described here: npm install -g ts-node but when running mocha --require ts-node/register -t 10000 ./**/*.unit.ts I get an error Cannot find module…
lenny
  • 2,978
  • 4
  • 23
  • 39
114
votes
1 answer

The difference between assert.equal and assert.deepEqual in Javascript testing with Mocha?

I'm using Mocha to test a small module in my Express.js application. In this module, one of my functions returns an array. I want to test whether or not the array is correct for a given input. I am doing so like this: suite('getWords', function(){ …
mshell_lauren
  • 5,171
  • 4
  • 28
  • 36
112
votes
6 answers

How do you install and run Mocha, the Node.js testing module? Getting "mocha: command not found" after install

I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running. I've installed it using npm (both globally and locally), and each time…
Stephen Melrose
  • 4,772
  • 5
  • 29
  • 42
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