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

How do you test an AJAX request with RSpec/RoR?

I'm fairly new to RoR and recently started learning BDD/Rspec for testing my application. I've been looking for a way to spec an AJAX request, but so far I haven't found much documentation on this at all. Anyone know how to do this? I'm using…
user480826
  • 611
  • 1
  • 5
  • 3
61
votes
4 answers

How to run Mocha tests written in TypeScript?

Setup: I have a Node project (pure Node, no browser bits) written in TypeScript. I can use the TypeScript compiler (tsc) from the typescript module to compile the code. So far so good. However, I want to write tests using Mocha, and that's where I'm…
Thomas
  • 174,939
  • 50
  • 355
  • 478
59
votes
4 answers

How can I execute async Mocha tests (NodeJS) in order?

This question relates to the Mocha testing framework for NodeJS. The default behaviour seems to be to start all the tests, then process the async callbacks as they come in. When running async tests, I would like to run each test after the async part…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
59
votes
8 answers

Using webpack aliases in mocha tests

I'm developing a web app at work in React/Redux/Webpack and am now starting to integrate testing with Mocha. I followed the instructions for writing tests in the Redux documentation, but now I have run into an issue with my webpack aliases. For…
Danny Delott
  • 6,756
  • 3
  • 33
  • 57
58
votes
5 answers

How to Unit Test React-Redux Connected Components?

I am using Mocha, Chai, Karma, Sinon, Webpack for Unit tests. I followed this link to configure my testing environment for React-Redux Code. How to implement testing + code coverage on React with Karma, Babel, and Webpack I can successfully test my…
Ayushya
  • 1,862
  • 1
  • 10
  • 15
58
votes
6 answers

How can I make Mocha load a helper.js file that defines global hooks or utilities?

I have a file named test/helper.js that I use to run Mocha tests on my Node.js apps. My tests structure looks like: test/ test/helper.js # global before/after test/api/sometest.spec.js test/models/somemodel.spec.js ... more here The file…
Zlatko
  • 18,936
  • 14
  • 70
  • 123
58
votes
3 answers

How to run mocha and mocha-phantomjs tests from one "npm test" command in node.js?

I have got few node packages which works in node.js environment and also in browser. Now I have got two seperate tests (for each environment). What is the best way to run these tests with just npm test command? Also I want to add these packages to…
David Kudera
  • 806
  • 2
  • 8
  • 14
57
votes
7 answers

Why am I getting "Error: Resolution method is overspecified"?

After the upgrade, Mocha can not even run a simple test here is the code const assert = require('assert'); it('should complete this test', function (done) { return new Promise(function (resolve) { assert.ok(true); resolve(); }) …
coder fire
  • 993
  • 2
  • 11
  • 24
54
votes
7 answers

How to mock e.preventDefault in react component's child

Hy, I don't know how to mock an inline function in React component's child My stack: sinon, chai, enzyme; Component usage: someFn()} /> Component's render: render() { return (
  • S Panfilov
    • 16,641
    • 17
    • 74
    • 96
  • 53
    votes
    4 answers

    How do you run mocha tests in the browser?

    Is it just me, or does their documentation not explain how to run the tests in the browser at all? Do I have to create that HTML file that they show in the example? How do I make it run my specific set of test cases for my project then? I want the…
    Marco Prins
    • 7,189
    • 11
    • 41
    • 76
    53
    votes
    4 answers

    With Supertest, can I create an alternative request with some headers set by default?

    I am using Supertest with Mocha to test an API developed with Node JS. And I want to do a lót of different tests on the API. With almost all of them I have to set Authorization and Content-Type headers again (because the API requires them for this…
    Christiaan Westerbeek
    • 10,619
    • 13
    • 64
    • 89
    52
    votes
    4 answers

    How to set timeout on before hook in mocha?

    I want to set timeout value on before hook in mocha test cases. I know I can do that by adding -t 10000 on the command line of mocha but this will change every test cases timeout value. I want to find a way to change the timeout programmatically…
    Joey Yi Zhao
    • 37,514
    • 71
    • 268
    • 523
    49
    votes
    1 answer

    How to test async code with mocha using await

    How do I test async code with mocha? I wanna use multiple await inside mocha var assert = require('assert'); async function callAsync1() { // async stuff } async function callAsync2() { return true; } describe('test', function () { …
    deathangel908
    • 8,601
    • 8
    • 47
    • 81
    49
    votes
    3 answers

    Running Mocha + Istanbul + Babel

    I'm having some issues while running Istanbul with Mocha and the Babel compiler. All my tests are running just fine, but after all the tests done it shows me this message: No coverage information was collected, exit without writing coverage…
    Weslley Araujo
    • 668
    • 1
    • 6
    • 18
    49
    votes
    1 answer

    How to mock dependency classes for unit testing with mocha.js?

    Given that I have two ES6 classes. This is class A: import B from 'B'; class A { someFunction(){ var dependency = new B(); dependency.doSomething(); } } And class B: class B{ doSomething(){ // does something …
    mvmoay
    • 1,535
    • 3
    • 15
    • 26