2017-08-15 9 views
-5
mocha.js: 3.4.2 
chai.js: 4.1.0 
node.js: 6.10.3 
npm:  5.3.0 

後から動作しません。テストハーネスはmocha.js/chai.jsに依存しています。 httpsでエンドポイントを呼び出す各テストは機能します。同様に、before()after() 関数が呼び出されます。ただし、after()関数内のRESTfulエンドポイントのいずれも起動することは不可能です。どうして?モカ/ chai.jsテストハーネス:httpsの呼び出しは、私がnode.js.の下で展開されたRESTfulサービスのための簡単なテストハーネスを設定した()関数

after()がテストの最後に来ているので、私はRESTfulエンドポイントの非同期呼び出しが、テストプロセスが終了する前に戻る時間がないと考えました。だから、応答時間を与えるためにbusy-waitループをコード化しました。いいえ:エンドポイント呼び出しは単にafter()では機能しません。ここで

は私 package.jsonです:

{ 
    "name": "so-mcv-example", 
    "version": "1.0.0", 
    "description": "Minimum, Complete, and Verifiable Example", 
    "main": "index.js", 
    "dependencies": { 
     "restify": "^4.3.0" 
    }, 
    "engines": { 
     "node": "6.10.3", 
     "npm": "5.3.0" 
    }, 
    "devDependencies": { 
     "chai": "^4.1.0", 
     "chai-http": "^3.0.0", 
     "mocha": "^3.4.2" 
    }, 
    "scripts": { 
     "test": "mocha" 
    } 
} 

index.jsは、次のようになります。

const restify = require('restify'), 
      fs = require('fs'); 

const server = restify.createServer({ 
    certificate: fs.readFileSync('security/server.crt'), 
    key: fs.readFileSync('security/server.key'), 
    name: 'so-mcv-example' 
}); 

server.listen(443, function() { 
    console.log('%s listening at %s', server.name, server.url); 
}); 

server.pre(restify.pre.userAgentConnection()); 

server.get('/status', function(req, res, next) { 
    res.send({ status: 'so-mcv-example is running ok.' }); 
    return next(); 
}); 

testディレクトリの下に、私が持っているmocha.opts

--require ./test/test.bootstrap 

test.bootstrap.js

process.env.NODE_ENV = 'test'; 

// CAUTION: never do this in production! For testing purposes, we ignore 
// faults in the standard SSL handshaking. 
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; 

後者の設定がindex.jsで使用自己署名SSL証明書を交渉します。 test/status-test.jsでは、私が持っている:

const chai = require('chai'); 
const expect = chai.expect; 
const should = chai.should(); 
const server = 'https://localhost'; 

chai.use(require('chai-http')); 

before(function() { 
    if (process.env.NODE_ENV != "test") { 
     this.skip(); 
    } 

    // XXX Wipe the database. 
    console.log("Wipe the database BEFORE tests."); 
}); 

describe('GET /status', function() { 
    it('passes, as expected', function(done) { 
     chai.request(server) 
     .get('/status') 
     .end(function(err, res) { 
      expect(err).to.be.null; 
      expect(res).to.have.status(200); 
      done(); 
     }); 
    }); 
}); 

after(function() { 
    if (process.env.NODE_ENV != "test") { 
     this.skip(); 
    } 

    chai.request(server) 
    .get('/status') 
    .end(function(err, res) { 
     expect(err).to.be.null; 
     expect(res).to.have.status(200); 
     console.log("2nd call to /status returned."); 
     done(); 
    }); 
}); 

確かに、after()機能でエンドポイントを呼び出すためにchaiを使用することが不自然です。それはポイントではありません。要点は、console.log()ディレクティブは決して実行されないということです。

> [email protected] test /Users/charlie/workspace/so-mcv-mocha 
> mocha 



Wipe the database BEFORE tests. 
    GET /status 
    ✓ passes, as expected (44ms) 


    1 passing (56ms) 

console.log()が実行されていない理由についての説明のために以下に私の答えを参照してください:それは私がnpm testを実行すると、結果は、です。

は、問題を解決するには、私はmocha.optsにオプション--no-exitを追加しました:

> [email protected] test /Users/charlie/workspace/so-mcv-mocha 
> mocha 



Wipe the database BEFORE tests. 
    GET /status 
    ✓ passes, as expected (43ms) 


    1 passing (55ms) 

2nd call to /status returned. 
+2

あなたのテストコードを確認すると便利だと思います。 –

答えて

-1

私は、私が発見したと信じて:私はnpm test実行したときに

--require ./test/test.bootstrap 
--no-exit 

を次に、所望の結果が出力されます溶液。 after()では、テスト環境の外部のサービスで非同期に一部のエンドポイントを起動すると、その非同期呼び出しはキューに入れられ、直ちに実行されません。これは、node.jsの非同期関数を呼び出す性質です。さて、after()コールの後にロジックがない場合は、process.exit()を呼び出すと、mochaがすぐに終了します。結果として、外部エンドポイントへの非同期呼び出しはキューに入れられ、呼び出されません。

解決方法:testディレクトリのmocha.opts内にある場合は、オプション--no-exitをインストールしてください。

+0

'--no-exit'を使うよりも良いオプションがあるかもしれませんが、質問に[mcve]を含めるように編集するまでは分かりません。 – Louis

+0

あなたのご要望に合わせて、私はノートに例を追加しました。私の質問にポイントを追加してください。そして、あなたがより良い答えを出すならば、私はそれを見直してそれに応じてスコアをつけます。 –

関連する問題