Jest globalTeardown not run after test is failed

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.


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.