lisaogren / axios-cache-adapter

Caching adapter for axios. Store request results in a configurable store to prevent unneeded network requests.
MIT License
726 stars 107 forks source link

axios-cache-adapter causes leaks in unit tests when running jest with `--detect-leaks` #212

Open ahayes91 opened 4 years ago

ahayes91 commented 4 years ago

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

  1. Can we find out what code in the module is causing the leakage & fix it / have you seen this before?
  2. 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',
  ], 
...
ghost commented 3 years ago

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