2017-11-18 189 views
2

我有幾個具有通用接口的類。我想編寫一個Jest測試套件並將其應用於所有類。理想情況下,它不應該混合在一個測試模塊中,而是我希望將該套件導入到每個類的每個單獨測試模塊。如何使用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) 
    }) 
})