@swc/jest
does not hoist mocks if the TypeScript file imports @jest/globals
#10325
Milestone
@swc/jest
does not hoist mocks if the TypeScript file imports @jest/globals
#10325
Problem
I have a TypeScript Jest test that imports
jest
from@jest/globals
, and also mocks some modules. When compiled withswc
, the mock is not hoisted to the top of the file like it should be. Removing the import for@jest/globals
fixes the issue, but then Jest types don't work. Obviously that can be fixed by installing the@types/jest
package instead of explicitly importing the globals, but I would expect either way to work, since Jest mentions both ways in it's documentation, andts-jest
supports both methods.Reproduction
Say I have two files,
example.ts
, and its test,example.test.ts
:If I run Jest (configured to use
@swc/jest
) on the test, it fails becausedoSomething
is imported from./example
before the mock is created. This can be seen by using theswc
CLI with the same configuration that Jest uses:> npx swc -C jsc.transform.hidden.jest=true -C module.type=commonjs example.test.ts
Outputs:
Notice how the mock comes after the
require
statements.Now, if I remove the
@jest/globals
import, and run Jest again, the test passes. Using theswc
CLI we can see that the mock is properly hoisted to the top:> npx swc -C jsc.transform.hidden.jest=true -C module.type=commonjs example.test.ts
Expected Behaviour
@swc/jest
should work with TypeScript tests that explicitly import globals, the same way it works when the globals are implicit from@types/jest
.The text was updated successfully, but these errors were encountered: