2016-08-25 3 views
1

私は、Mocha/Chaiを使って分度器テストスイートに取り組んでいます(そして、はい、ジャスミンで同じ問題が発生します)。なぜMochaは.thenステートメントで見つかったテストを実行しませんか?

アプリがかなり複雑なので、私は乾いた状態でテストスイートをセットアップして、操作を機能に連鎖させることができます。 (つまり、ログインしてから[parameterX]を参照し、[parameterY]を参照し、最初の投稿のタイトルを[parameterZ]にしてください)

しかし、私はMochaにテストを実行させることに問題があるようです私は.then()文の中に置く。

をここに私が意味するの挙動を示す小さなコードスニペットがあります。

var chai = require('chai'); 
var cAP = require('chai-as-promised') 
chai.use(cAP); 
const expect = chai.expect; 

const doTest = (x) => { 
    describe('main', function() { 
    it('should return foo', function(done) { 
     expect(x).to.equal('foo') 
    }) 
    }) 
} 

const wait =() => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(), 10000) 
}) 

wait() 
    .then(() => doTest('foo')) 

/* results in 
* 0 passing (4ms) 
*/ 
+0

...私が好む理由である、私は神経質にとまったく同じとは全く異なりますちょうど不思議なことに、あなたがここに持っているすべての暗黙的なリターンと、記述ブロックに全体をラップした後にそれを実行しようとしましたか? – Pytth

+0

暗黙のリターンは、私がこれをやっている理由です。 –

答えて

1

describeitブロックが非同期で実行されない場合があります。あなたは10秒を待ちたい場合テストを実行する前に、非同期実行をサポートするbeforeブロックを使用する必要があります。beforeコールバックには、1つのarg(doneコールバック)が必要です。このブロックの中で10秒待ってから完了してください。その後のすべてのブロックは、donebeforeブロック内から呼び出されるまで待機します。

describe('your tests',() => { 
    before(done => { 
     setTimeout(done, 10000) 
    }) 
    it('should return foo',() => { 
     expect(x).to.equal('foo') 
    }) 
}) 
+0

私はそれを恐れていました。 "beforeAll"ブロックを使用する際の問題は、基本的に同じテストを実行する前に、別の非同期の "beforeAll"ルートが別のページにある可能性があることです。 記述方法があり、非同期に互換性がありますか? –

+0

'it'ブロックの中から非同期コードを実行することもできます(後で' done'を呼び出すこともできます)。しかし、私は '記述'と 'それは単に非同期的に呼び出すことはできませんが恐れている。その後、いつ終了するのかわからない。 –

+0

私はモカをフォークして自分のテストスイートを書く必要がありますか? –

1

問題は、テストスイートを非同期で作成することです。デフォルトでは、Mochaはスイートを非同期で作成することはできません。私はあなたのコードを取り、以下の変更を加えた:それは無用ですので

  1. itに渡されたコールバックからdoneパラメータを削除しました。

  2. doTestの末尾にrunが追加されました。

  3. --delayオプションを使用してMochaを呼び出す:mocha --delay

--delayオプションは、テストスイートを構築するのを待ちます。 run()に電話をかけてビルドを完了したことを示します。そう

var chai = require('chai'); 
var cAP = require('chai-as-promised') 
chai.use(cAP); 
const expect = chai.expect; 

const doTest = (x) => { 
    describe('main', function() { 
    it('should return foo', function() { 
     expect(x).to.equal('foo') 
    }) 
    }) 

    run(); 
} 

const wait =() => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(), 10000) 
}) 

wait() 
    .then(() => doTest('foo')) 
0

まあ、私は正確な答えを見つけることができませんでしたが、私はちょっと私は本当にのためにこれを必要とするもののために働く何かを見つけることができた、と、:ここでは、上記の変化との完全なコードですここに私がしたことがあります。

本質的に、私はテスト関数のパラメータとして渡すことができ、beforeブロックのdone()コールバックを使用して、それらが正しい時刻に実行されることを確認できます。

const wait5 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 5), 5000) 
}) 

const wait10 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 10), 10000) 
}) 

const doTest = (x, y, prep) => { 
    describe('main', function() { 
    let z; 
    before(function (done) { 
     this.timeout(30000); 
     prep(y).then((val) => { 
     z = val; 
     done(); 
     }) 
    }) 
    it('should return the right number for ' + x + ' and ' + y,() => { 
     expect(x).to.equal(z) 
    }) 
    }) 
} 

[[17, 12, wait5], [15, 5, wait10], [15, 5, wait5]].forEach((listing) => doTest(...listing)) 


/* Result: 

    main 
    √ should return the right number for 17 and 12 

    main 
    √ should return the right number for 15 and 5 

    main 
    1) should return the right number for 15 and 5 


    2 passing (20s) 
    1 failing 

    1) main should return the right number for 15 and 5: 

     AssertionError: expected 15 to equal 10 
     + expected - actual 

     -15 
     +10 
*/ 
+0

約束どおりにチャーを断念するのではなく、矢印の機能に注意してください。私は少しあなたのソリューションを取った... –

1

チャイ-AS-約束し

/** 
* Modified by cool.blue on 26-Aug-16. 
*/ 
'use strict'; 
var chai = require('chai'); 
var cAP = require('chai-as-promised'); 
chai.use(cAP); 
const should = chai.should(); 

const wait5 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 5), 5000) 
}); 

const wait10 = (something) => new Promise((resolve, reject) => { 
    setTimeout(() => resolve(something + 10), 10000) 
}); 

const doTest = (x, y, prep) => { 
    describe('main', function() { 
    this.timeout(15000); 
    it('should return the right number for ' + x + ' and ' + y, function() { 
     return prep(y).should.become(x) 
    }) 
    }) 
}; 

[[17, 12, wait5], [15, 5, wait10], [15, 5, wait5]].forEach((listing) => doTest(...listing)); 

それが少しきれいにする。しかし、矢印の機能に注意することができます。彼らは異なった文と式を扱います。

it('should return the right number for ' + x + ' and ' + y, 
    () => prep(y).should.become(x) 
    ) 

it('should return the right number for ' + x + ' and ' + y,() => { 
    prep(y).should.become(x) 
}) 

しかし

it('should return the right number for ' + x + ' and ' + y,() => { 
    return prep(y).should.become(x) 
}) 

アロー機能が

it('should return the right number for ' + x + ' and ' + y, function() { 
    return prep(y).should.become(x) 
}) 
関連する問題