问题
I'm trying to create a unit test for express node application. I want the configuration used for the test to be different than the one used in production, so I implemented the following.
In my index.js
, I load the configuration into the global variable like this:
global.env = {};
global.env.config = require('./config/config');
// Create the server ...
server.listen(3000);
module.exports = server;
In some other controller myController.js
, I access the global variable like this
var Config = global.env.config
When I launch this using node index.js
it works just fine.
But when I use mocha with proxyquire to override the config:
describe('myController', function () {
describe("#myMethod", () => {
it("must work", (done) => {
const config = {
INPUT_FILE_DIR: path.resolve('../ressources/input/')
}
const server = proxyquire('../index.js', { './config/config': config })// error in this line
})
})
})
I have an error telling that myController
can't read the property config
Cannot read property 'config' of undefined
Thanks for your help
回答1:
This is how I would have approached it. Firstly, i would export config as a function instead of an object.
The reason is the code will have a better structure and easy to maintain. Also there's no need to expose the config globally, as that could pose some security risk.
export const getConfig = () => {
if(process.env.NODE_ENV==="production"){
return require('./production.config');
}
return require('./default.config');
};
In my test file, I would mock the function call using sinonjs
like below.
const configModule = require("./config");
sinon.stub(configModule, "getConfig").returns(require('./e2e.config'));
This is not a tested code, but I a bit certain that this pattern of thought should work.
回答2:
Why not just overwrite it with the new config in your test case.
E.g.
index.js
:
const express = require('express');
const server = express();
const userController = require('./userController');
global.env = {};
global.env.config = require('./config');
server.get('/api/user', userController.getUser);
if (require.main === module) {
const port = 3000;
server.listen(port, () => {
console.log(`HTTP server is listening on http://localhost:${port}`);
});
}
module.exports = server;
userController.js
:
const Config = global.env.config;
const userController = {
getUser(req, res) {
res.json(Config.user);
},
};
module.exports = userController;
config.js
:
module.exports = {
user: { name: 'james' },
};
userController.test.js
:
const sinon = require('sinon');
describe('userController', () => {
describe('#getUser', () => {
it('should pass', () => {
global.env = {};
global.env.config = { user: { name: 'jane' } };
const userController = require('./userController');
const mReq = {};
const mRes = { json: sinon.stub() };
userController.getUser(mReq, mRes);
sinon.assert.calledWithExactly(mRes.json, { name: 'jane' });
});
});
});
unit test results with coverage report:
userController
#getUser
✓ should pass (880ms)
1 passing (893ms)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
userController.js | 100 | 100 | 100 | 100 |
-------------------|---------|----------|---------|---------|-------------------
UPDATE
index.js
:
// Order is matter, assign config to global firstly, then the controllers can access it.
global.env = {};
global.env.config = require('./config');
const express = require('express');
const server = express();
const userController = require('./userController');
server.get('/api/user', userController.getUser);
if (require.main === module) {
const port = 3000;
server.listen(port, () => {
console.log(`HTTP server is listening on http://localhost:${port}`);
});
}
module.exports = server;
userController.js
and config.js
are same as above.
index.test.js
:
const request = require('supertest');
const proxyquire = require('proxyquire');
const { expect } = require('chai');
describe('60990025', () => {
it('should get user', (done) => {
const config = { user: { name: 'jane' } };
const server = proxyquire('./', {
'./config': config,
});
request(server)
.get('/api/user')
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body).to.be.eql({ name: 'jane' });
done();
});
});
});
API test results with coverage report:
60990025
✓ should get user (2946ms)
1 passing (3s)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 81.25 | 50 | 50 | 81.25 |
config.js | 100 | 100 | 100 | 100 |
index.js | 72.73 | 50 | 0 | 72.73 | 12-14
userController.js | 100 | 100 | 100 | 100 |
-------------------|---------|----------|---------|---------|-------------------
source code: https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/60990025
来源:https://stackoverflow.com/questions/60990025/nodemochaglobal-variables-not-accessible-when-testing-with-mocha