私は役立つかもしれない、この記事を見つけた: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)
})
})