【问题标题】:Jest globalTeardown not run after test is failed测试失败后不运行 Jest globalTeardown
【发布时间】:2020-05-05 08:33:43
【问题描述】:

我正在尝试通过代码启动和停止无服务器应用程序。一旦所有测试通过,我就可以启动和停止它。但是,当测试失败时 globalTeardown 不要运行。您可以在此处查看示例项目:https://github.com/bilalsha/sls-test-jest/tree/fail_test

teardown.js

module.exports = async function() {
    let slsOfflineProcess = global.__SERVERD__;
    slsOfflineProcess.stdin.write('q\n');
    slsOfflineProcess.stdin.pause();
    await slsOfflineProcess.kill('SIGINT');
    console.log('Serverless Offline stopped');
};

output

      7 |              expect(res.statusCode).toEqual(200);
    >  8 |              expect(res.body).toEqual('Go Serverless v1.0! Your function executed successfully!');
         |                               ^
       9 |      });
      10 | });
      11 | 

      at Object.<anonymous> (handler.test.js:8:20)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.972s, estimated 2s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

【问题讨论】:

    标签: node.js testing jestjs


    【解决方案1】:

    您可以做的是添加创建一个包含afterAll 函数的脚本:

    afterAll(() => {
      console.log("I ran");
    });
    

    并将脚本添加到setupFilessetupFilesAfterEnv。就我而言,我弹出了一个测试失败的反应 poc 代码:

    package.json 的 Jest 配置中有这个条目:

    "jest": {
        ...
        "setupFilesAfterEnv": [
          "<rootDir>/src/setupTests.js"
        ],
        ...
    }
    

    所以我在setupTests.js中添加了下面的子句是编辑后的文件:

    // jest-dom adds custom jest matchers for asserting on DOM nodes.
    // allows you to do things like:
    // expect(element).toHaveTextContent(/react/i)
    // learn more: https://github.com/testing-library/jest-dom
    import '@testing-library/jest-dom/extend-expect';
    
    afterAll(() => {
      console.log("I ran");
    });
    

    现在,当我运行测试时,结果如下:

     FAIL  src/App.test.js
      ✓ renders learn react link (14ms)
      ✕ renders class (5ms)
    
      ● renders class
    
        expect(jest.fn()).toHaveBeenCalledTimes(expected)
    
        Expected number of calls: 1
        Received number of calls: 0
    
          18 |   // someClass.someProp = "";
          19 |   render(<App />);
        > 20 |   expect(track).toHaveBeenCalledTimes(1);
             |                 ^
          21 | });
          22 | 
    
          at Object.<anonymous> (src/App.test.js:20:17)
    
      console.log src/setupTests.js:8
        I ran <---------------
    
    Test Suites: 1 failed, 1 total
    Tests:       1 failed, 1 passed, 2 total
    Snapshots:   0 total
    Time:        1.352s, estimated 2s
    Ran all test suites.
    

    您可以看到I ran 即使在测试失败后仍然存在。这是您可以使用的替代方法,因为您提供了赏金,我认为解决问题可能比为什么 globalTeardown 不起作用更重要。

    【讨论】:

    • 我试过了,但是我将如何获得进程 ID 来杀死离线运行的无服务器应用程序?我无法做到这一点,所以我的测试失败并且应用程序仍在运行
    【解决方案2】:

    当您将其设置为 bail: true 时,只需查看文档,您的问题似乎出在您的 jest.config.js 中

    https://github.com/bilalsha/sls-test-jest/blob/fail_test/test/jest.config.js#L3

    文档说,如果 bail 为真,则与在第一次失败后停止测试相同。

    https://jestjs.io/docs/en/configuration#bail-number--boolean

    我会尝试更改 bail: 0(默认值),看看它是否会产生您预期的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-01
      • 2020-11-18
      • 1970-01-01
      • 2019-03-02
      • 2019-03-17
      • 1970-01-01
      • 2019-04-05
      • 2021-04-04
      相关资源
      最近更新 更多