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
81
votes
14 answers

Is there a way to get Chai working with asynchronous Mocha tests?

I'm running some asynchronous tests in Mocha using the Browser Runner and I'm trying to use Chai's expect style assertions: window.expect = chai.expect; describe('my test', function() { it('should do something', function (done) { …
Thomas Parslow
  • 5,712
  • 4
  • 26
  • 33
80
votes
7 answers

How to say "any_instance" "should_receive" any number of times in RSpec

I've got an import controller in rails that imports several csv files with multiple records into my database. I would like to test in RSpec if the records are actually saved by using…
Harm de Wit
  • 2,150
  • 2
  • 18
  • 24
78
votes
9 answers

How can I check that two objects have the same set of property names?

I am using node, mocha, and chai for my application. I want to test that my returned results data property is the same "type of object" as one of my model objects (Very similar to chai's instance). I just want to confirm that the two objects have…
dan27
  • 1,425
  • 3
  • 14
  • 13
77
votes
9 answers

Mocha testing failed due to css in webpack

I'm new to Mocha and I am trying to use it to test a simple React component. The test would pass if the react component doesn't have any CSS styling but throws a syntax error if the tag within the React component contains any…
Yi Ren
  • 841
  • 1
  • 8
  • 17
75
votes
6 answers

Running the same mocha test multiple times with different data

Problem I have multiple Mocha tests that perform the same actions, leading to code duplication. This impacts the maintainability of the code base. var exerciseIsPetitionActive = function (expected, dateNow) { var actual =…
Tomás
  • 3,501
  • 3
  • 21
  • 38
75
votes
8 answers

How to authenticate Supertest requests with Passport?

I'm using Passport.js for authentication (local strategy) and testing with Mocha and Supertest. How can I create a session and make authenticated requests with Supertest?
Gal Ben-Haim
  • 17,433
  • 22
  • 78
  • 131
74
votes
2 answers

Jasmine vs. Mocha JavaScript testing for Rails 3.1+

I have experience with Jasmine and do like it quite a bit. Does anyone have experience with both Jasmine and Mocha, specifically for Rails? I am wondering if it's worth switching to.
LupineDev
  • 1,046
  • 1
  • 8
  • 10
74
votes
3 answers

How to assert not null?

I'm very new in javascript testing, I would like to know how to assert not null in Mocha framework.
Hosar
  • 5,163
  • 3
  • 26
  • 39
72
votes
8 answers

How to set execution order of mocha test cases in multiple files

I have two javascript files which contain mocha test cases. //----------abc.js ------------- describe("abc file", function(){ it("test 1" , function(){ assert.equal(20 , 20); }); }); //---------xyz.js-------------- describe("xyz file",…
tnishada
  • 1,315
  • 1
  • 16
  • 24
67
votes
5 answers

How do i test my express app with mocha?

I've just added shouldjs and mocha to my express app for testing, but I'm wondering how to test my application. I would like to do it like this: app = require '../app' routes = require '../src/routes' describe 'routes', -> describe…
Robin Heggelund Hansen
  • 4,906
  • 6
  • 37
  • 54
67
votes
11 answers

How to unit test private methods in Typescript

When I tried to do unit testing for private methods in a Class getting error as private methods are only accessible inside the class. Here I added sample snippet for my class and mocha test. Kindly provide me solution to implement unit test for…
Ashok JayaPrakash
  • 2,115
  • 1
  • 16
  • 22
67
votes
2 answers

Testing a redirect to a new route with Cypress

I am using Cypress for testing my web application. This snippet currently works and will submit a new thing: describe('The Create Page', () => { it('successfully creates a thing', () => { cy.visit('/create') …
SeanPlusPlus
  • 8,663
  • 18
  • 59
  • 84
66
votes
1 answer

Mongoose & unique field

I have the following schema with mongoose: var SimSchema = new Schema({ msisdn : { type : String , unique : true, required : true }, imsi : { type : String , unique : true, required : true }, status : { type : Boolean,…
Julio
  • 2,493
  • 4
  • 33
  • 53
65
votes
6 answers

How to suppress application logging messages from a node.js application when running unit tests?

While unit-testing my node.js application (which is basically a REST backend) using mocha and supertest, I need only the test-specific message on the screen, but the stdout is also cluttered with application log messages. I start the unit test…
andimeier
  • 1,213
  • 2
  • 12
  • 19
65
votes
1 answer

Skip subsequent Mocha tests from spec if one fails

I can't find a way how to stop some part of it's from run if one of them failed I'm using mocha-as-promised, so the code might look different from as usuall describe("remote promises", function() { describe("browsing", function() { …
llamerr
  • 2,997
  • 4
  • 29
  • 40