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

mocha pass variable to the next test

describe('some test', function(){ // Could put here a shared variable it('should pass a value', function(done){ done(null, 1); }); it('and then double it', function(value, done){ console.log(value * 2); …
Poni
  • 11,061
  • 25
  • 80
  • 121
34
votes
3 answers

Mocha, Chai: Assert that Object is included in an Array of Objects

Chai has a nice way to assert if an Array includes a certain element expect([1,2,3]).to.include(2); What I would like is something similar, given an Array of Objects: expect([{a:1},{b:2}]).to.include({b:2}); Is this possible?
mck
  • 1,334
  • 2
  • 12
  • 20
34
votes
8 answers

What's the best way to unit test an event being emitted in Nodejs?

I'm writing a bunch of mocha tests and I'd like to test that particular events are emitted. Currently, I'm doing this: it('should emit an some_event', function(done){ myObj.on('some_event',function(){ assert(true); done(); }); …
manalang
  • 805
  • 1
  • 6
  • 10
33
votes
7 answers

How to fix "Error: Expected to be running in 'ProxyZone', but it was not found." in mocha testing?

I am trying to test my Tour of Heroes Angular application using mocha, chai & webpack. I have followed this post & also with the help of this guide, have gone through the setup and got the build errors fixed and tests up and running. But, my tests…
Kushan
  • 407
  • 1
  • 7
  • 11
33
votes
9 answers

Skipping a test in Cypress conditionally

I'm trying to find out if I'm able to conditionally skip a test it() in my test suite and deal with its async nature as well. I've read about conditional testing in Cypress docs https://docs.cypress.io/guides/core-concepts/conditional-testing.html…
DavidZ
  • 519
  • 1
  • 5
  • 9
33
votes
4 answers

What does 'pending' test mean in Mocha, and how can I make it pass/fail?

I am running my tests and noticed: 18 passing (150ms) 1 pending I haven't seen this before. Previously test either passed, or failed. Timeouts caused failures. I can see which test is failing because it's also blue. But it has a timeout on it.…
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
33
votes
5 answers

Mocha's describe "require() is missing" in WebStorm 11

I've just installed WebStorm 11 and created a new project, however WebStorm is saying that a require() is needed for the describe method: I've added the definitely-types mocha library and the scope is for the test directory (which this file is in).
BanksySan
  • 27,362
  • 33
  • 117
  • 216
32
votes
3 answers

Stubbing window.location.href with Sinon

I am trying to test some client-side code and for that I need to stub the value of window.location.href property using Mocha/Sinon. What I have tried so far (using this example): describe('Logger', () => { it('should compose a Log', () => { …
Francesco Pezzella
  • 1,755
  • 2
  • 15
  • 18
32
votes
9 answers

Protractor error message "unsupported command-line flag" in Chrome?

I'm a new user to Protractor, and I encountered this error running my tests using Chrome (error displays beneath the address bar in the launched browser): You are using an unsupported command-line flag --ignore-certificate-errors. Stability and…
mikeybaby173
  • 1,302
  • 2
  • 13
  • 26
32
votes
3 answers

Testing asynchronous function with mocha

I want to test a asynchronous javascript function that runs in node.js and makes a simple request to a http api: const HOST = 'localhost'; const PORT = 80; http = require('http'); var options = { host: HOST, port: PORT, path:…
MadFool
  • 321
  • 1
  • 3
  • 4
31
votes
3 answers

nodejs mocha suite is not defined error

I am trying to run some tests using mocha but cant seem to get over this error. E:\tdd\nodejs\cart>mocha cart.test.js node.js:201 throw e; // process.nextTick error, or 'err ^ ReferenceError: suite is not defined at…
thedev
  • 2,816
  • 8
  • 34
  • 47
31
votes
2 answers

Post request via Chai

I am trying to make a request to my node JS server which accepts post/put call. The parameters I am trying to send with post call via chai is not visible on server (req.body.myparam). I have tried with below post request but ended with not…
SCJP1.6 PWR
  • 371
  • 1
  • 4
  • 7
31
votes
2 answers

Testing JS exceptions with Mocha/Chai

Trying to test some code that throws an exception with Mocha/Chai, but having no luck, here's the simple code I'm trying to test: class window.VisualizationsManager test: -> throw(new Error 'Oh no') Here is my test: describe…
TheDelChop
  • 7,938
  • 4
  • 48
  • 70
31
votes
1 answer

Supertest custom express server in node

Please be gentle with me. I'm new to async coding and have been thrown headfirst into an intensive project using node to develop and API server. I'm loving it but some things aren't coming naturally. Our project is built using express js. We have a…
akronymn
  • 2,426
  • 4
  • 25
  • 39
30
votes
3 answers

Redux How to update the store in unit tests?

Using enzyme, mocha and expect asserts. The aim of my unit test is to check that dispatch gets called with the correct arguments when paused and not paused in mergeProps. I need to dynamically change the state of my store to do: paused: true. At…
Martin Dawson
  • 7,455
  • 6
  • 49
  • 92