【问题标题】:How to implement shared test cases using Jest?如何使用 Jest 实现共享测试用例?
【发布时间】:2021-05-02 16:16:38
【问题描述】:

我有几个具有通用接口的类。我想编写一次 Jest 测试套件并将其应用于所有类。理想情况下,它不应该混在一个测试模块中,而是希望将此套件导入到每个类的每个单独的测试模块中。

有人可以指出一个完成类似事情的项目或提供一个例子吗?谢谢。

【问题讨论】:

    标签: javascript testing jestjs


    【解决方案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", () => {
      describe("A Live Person", () => {
        const person = new Person({alive: true})
        const args = {target: person}
        itBehavesLike("aLiveBeing")(args)
      })
    })
    

    【讨论】:

    • 'context()' 定义在哪里?
    • @devboell 谢谢,应该是describe。修好了。
    猜你喜欢
    • 2020-01-24
    • 2018-10-28
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2019-04-21
    • 1970-01-01
    相关资源
    最近更新 更多