[英]How do I mock my config file for testing?
I have a Koa app I just started and I need to test something that grabs data from a config file.我有一个刚刚启动的 Koa 应用程序,我需要测试一些从配置文件中获取数据的东西。
I need to test with specific data, but I'm not sure how to modify what data the test receives from the config file.我需要使用特定数据进行测试,但我不确定如何修改测试从配置文件接收的数据。
Example:例子:
app.js应用程序.js
var router = require('koa-router');
var config = require('./config.js');
var db = require('./db.js');
var auth = require('./auth');
var app = require('koa')();
router.get('/', function *() {
if(auth(this.req, config.credentials.secret)) { // Authenticates request based on a hash created using a shared secret
this.body = "Request has been authenticated";
}
});
app.use(router.routes());
app = module.exports = http.createServer(app.callback());
app.listen(3000);
appSpec.js appSpec.js
var request = require('supertest');
var app = require('../app.js');
describe('app', function() {
it('should authenticate all requests against config shared secret', function() {
var secret = 'some_secret';
var validHash = /* hash created from test secret and query */;
request(app)
.get('/')
.query({query: 'some_query'})
.query({hash: validHash})
.expect(403, done);
});
});
This spec will fail because the app will use the secret from the config file(empty string) instead of my test secret.此规范将失败,因为应用程序将使用配置文件(空字符串)中的机密而不是我的测试机密。
Alright, I played around with some different ways to handle this.好吧,我尝试了一些不同的方法来处理这个问题。
The best option I found, for my particular use case, was proxyquire .对于我的特定用例,我发现的最佳选择是proxyquire 。 It's an npm package that lets you override dependencies in any file that you require in your test suites.
它是一个 npm 包,可让您覆盖测试套件中所需的任何文件中的依赖项。
So if I am testing this module:因此,如果我正在测试此模块:
./controllers/myController.js ./controllers/myController.js
var config = require('../config.js');
module.exports = function() {
// Do some stuff
};
I would do something like this:我会做这样的事情:
./test/controllers/myControllerSpec.js ./test/controllers/myControllerSpec.js
var proxyquire = require('proxyquire');
var config = {
credentials: {
secret: 'my_secret'
}
// other fake config stuff
};
var myController = proxyquire('../../controllers/myController.js', {'../config.js', config});
describe('myController', function() {
// TESTS
});
and this instance of myController
will use my test config.这个
myController
实例将使用我的测试配置。
This won't work for end to end testing, unless the only place you import your config is the main app file.这不适用于端到端测试,除非您导入配置的唯一位置是主应用程序文件。
I use node-config for my config files and configuration loading based on machine or env variable.我将node-config用于我的配置文件和基于机器或环境变量的配置加载。
You can specify your config in a variety of formats (.json, .js, yaml, etc.) Using the default settings, you need to create a config
folder in your app root and a default.<format>
with your default config.您可以以多种格式(.json、.js、yaml 等)指定您的配置。使用默认设置,您需要在您的应用程序根目录中创建一个
config
文件夹,并使用您的默认配置创建一个default.<format>
。
To override that for testing you can create a test.<format>
file in your config
directory.要覆盖它以进行测试,您可以在
config
目录中创建一个test.<format>
文件。 When you set your NODE_ENV=test
, then node-config
will see load your default config file and then it will load your test config file and if there are any conflicts for the values, your test config file will override the values in your default file.当您设置
NODE_ENV=test
, node-config
将看到加载您的默认配置文件,然后它将加载您的测试配置文件,如果这些值存在任何冲突,您的测试配置文件将覆盖默认文件中的值.
Here are the full docs for setting up Configuration Files in node-config
以下是在
node-config
设置配置文件的完整文档
Below is an example using node-config
with a .js
config file format.下面是一个使用
node-config
和.js
配置文件格式的示例。
./config/default.js ./config/default.js
module.exports = {
credentials: {
secret: ''
}
}
./config/test.js ./config/test.js
module.exports = {
credentials: {
secret: 'abcdef123456'
}
}
app.js应用程序.js
var router = require('koa-router');
var config = require('config');
var db = require('./db.js');
var auth = require('./auth');
var app = require('koa')();
var credentials = config.get('credentials');
router.get('/', function *() {
if(auth(this.req, credentials.secret)) { // Authenticates request based on a hash created using a shared secret
this.body = "Request has been authenticated";
}
});
app.use(router.routes());
app = module.exports = http.createServer(app.callback());
app.listen(3000);
Jest has nice support for this case. Jest 对这种情况有很好的支持。 In your test file, add
在您的测试文件中,添加
jest.mock('../config.js', () => ({
credentials: {
secret: 'my_secret'
}
// other fake config stuff }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.