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

You can use sinon to stub isAuthenticated method, but you should do that before a reference to auth.isAuthenticated is set as a middleware, so before you require the index.js and app is created. Most likely you would want this in a beforeEach hook: var app; var auth; beforeEach(function() { auth = require(‘../wherever/auth/auth.service’); sinon.stub(auth, ‘isAuthenticated’) .callsFake(function(req, … Read more

Cannot run Mocha with CoffeeScript

As of Mocha 1.0: coffee-script is no longer supported out of the box. CS and similar transpilers may be used by mapping the file extensions (for use with –watch) and the module name. For example –compilers coffee:coffee-script with CoffeeScript 1.6- or –compilers coffee:coffee-script/register with CoffeeScript 1.7+. (Quoting http://visionmedia.github.io/mocha/#compilers-option) So, you need to add the line … Read more

How to get Mocha to fail a test

You can call assert.fail: it(“should return empty set of tags”, function() { assert.fail(“actual”, “expected”, “Error message”); }); Also, Mocha considers the test has failed if you call the done() function with a parameter. For example: it(“should return empty set of tags”, function(done) { done(new Error(“Some error message here”)); }); Though the first one looks clearer … Read more

How to unit test console output with mocha on nodejs?

I prefer mocha-sinon over “plain” sinon because it integrates nicely with Mocha. Example: var expect = require(‘chai’).expect; require(‘mocha-sinon’); // Function to test, can also be in another file and as long as it’s // being called through some public interface it should be testable. // If it’s not in any way exposed/exported, testing will be … Read more

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

I found out that Sinon.JS has support for manipulating the JavaScript clock, via sinon.useFakeTimers, as described in its Fake Timers documentation. This is perfect since I already use Sinon for mocking purposes, and I guess it makes sense that Mocha itself doesn’t support this as it’s more in the domain of a mocking library. Here’s … Read more

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

Edit, years later: the shortest path in Node 6+ is: mocha –debug-brk –inspect ./test.js coupled with the Node Inspector Manager plugin. Many weeks later, no answers. Here’s the quickest path that I found. write mocha tests install node-inspector start node-inspector — it will now be listening on 5858 start the mocha test with –debug-brk at … Read more

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

You can use SinonJS to create a stub to prevent the real function to be executed. For example, given class A: import B from ‘./b’; class A { someFunction(){ var dependency = new B(); return dependency.doSomething(); } } export default A; And class B: class B { doSomething(){ return ‘real’; } } export default B; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)