2017-11-18 13 views
2

私は、共通のインターフェイスを持つクラスはほとんどありません。 Jestテストスイートを一度書いて、それをすべてのクラスに適用したいと思います。理想的には、1つのテストモジュールで混同されるべきではなく、このスイートを各クラスの各テストモジュールにインポートすることが期待されます。Jestを使用して共有テストケースを実装する方法は?

誰かがこのようなことが行われたプロジェクトを指摘したり、例を挙げたりできますか?ありがとう。

答えて

1

私は役立つかもしれない、この記事を見つけた:https://medium.com/@walreyes/sharing-specs-in-jest-82864d4d5f9e

抽出アイデア:

// shared_examples/index.js 

const itBehavesLike = (sharedExampleName, args) => { 
    require(`./${sharedExampleName}`)(args); 
}; 

exports.itBehavesLike = itBehavesLike; 

&

// aLiveBeing.js 

const sharedSpecs = (args) => { 
    const target = args.target; 

    describe("a Live Being",() => { 
    it("should be alive",() => { 
    expect(target.alive).toBeTruthy(); 
    }) 
    }) 

} 

module.exports = sharedSpecs 

&

// Person.spec.js 

const { itBehavesLike} = require('shared_examples'); 

describe("Person",() => { 
    context("A Live Person",() => { 
    const person = new Person({alive: true}) 
    const args = {target: person} 
    itBehavesLike("aLiveBeing")(args) 
    }) 
}) 
関連する問題