1

私はそれを行う方法を見つけましたが、私の直感では、もっと慣用的な方法があるはずです. 基本的に私が気に入らないのは、テスト スイートで高速アプリを要求する必要があることです。これにより、競合状態が発生しているかどうか疑問に思います。また、このように複数のファイルで複数のテストスイートを実行するとどうなるのだろうか。

誰もがよりクリーンなソリューションを知っていますか?

私の簡略化されたアプリは次のとおりです。

app.js

app = module.exports = express()
...
http.createServer(app).listen(app.get('port'), function(){
     console.log('app listening');
});

test.js

var request = require('superagent');
var assert = require('assert');
var app = require('../app');
var port = app.get('port');
var rootUrl = 'localhost:'+port;

    describe('API tests', function(){
        describe('/ (root url)', function(){

            it('should return a 200 statuscode', function(done){
                request.get(rootUrl).end(function(res){
                    assert.equal(200, res.status);
                    done();
                });
            });
    ...
4

2 に答える 2

2

mocha では、ルート Suiteを使用して、任意の数のテストのためにサーバーを 1 回起動できます。

You may also pick any file and add "root" level hooks, for example add beforeEach() outside of describe()s then the callback will run before any test-case regardless of the file its in. This is because Mocha has a root Suite with no name.

これを使用して Express サーバーを 1 回起動します (また、開発サーバーとは異なるポートで実行されるように環境変数を使用します)。

before(function () {
  process.env.NODE_ENV = 'test';
  require('../../app.js');
});

(require は同期的であるため、ここでは必要ありませんdone()。) このように、このルートレベルのbefore関数を含む異なるテスト ファイルの数に関係なく、サーバーは 1 回だけ起動されます。

次に、以下も使用して、開発者のサーバーを nodemon で実行し続け、同時にテストを実行できるようにします。

  if (process.env.NODE_ENV === 'test') {
    port = process.env.PORT || 3500; // Used by Heroku and http on localhost
    process.env.PORT = process.env.PORT || 4500; // Used by https on localhost
  }
  else {
    port = process.env.PORT || 3000; // Used by Heroku and http on localhost
    process.env.PORT = process.env.PORT || 4000; // Used by https on localhost
  }
于 2013-10-08T09:50:34.033 に答える
1

これに適したsupertest github.com/visionmedia/supertestというモジュールを使用しています。

于 2013-10-15T06:07:55.843 に答える