I have few classes that have common interface. I would want to write a Jest test suite once and apply it to all the classes. Ideally it should not be mixed up in one test module, instead I expect this suite to be imported to each individual test module for each class.
Could someone please point me out to a project where something like this is done or provide an example? Thanks.
I found this article that might be helpful: https://medium.com/@walreyes/sharing-specs-in-jest-82864d4d5f9e
The idea extracted:
// 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)
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With