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
49
votes
1 answer

Reset "called" Count on Sinon Spy

How do I reset the "called" count on a Sinon spy before each test? Here's what I'm doing now: beforeEach(function() { this.spied = sinon.spy(Obj.prototype, 'spiedMethod'); }); afterEach(function() { Obj.prototype.spiedMethod.restore(); …
cantera
  • 24,479
  • 25
  • 95
  • 138
49
votes
3 answers

How do I test 'normal' (non-Node specific) JavaScript functions with Mocha?

This seems like it should be extremely simple; however, after two hours of reading and trial-and-error without success, I'm admitting defeat and asking you guys! I'm trying to use Mocha with Should.js to test some JavaScript functions, but I'm…
Mark Bell
  • 28,985
  • 26
  • 118
  • 145
48
votes
3 answers

How to unit test console output with mocha on nodejs?

Take into account the following example Javascript code below: function privateFunction (time) { if (time < 12) { console.log('Good morning'); } if (time >= 12 && time <19) { console.log('Good afternoon'); } else { console.log('Good night!');…
Kemel Zaidan
  • 503
  • 1
  • 5
  • 9
48
votes
1 answer

How can I simulate the passing of time in Mocha tests so that setTimeout callbacks are called?

I need to test JavaScript code that relies on setTimeout in order to perform periodic tasks. How can I from my Mocha tests simulate the passing of time so that setTimeout callbacks gets called? I am basically asking for functionality similar to…
aknuds1
  • 65,625
  • 67
  • 195
  • 317
48
votes
11 answers

what's the least resistance path to debugging mocha tests?

Edit Nov 2016: Node now has a built in debugger that you can start with --inspect. This answer explains it: https://stackoverflow.com/a/39901169/30946. I'm building a mocha test in coffeescript. Right at the top of the test I have: require…
jcollum
  • 43,623
  • 55
  • 191
  • 321
47
votes
4 answers

How to get Mocha to fail a test

I have the following test: it.only('validation should fail', function(done) { var body = { title: "dffdasfsdfsdafddfsadsa", description: "Postman Description", beginDate: now.add(3, 'd').format(), endDate:…
Pompey Magnus
  • 2,191
  • 5
  • 27
  • 40
45
votes
32 answers

mongoose.connect(), first argument should be String, received undefined

I am trying to set the test database for the testing purpose, but its not working. I am trying to connect to MongoDB using mongoose, but finding problem in the connection error shows: throw new MongooseError('The `uri` parameter to `openUri()` must…
abhigyan nayak
  • 1,394
  • 6
  • 25
  • 35
45
votes
1 answer

How to mock middleware in Express to skip authentication for unit test?

I have the following in Express //index.js var service = require('./subscription.service'); var auth = require('../auth/auth.service'); var router = express.Router(); router.post('/sync', auth.isAuthenticated, service.synchronise); …
jeh
  • 2,373
  • 4
  • 23
  • 38
45
votes
1 answer

Use SinonJS to stub and spy on the same function?

In the following example, I want to stub the get function to prevent the actual HTTP request from occurring. I want to spy on the get method to check what arguments it was called with. var request = require('request'), sinon =…
Armand
  • 23,463
  • 20
  • 90
  • 119
44
votes
6 answers

Cannot run Mocha with CoffeeScript

Makefile - Content: REPORTER = dot all: build build: @./node_modules/coffee-script/bin/coffee \ -c \ -o lib src clean: rm -rf lib mkdir lib watch: @./node_modules/coffee-script/bin/coffee \ -o lib \ …
Sameet
  • 2,191
  • 7
  • 28
  • 55
44
votes
17 answers

Testing React Select component

https://github.com/JedWatson/react-select I would like to use React-Select react component, but I need to add tests. I've tried several options I found with google, but nothing seems to work. I have the code below, but it's not causing a change…
Llewellyn
  • 651
  • 2
  • 8
  • 9
43
votes
6 answers

Trying ES6 style import gives 'Cannot use import statement outside a module'

I am trying to write a javascript test in intellij for which I need to import some dependancies and I want to use ES6 style import statements but getting error /usr/local/bin/node /workspace/rr-sample/node_modules/mocha/bin/_mocha --ui bdd…
driftwood
  • 2,051
  • 4
  • 21
  • 28
42
votes
1 answer

How to test React Native Module?

I developed a React Native module (wrapping an SDK) and I’m interested in creating some unit tests using mocha. I’m not very familiar with mocha, but I can’t exactly figure out how to proceed. I have my react native module, call it…
Clip
  • 3,018
  • 8
  • 42
  • 77
42
votes
1 answer

Enzyme simulate an onChange event

I'm testing a react component with Mocha and Enzyme. Here is the component (shortened for simplicity of course): class New extends React.Component { // shortened for simplicity handleChange(event) { // handle changing state of input …
stoebelj
  • 1,536
  • 2
  • 14
  • 31
42
votes
7 answers

How to await and return the result of a http.request(), so that multiple requests run serially?

Assume there is a function doRequest(options), which is supposed to perform an HTTP request and uses http.request() for that. If doRequest() is called in a loop, I want that the next request is made after the previous finished (serial execution, one…
CodeManX
  • 11,159
  • 5
  • 49
  • 70