2017-11-18 117 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) 
    }) 
})