I'm trying to stub function called on one of my routes in express Router with request from supertest library. I see that function foo is called correctly, unfortunately it is not replaced by stub function I wrote in test. Code is written in ES6 and I'm using babel-register
and babel-polyfill
to make it work.
I run testing script using
./node_modules/mocha/bin/mocha server --timeout 10000 --compilers js:babel-register --require babel-polyfill --recursive
router.js
import {foo} from '../controller';
const router = new Router();
router.route(ROUTE).post(foo);
controller.js
export function foo(req, res) {
res.status(200).send({
ok: 'ok'
});
}
test.js
import request from 'supertest';
import sinon from 'sinon';
import {app} from 'app';
import * as controller from 'controller';
const agent = request.agent(app);
describe('Admin routes tests', () => {
it('Tests login admin route', async () => {
const bar = () => {
console.log('bar');
};
sinon.stub(controller, 'foo', bar);
const req = await agent
.post(ROUTE)
.set('Accept', 'application/json');
console.log(stub.calledOnce); // false
});
});
Any help would be much appreciated.