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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With