Open AKhalil609 opened 2 years ago
I hit the same issue. I believe something like https://github.com/JustinBeckwith/retry-axios/pull/243 would fix the root cause but has been closed.
As a workaround (not ideal) you can mock out retry-axios
package in your jest tests by adding a mock retry-axios.ts
file like this:
export function attach() {}
Then in your jest.config.js
add a section like:
moduleNameMapper: { '^retry-axios$': '<rootDir>/path-to-my-mock-file/retry-axios.ts' },
Note this means that retry-axios
functionality is totally overridden in your tests. If your tests depend on this they will no longer work. You can either add what you need to the mock file above or find another way to do this.
I think there may be a better way to fix this like transformIgnorePatterns
and transform
in https://jestjs.io/docs/configuration#transformignorepatterns-arraystring but I spent a bit of time trying things and couldn't get it to work - if anyone does please add it below 😄
Ideally retry-axios
wouldn't require us to jump through these hoops - I suspect this will end up driving people away from this package.
Yeah I mean if folks want to submit a PR to better support jest, I'm all for it! I just don't really use this package or jest at this point, so it's unlikely something that I'll dig into.
Hi @JustinBeckwith - thank you for your response and for your work on this package.
From what I understand, the linked PR https://github.com/JustinBeckwith/retry-axios/pull/243 would fix this for jest, as by default jest assumes that packages in node_modules
are CommonJS.
by default jest assumes that packages in node_modules are CommonJS.
.... seriously!? I obviously want to make sure folks can test with this library, but again - CommonJS is a step backwards and not something we should be trying to support (in the general) sense. I won't go an ESM rant here, but I find it super hard to believe Jest wouldn't be able to support ESM 😆
I believe there's a way to override the default Jest behaviour with transformIgnorePatterns
but I couldn't get it to work.
jest support for ESM is experimental, and you must run node with --experimental-vm-modules
for it to work.
I've been having trouble with imports failing when running tests on the file that has
retry-axios
imported. This error only appears when running tests with jest ever since jest v28 update. I get no errors when building or running the project.` FAIL tests/core/HttpClient.spec.ts ● Test suite failed to run
Test Suites: 1 failed, 3 passed, 4 total Tests: 9 passed, 9 total Snapshots: 0 total Time: 9.129 s Ran all test suites. error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.`
I tried using a resolver similar to the one used here https://github.com/microsoft/accessibility-insights-web/pull/5421/commits/9ad4e618019298d82732d49d00aafb846fb6bac7 but it didn't work.
jest.config.json file
babel.config.ts file
tsconfig.spec.json