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
37
votes
5 answers

Why do I see "define not defined" when running a Mocha test with RequireJS?

I am trying to understand how to develop stand-alone Javascript code. I want to write Javscript code with tests and modules, running from the command line. So I have installed node.js and npm along with the libraries requirejs, underscore, and…
andrew cooke
  • 45,717
  • 10
  • 93
  • 143
37
votes
1 answer

Istanbul nyc to exclude test files

I'm currently getting my test files in the final coverage. That's probably because they sit alongside my components instead of having their own test folder. How can I exclude these from the coverage? I have installed istanbul and nyc and I'm using…
Manu
  • 1,632
  • 3
  • 17
  • 23
37
votes
8 answers

Chai - Testing for values in array of objects

I am setting up my tests for the results to a REST endpoint that returns me an array of Mongo database objects. [{_id: 5, title: 'Blah', owner: 'Ted', description: 'something'...}, {_id: 70, title: 'GGG', owner: 'Ted', description:…
Jared Yach
  • 517
  • 1
  • 4
  • 10
36
votes
4 answers

Error: This method is only meant to be run on single node. 0 found instead

I am testing a keybinding feature in a component. The component is rather simple, event listener for the keyup and fires up a redux action which will hide the component. I have cleaned up my code here to only relevant information. I am able to make…
Sam Sedighian
  • 885
  • 1
  • 7
  • 21
36
votes
6 answers

How to check if two files have the same content?

I am using mocha/supertest/should.js to test REST Service GET /files/ returns file as stream. How can I assert in should.js that file contents are the same? it('should return file as stream', function (done) { var writeStream =…
hellboy
  • 1,567
  • 6
  • 21
  • 54
36
votes
2 answers

detecting test failures from within afterEach hooks in Mocha

I'm trying to create an afterEach hook with logic that should only fire if the previous test failed. For example: it("some_test1", function(){ // something that could fail }) it("some_test2", function(){ // something that could…
omdel
  • 911
  • 1
  • 9
  • 13
36
votes
6 answers

Chai unittesting - expect(42).to.be.an('integer')

According to http://chaijs.com/api/bdd/#a, a/an can be used to check for the type of a variable. .a(type) @param{ String } type @param{ String } message _optional_ The a and an assertions are aliases that can be used either as language chains or…
soerface
  • 6,417
  • 6
  • 29
  • 50
36
votes
1 answer

Difference between Karma and Mocha

I am new to the Javascript world, being majorly into OOP. I have tried to look up online for a clear cut distinction between Karma and Mocha but in vain. I know Karma is a Test Runner and Mocha is a unit testing Framework, but Mocha also has its own…
Setafire
  • 719
  • 2
  • 9
  • 21
35
votes
2 answers

How can mocha recursively search my `src` folder for a specific filename pattern?

In my npm package, I would like to emulate the pattern Meteor follows: a source file (named client.js) has a test file (named client.tests.js) live in a src/ folder. Tests run with the npm test command. I'm following the usage docs to the 't'. I do…
4Z4T4R
  • 2,340
  • 2
  • 26
  • 45
35
votes
3 answers

JS Unit testing run multiple times with different parameters

Is their any way to have multiple parameters in one test instead of copying and pasting the function again? Example in NUnit for C#: [TestCase("0", 1)] [TestCase("1", 1)] [TestCase("2", 1)] public void UnitTestName(string input, int expected) { …
Martin Dawson
  • 7,455
  • 6
  • 49
  • 92
35
votes
2 answers

Mocha tests don't run with Webpack and mocha-loader

Background I am porting some npm scripts to Webpack loaders to better learn how Webpack works and I’ve got everything working except for my Mocha tests: I have one failing test, but it is not showing that Mocha is being run with the mocha-loader or…
user3773571
34
votes
2 answers

How to test an npm module with peerDependencies?

I am new to understand peerDependencies, and I have read the following references seeking as to how to test an npm module contains peerDependencies within its package.json: Peer Dependencies Understanding the npm dependency model Common npm…
Jeff
  • 1,917
  • 1
  • 25
  • 43
34
votes
4 answers

Is it possible to use ES6 modules in Mocha tests?

ES6, Windows 10 x64, Node.js 8.6.0, Mocha 3.5.3 Is it possible to use ES6 modules in Mocha tests? I have the problems with export and import keywords. /* eventEmitter.js */ /* Event emitter. */ export default class EventEmitter{ …
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
34
votes
6 answers

Nested components testing with Enzyme inside of React & Redux

I have a component SampleComponent that mounts another "connected component" (i.e. container). When I try to test SampleComponent by mounting (since I need the componentDidMount), I get the error: Invariant Violation: Could not find "store" in…
Detuned
  • 3,652
  • 4
  • 27
  • 54
34
votes
2 answers

How to mock window/document with mocha/chai

When I try to unit test the getElement function class BarFoo { getElement() { return document.querySelector('#barfoo'); } } mocha doesn't know anything about document, so I figured that you might do something like…
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333