【发布时间】:2021-05-02 16:16:38
【问题描述】:
我有几个具有通用接口的类。我想编写一次 Jest 测试套件并将其应用于所有类。理想情况下,它不应该混在一个测试模块中,而是希望将此套件导入到每个类的每个单独的测试模块中。
有人可以指出一个完成类似事情的项目或提供一个例子吗?谢谢。
【问题讨论】:
标签: javascript testing jestjs
我有几个具有通用接口的类。我想编写一次 Jest 测试套件并将其应用于所有类。理想情况下,它不应该混在一个测试模块中,而是希望将此套件导入到每个类的每个单独的测试模块中。
有人可以指出一个完成类似事情的项目或提供一个例子吗?谢谢。
【问题讨论】:
标签: javascript testing jestjs
我发现这篇文章可能会有所帮助: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", () => {
describe("A Live Person", () => {
const person = new Person({alive: true})
const args = {target: person}
itBehavesLike("aLiveBeing")(args)
})
})
【讨论】:
describe。修好了。