swc
swc copied to clipboard
Variables with names starting with `mock` are not considered when hoisting `jest.mock()` calls
With babel-jest
it seems like jest.mock()
calls are hoisted to the top of the file. swc
seems to have this transformation as well, so no issue with that.
The problem occurs when the jest.mock()
calls depends on other variable, for instance:
const mockCreate = jest.fn().mockResolvedValue(undefined)
jest.mock('twilio', () => () => ({ messages: { create: mockCreate } }))
With babel
, this code works fine because jest
makes an exception for variables with names starting with mock
. With swc
, it seems like this isn't the case and we end up with the following error:
ReferenceError: Cannot access 'mockCreate' before initialization
Quoting this comment from ts-jest repo: https://github.com/kulshekhar/ts-jest/issues/1088#issuecomment-623033610
What happens is that when doing the hoisting for mock, the babel plugin checks variables that are being used in the mock factory to ensure that everything being used will be within scope after hoisting.
I am also running this package on large test bases and need the ts-jest style of hoisting. Any ideas on how to make this work like ts-jest?
Just came across this problem too. Without this feature working correctly it makes certain test scenarios either very difficult or impossible.
As for my own workaround, I used doMock
instead of mock
, and it worked fine.
https://jestjs.io/docs/jest-object#jestdomockmodulename-factory-options
@kj455 are you applying doMock
to each test in a file? I tried using it once at the top of a test file but it didn't do mocking at all.
There's not many issues open on this repo, but 3 or 4 of them are currently to do with this mocking problem. It seems like a serious limitation. Is there no update from the maintainers?
Couple of months later and this still seems to be a major problem with no acknowledgement...
this is swc/core upstream regression it seems, worked fine last time I tried (^1.2.160) @kdy1 can you please check?
thanks @wight554 can confirm reverting to "@swc/core": "1.2.160", has got me switched away from ts-jest
thanks @wight554 can confirm reverting to "@swc/core": "1.2.160", has got me switched away from ts-jest
That means it should be investigated, @kwonoj can you also check?
Same issue, confirming the error is gone when downgrading to "@swc/core": "1.2.160" (was on 1.2.232)
Test suite failed to run
ReferenceError: Cannot access 'compressionMock' before initialization
25 |
26 | const compressionMock = jest.fn();
> 27 | jest.mock("compression", () => compressionMock);
| ^
.swcrc:
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"dynamicImport": false,
"privateMethod": false,
"functionBind": false,
"exportDefaultFrom": false,
"exportNamespaceFrom": false,
"decorators": false,
"decoratorsBeforeExport": false,
"topLevelAwait": false,
"importMeta": false
},
"transform": null,
"target": "es2016",
"loose": false,
"externalHelpers": false,
"keepClassNames": true
},
"minify": false
}
package.json
"@swc/core": "1.3.1",
"@swc/jest": "0.2.22",
I have the same issue while migrating a jest testing project from ts-jest/babel
to swc
.
In my case the latest working versions are
"@swc/core": "1.2.205",
"@swc/jest": "0.2.24",
Any updates on this? Can confirm this is still an issue with @swc/jest 0.2.29.
jest.mock('twilio', ...)
does not conform to the ESM standard.
Please see https://github.com/swc-project/swc/issues/5205
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.