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
41
votes
4 answers

TypeScript error TS2403: Subsequent variable declarations must have the same type

I seem to be running into some compile errors on my TypeScript project. The full error is: node_modules/@types/mocha/index.d.ts:2680:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'beforeEach' must be of type…
Luke
  • 20,878
  • 35
  • 119
  • 178
41
votes
3 answers

How to import "describe" and "it" from mocha in TypeScript?

By default, when importing mocha in TypeScript, it brings in describe and it (and some others) into the global namespace. Is there a way to bring in specific imports like import {describe, it} from 'mocha'?
Ace
  • 1,392
  • 3
  • 14
  • 20
41
votes
2 answers

Catching gulp-mocha errors

I may be missing something extremely obvious but I can't get gulp-mocha to catch errors, causing my gulp watch task to end everytime I have a failing test. It's a very simple set up: gulp.task("watch", ["build"], function () { …
Roy Jacobs
  • 602
  • 1
  • 5
  • 11
41
votes
4 answers

Testing requests that redirect with mocha/supertest in node

I can't seem to get the following integration test to pass in an express project using mocha, supertest, and should (and coffeescript). The test should = require('should') request = require('supertest') app = require('../../app') describe…
Feech
  • 4,072
  • 4
  • 28
  • 36
40
votes
7 answers

mocha Error: No test files found: "test/" npm ERR! Test failed

I am trying to run mocha tests on linux > @ test C:\Users\wd.ssh\tel\qa_test\mocha-api-tests Error: No test files found: "test/" npm ERR! Test failed. See above for more details. here is my package.json { "scripts": { "test": "mocha…
ranger
  • 637
  • 2
  • 9
  • 18
40
votes
6 answers

How to mock request and response in nodejs to test middleware/controllers?

My application has several layers: middleware, controllers, managers. Controllers interface is identical to middlewares one: (req, res, next). So my question is: how can I test my controllers without starting the server and sending 'real' requests…
potomok
  • 1,249
  • 3
  • 12
  • 16
40
votes
6 answers

How can you use cookies with superagent?

I'm doing cookie session management with express with something like this: req.session.authentication = auth; And I verify the authenticated urls with something like if(!req.session.authentication){res.send(401);} Now I'm building tests for the…
Kuryaki
  • 981
  • 2
  • 8
  • 18
40
votes
3 answers

Declare "pending" specs/tests in jasmine or mocha

I would like to describe specifications that should be in the code, but implementation of them would be added later. In test results I would like to see them neither passed nor failed, but "are waiting" for implementation instead. I'm interested if…
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
39
votes
9 answers

global leak errors in mocha

I was trying to unit test the apple push notification library when I got a global leak error trying to open up an APN connection. Is that a configuration error on my part or an error in node-apn or mocha? I'm not sure I understand what checkGlobals…
39
votes
2 answers

Difference between fake, spy, stub and mock of sinon library ( sinon fake vs spy vs stub vs mock )

I tried to understand difference between sinon library's fake, spy, stub and mock but not able to understand it clearly. Can anybody help me to understand about it?
Ziaullhaq Savanur
  • 1,848
  • 2
  • 17
  • 20
39
votes
4 answers

Running Mocha setup before each suite rather than before each test

Using NodeJS and Mocha for testing. I think I understand how before() and beforeEach() work. Problem is, I'd like to add a setup script that runs before each "describe" rather than before each "it". If I use before() it will run only once for the…
FuzzyYellowBall
  • 428
  • 1
  • 4
  • 5
38
votes
6 answers

Mocha + TypeScript: Cannot use import statement outside a module

I was watching this video in order to learn how to add some simple tests to my Express routes but I am getting all kind of errors while executing a test. The error is: import * as chai from 'chai'; ^^^^^^ SyntaxError: Cannot use import statement…
Maramal
  • 3,145
  • 9
  • 46
  • 90
38
votes
3 answers

unit test mocha Visual Studio Code describe is not defined

If i run in the console the test runs fine mocha --require ts-node/register tests/**/*.spec.ts Note: I installed mocha and mocha -g I want to run unit test from Visual Studio Code launcgh.js file "version": "0.2.0", …
Rolly
  • 3,205
  • 3
  • 26
  • 35
38
votes
3 answers

mocha command giving ReferenceError: window is not defined

I am using command: mocha --compilers :./test/babel-setup.js --recursive --watch It is giving error: ReferenceError: window is not defined I have the following in my babel-setup.js: require("babel/register")({ compact: false }); I am using node…
jit
  • 1,616
  • 3
  • 21
  • 49
38
votes
5 answers

How can I retrieve the current test's name within a Mocha test?

For additional logging, I need to be able to print the current test's description. How can I do this (with Mocha BDD)?
lairtech
  • 2,337
  • 4
  • 25
  • 38