Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.
This repository was archived by the owner on Feb 28, 2023. It is now read-only.

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

@ahayes91

Description

@ahayes91

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',
  ], 
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions