Mocha + Nodejs + Heroku .env文件

Gim*_*mli 10 heroku mocha.js node.js heroku-postgres

我有一个在Heroku上运行的NodeJS编写的REST应用程序.我有本地开发的.env文件设置,每当我运行foreman在本地提供我的应用程序时,它都能正常工作.当我将它部署到我的heroku服务器时,该应用程序也运行良好.

我正在尝试使用Mocha/Supertest/should/assert为我的应用编写单元测试.当我通过Mocha运行我的应用程序时,它不会加载.env文件来获取我的环境变量 - 在我的例子中,是PSQL数据库的URL.结果,我的所有测试都涉及DB I/O超时.

我一直在寻找互联网寻求解决方案,但我似乎找不到任何有用的东西.

以下是一些示例代码:

app.js:

var application_root = __dirname,
    express          = require("express"),
    port             = process.env.PORT || 4482;
    pg               = require('pg').native,
    client           = new pg.Client(process.env.DATABASE_URL);

// Connect To DB
client.connect();

(...)

app.get('/api', function (req, res) {
  res.send('PS API is running');
});
app.get('/', function (req, res) {
  res.send('PS API is running');
});

(...)

// Read Users
app.get('/users', function (req,res) {
    user.readUsers(res,client);
});

(...)

// Launch server
console.log('Listening on port: '+ port);
app.listen(port);
module.exports = app;
Run Code Online (Sandbox Code Playgroud)

userTest.js

var request = require('supertest');
var assert  = require('assert');
var app     = require('app.js');
var should  = require('should');

describe('Get /', function(){
    it('should respond OK',function(done){
        request(app)
        .get('/')
        .end(function(err, res){
          res.status.should.equal(200);
          done(err);
        });
    });
});

describe('Get /api', function(){
    it('should respond OK',function(done){
        request(app)
        .get('/api')
        .end(function(err, res){
          res.status.should.equal(200);
          done(err);
        });
    });
});

// Getting All Users
describe('Get /users', function(){
    it('should respond OK',function(done){
        request(app)
        .get('/users')
        .end(function(err, res){
          res.status.should.equal(200);
          done(err);
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

.ENV

== LOCAL DB ==
DATABASE_URL=MY_DB_URL
HEROKU_POSTGRESQL_GOLD_URL=MY_DB_URL
PATH=bin:node_modules/.bin:/usr/local/bin:/usr/bin:/bin
Run Code Online (Sandbox Code Playgroud)

我从跑步中得到的输出 mocha test

Listening on port: 4482

  ??Getting all users
?

  2 passing (2 seconds)
  1 failing

  1) Get /users should respond OK:
     Error: timeout of 2000ms exceeded
      at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14)
      at Timer.list.ontimeout (timers.js:101:19)
Run Code Online (Sandbox Code Playgroud)

当我process.env.DATABASE_URL用我的硬编码PSQL本地URL 替换时,测试全部通过.所以很明显mocha没有读取.env文件.

我也尝试过将env vars传递给Mocha,但收效甚微.有没有人知道在我的环境中使用.env文件读取Mocha的正确方法?

Rya*_*cox 13

工头宝石(由Heroku的工程师编写,使其易于使用.env的文件和Procfiles在发展,你会在Heroku生产)有一个命令只是为了这个目的:run.

foreman run npm test < - 或者你触发测试.

或者,在我目前的项目中,这是我们的工作:

  • 我们有一个test.env文件,其中包含适用于测试的环境变量和值,采用Unix export格式.所以,是的,export DATABASE_URL=MY_DB_URL.格式有点不同,但这是一个烦恼,我们没关系
  • 我们有一个包含以下指令的Makefile:
-include test.env

test:
    npm test

当我们想要为我们的项目运行测试时,我们就是这样make test.Make将加载test.env文件,分配所有环境变量,然后npm test为我们运行.