Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Cannot get to work with an express route module #38

Closed
saiait opened this issue Jan 4, 2019 · 4 comments
Closed

Cannot get to work with an express route module #38

saiait opened this issue Jan 4, 2019 · 4 comments

Comments

@saiait
Copy link

saiait commented Jan 4, 2019

I am trying to mock a dependency in a route module in an express js server but cannot get it to work.
I tried everything with the route module loaded in the main server/index.js file so I thought ok just remount the route on another root path so to have two paths to the routes with different prefixes like when you version an end point but this one will be for testing with the mocked service/business layer module which is called ecourier.controller in my sample. See below but even then on the new /apitest route path it still does not mock the Module.... AWWWWWW..

Please what am I doing wrong...

import chai from 'chai';
import server from '..';

var mock = require('mock-require');

chai.use(require('chai-http'));

var expect = chai.expect;

var route = '/apitest/shipmentDispatched';

var mockreq = null;

var winstoncls = {
stream: {
write: (msg) => { console.log(msg); }
}
}

const emptycls = { name: 'Dummy class' };

mock('../controllers/ecourier.controller', {
updateDispatch: function(res,req,config) {
res.json({ status: 'OK' });
}
});

mock.reRequire('../controllers/ecourier.controller');

import routes from '../routes';

server.use('/apitest', routes({ emptycls, emptycls, emptycls, winstoncls }));

@boblauer
Copy link
Owner

boblauer commented Jan 4, 2019

Instead of import routes, trying using require.

@saiait
Copy link
Author

saiait commented Jan 5, 2019

Thanks for the reply, Tried require instead still not picking up the mock, any other ideas?

import chai from 'chai';
import server from '..';
import { version } from '../../package.json';

var mock = require('mock-require');

chai.use(require('chai-http'));

var expect = chai.expect;

var route = '/apitest/shipmentDispatched';

var mockreq = null;

var winstoncls = {
stream: {
write: (msg) => { console.log(msg); }
}
}

const emptycls = { name: 'Dummy class' };

mock('../controllers/ecourier.controller', {
updateDispatch: function(res,req,config) {
res.json({ status: 'OK' });
}
});

mock.reRequire('../controllers/ecourier.controller');

var rtes = require('../routes').default;

server.use('/apitest', rtes({ emptycls, emptycls, emptycls, winstoncls }));

describe('Base API route path', () => {

@boblauer
Copy link
Owner

boblauer commented Jan 7, 2019

It's really hard to say without seeing the full code.

One thing you could try is setup your mock in a separate file before anything else gets required. In mocha that's mocha -r setup-mocks.js .

@saiait
Copy link
Author

saiait commented Jan 7, 2019

That worked, Thank you so much, appreciate the help.

@saiait saiait closed this as completed Jan 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants