fix(jest-cjs): add allowJs for override-for-dev#87
Conversation
|
Could you please edit the description of this PR why this change is needed and why allowJs fixes it? |
ok, will do later as I then need to find out why this fixes it |
|
If you are unable to determine the reason, then just state:
|
it's not that I don't want to or I'm lazy, but a wrong computer and no time issue |
|
@ST-DDT ping |
There was a problem hiding this comment.
Can you change the PR title to mention overrides-for-dev?
e.g. fix(jest-cjs): add allowJs for override-for-dev
I'm not convinced that the explanation in the description is true, as our shipped code is js as well.
But I dont want to block this PR for minor reasons.
relates to faker-js/faker#3558
when running
pnpm run allwithout this change, the command fails withWhy this fix is needed
The Jest config in
jest-cjsusests-jestto transform files matching^.+\.(t|j)s$— i.e. both.tsand.jsfiles. ThetransformIgnorePatternsis configured to let@faker-jspackages through, so Jest asksts-jestto transform faker's ESM source files (which are.jsfiles usingimport/exportsyntax).However,
ts-jestdelegates to the TypeScript compiler, and TypeScript will not process.jsfiles unlessallowJs: trueis set intsconfig.json. Without it,ts-jestsilently skips the transformation of faker's.jsfiles. The untransformed ESMimportstatements then hit Node's CJS runtime, causing:SyntaxError: Cannot use import statement outside a module
The fix
Adding
"allowJs": truetotsconfig.jsontells TypeScript (and thereforets-jest) to actually compile/transform.jsfiles, so faker's ESM code gets properly transpiled to CJS before Jest executes it.TL;DR
allowJsallowJsts-jestreceives.jsfileimport { ... } from "..."const { ... } = require("...")SyntaxError