axios-cache-adapter causes leaks in unit tests when running jest with `--detect-leaks`
ahayes91 opened this issue · 1 comments
ahayes91 commented
This took a while to track down - we're seeing issues with the performance of tests in our monorepo when running in the CI, and after some investigation using the --detect-leaks
flag in jest, I've identified that the leaks are occurring in our use of the axios-cache-adapter
package.
Repository to reproduce this issue:
https://github.com/ahayes91/TestTimeouts
- ahayes91/TestTimeouts#2 shows that the leak is removed when running
yarn workspace @test/parentApp test --detect-leaks
if the cache functionality is commented from the functional code - ahayes91/TestTimeouts#3 shows that the leak is removed for the App.integration.test.js file if the
axios-cache-adapter
module is mocked in the test file (you can runyarn jest PATH_TO_FILE --detect-leaks
to test this one individually)
- Can we find out what code in the module is causing the leakage & fix it / have you seen this before?
- Can you advise on how best to mock the module globally for unit tests without having to put
jest.mock
in every test file? https://jestjs.io/docs/en/manual-mocks didn't seem to work for me, even when I put the manual mocking file in a__mocks__
directory beside the root level node_modules and in a__mocks__
directory beside the package level node_modules.
Edit: I've updated our jest config to load the mock in the setupFilesAfterEnv step:
// mockAxiosCache.js
/* eslint-disable no-undef */
jest.mock('axios-cache-adapter', () => {
return {
setupCache: jest.fn().mockReturnValue({ adapter: 'mockAdapter' }),
};
});
// jest.config.js
...
setupFilesAfterEnv: [
'@testing-library/jest-dom/extend-expect',
'./mockAxiosCache.js',
],
...
Deleted user commented
Hello @ahayes91
Thank you for this investigation and for uncovering this leak issue. I've been away for a while (as you probably noticed).
I will look into this as soon as I can.
Cheers