2016-09-16 20 views
6

つの質問:テストのNode.jsのAPIここ

1)冗談はNode.jsのをテストするための良いオプションです()APIを表現しますか?

2)私はMockgooseでJestを使用しようとしていますが、後で接続を確立してテストを実行する方法を理解できません。ここに来る前に私の最後の試みです:

const Mongoose = require('mongoose').Mongoose 
const mongoose = new Mongoose() 
mongoose.Promise = require('bluebird') 
const mockgoose = require('mockgoose') 

const connectDB = (cb) =>() => { 
    return mockgoose(mongoose).then(() => { 
    return mongoose.connect('mongodb://test/testingDB', err => { 
     if (err) { 
     console.log('err is', err) 
     return process.exit() 
     } 
     return cb(() => { 
     console.log('END') // this is logged 
     mongoose.connection.close() 
     }) 
    }) 
    }) 
} 

describe('test api', connectDB((end) => { 
    test('adds 1 + 2 to equal 3',() => { 
    expect(1 + 2).toBe(3) 
    }) 
    end() 
})) 

エラーはYour test suite must contain at least one testです。このエラーは私にはちょっと意味がありますが、解決方法を理解することはできません。助言がありますか?

出力:

Test suite failed to run 

Your test suite must contain at least one test. 

答えて

1

非常に遅く答えが、私はそれが助けいただければ幸いです。 注意を払うと、記述ブロックにはテスト機能がありません。

実際にテスト関数は、説明するために渡されるコールバックの内部にあります。種類は、スタックは矢印関数コールバックのために複雑です。

Mongoのための接続がいずれも存在しないように、それは失敗し、冗談は最初説明内側「テスト」を見つけるために、関数を走査するとき、非同期であるので、この例のコードは、同じ問題..

describe('tests',function(){ 
    function cb() { 
    setTimeout(function(){ 
     it('this does not work',function(end){ 
     end(); 
     }); 
    },500); 
    } 
    cb(); 

    setTimeout(function(){ 
    it('also does not work',function(end){ 
     end(); 
    }); 
    },500); 
}); 

を生成します。 それはそれのように見えないかもしれませんが、それはまさにあなたがやっていることです。
このケースでは、あなたのソリューションは少し賢明だった(それはうまくいきませんでした)。簡単なステートメントに分割すると、この問題を突き止めることができました。

関連する問題