最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to implement shared test cases using Jest? - Stack Overflow

programmeradmin4浏览0评论

I have few classes that have mon 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 have few classes that have mon 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.

Share Improve this question asked Nov 18, 2017 at 0:59 Dmitry DruganovDmitry Druganov 2,3683 gold badges24 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

I found this article that might be helpful: https://medium./@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)
  })
})
发布评论

评论列表(0)

  1. 暂无评论