node.js 在测试环境中运行 mocha 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12236890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run mocha tests in test environment?
提问by Feech
I can't seem to tell mocha to run my test suite in the test environment.
我似乎无法告诉 mocha 在测试环境中运行我的测试套件。
app.js
应用程序.js
app.configure('test', function(){
app.set('port', 3002);
});
test/some-test.coffee
测试/一些测试.coffee
app = require('../../app')
process.env.NODE_ENV = 'test'
describe 'some test', ->
it 'should pass', ->
Since I'm requiring app, when I run the tests I expect to see
因为我需要应用程序,所以当我运行测试时,我希望看到
Express server listening on port 3002
Express server listening on port 3002
and instead I see
相反,我看到
Express server listening on port 3000
Express server listening on port 3000
Setting a different port number in a development configuration block in app.js yields
在 app.js 的开发配置块中设置不同的端口号会产生
Express server listening on port [whatever port I set in development block in app.js]
Express server listening on port [whatever port I set in development block in app.js]
I cannot get my tests to run in a test environment. Any suggestions?
我无法让我的测试在测试环境中运行。有什么建议?
回答by Vadim Baryshev
You need to define NODE_ENV before you require
app.js:process.env.NODE_ENV = 'test' app = require('../../app') describe 'some test', -> it 'should pass', ->You can't change listening port by
app.set. There is only one way to set port - pass it intolistenmethod. You can do something like this:var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); var port = 3000; app.configure('test', function(){ port = 3002; }); app.listen(port);
您需要在需要之前定义 NODE_ENV
app.js:process.env.NODE_ENV = 'test' app = require('../../app') describe 'some test', -> it 'should pass', ->您不能通过
app.set. 只有一种设置端口的listen方法- 将其传递给方法。你可以这样做:var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); var port = 3000; app.configure('test', function(){ port = 3002; }); app.listen(port);
回答by Eat at Joes
I would take a different approach from Vadim. Using Vadim's example you could instead load environment settings based on your process.env.NODE_ENVvalue. I know there is another step in my approach, but it is cleaner, extensible, and will prevent the addition of test conditionals in your logic.
我会采取与 Vadim 不同的方法。使用 Vadim 的示例,您可以改为根据您的process.env.NODE_ENV值加载环境设置。我知道我的方法还有另一个步骤,但它更清晰、可扩展,并且可以防止在您的逻辑中添加测试条件。
This approach uses dotenv, then defining both a defaultand a testenvironment file in the root of the application. These files will allow you to reconfigure properties within your application without changing your JavaScript.
这种方法使用dotenv,然后在应用程序的根目录中定义一个default和一个test环境文件。这些文件将允许您在不更改 JavaScript 的情况下重新配置应用程序中的属性。
Add dotenv as a
dependencyin yourpackage.jsonfile then install the new packages into yournode_modulesfolder:package.json
{ ... "dependencies": { ... "dotenv": "0.2.8" } }command line
$ npm installChange your
app.jsso that the port is using an environment value set from the loaded .env file.// Load .env files from root based on the NODE_ENV value require('dotenv').load(); var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); var port = process.env.port || 3000; -----------^ app.listen(port);Create two files in your folder root,
.env&.env.test, just add the one line below. These files have simple key value pairs on each line which can be accessed when prefixed withprocess.env...env
port=3000.env.test
port=3002From the command line or when you call your tests I would set
NODE_ENV:$ NODE_ENV=test mocha <TEST_FOLDER>/*.js ---------^
将 dotenv 添加
dependency到您的package.json文件中,然后将新软件包安装到您的node_modules文件夹中:包.json
{ ... "dependencies": { ... "dotenv": "0.2.8" } }命令行
$ npm install更改您
app.js的端口,以便端口使用从加载的 .env 文件中设置的环境值。// Load .env files from root based on the NODE_ENV value require('dotenv').load(); var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); var port = process.env.port || 3000; -----------^ app.listen(port);在文件夹根目录中创建两个文件
.env&.env.test,只需在下面添加一行。这些文件的每一行都有简单的键值对,当前缀为process.env...env
port=3000.env.test
port=3002从命令行或当您调用测试时,我会设置
NODE_ENV:$ NODE_ENV=test mocha <TEST_FOLDER>/*.js ---------^
When you run the application in all other cases without setting NODE_ENV, the values in the default .envfile will be loaded into process.env.
当您在所有其他情况下运行应用程序而不进行设置时NODE_ENV,默认.env文件中的值将被加载到process.env.

