Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest globalTeardown not run after test is failed

I am trying to start and stop serverless application through code. I am able to start and stop it once all tests pass. However when test fails globalTeardown do not run. You can check sample project here: 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.
like image 420
Bilal Shah Avatar asked Jan 18 '20 20:01

Bilal Shah


Video Answer


2 Answers

Just eyeballing the docs it looks like your problem is in your jest.config.js when you set it to bail: true

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

the docs say that if bail is true it's the same as making the tests stop after the first failure.

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

I would try changing bail: 0 (the default), and seeing if it produces your expected behavior.

like image 97
安杰帅 Avatar answered Sep 19 '22 12:09

安杰帅


What you can do is add create a script containing the afterAll function:

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

And add the script to the setupFiles or setupFilesAfterEnv. In my case, I ejected one react poc code that had failing tests:

In package.json's Jest config there was this entry:

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

So I added the clause in setupTests.js below is the edited file:

// 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");
});

Now, when I ran my tests this is the result:

 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.

You can see that I ran is there even after failing a test. This is an alternative that you may use, as you have put a bounty I thought maybe solving the problem is more important that why globalTeardown is not working.

like image 21
Aritra Chakraborty Avatar answered Sep 21 '22 12:09

Aritra Chakraborty