-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Describe the bug
When using jest.mock() in conjunction with variables whose names start with "mock", these variables are not correctly hoisted. This results in unexpected behavior during testing, as the mock variables are not available as expected.
In the provided example, mockVariable is defined and assigned a jest mock function. However, when it is referenced within the jest.mock() implementation, it is not hoisted correctly, leading to it being undefined.
ReferenceError: Cannot access 'mockCreate' before initialization
One more ticket about it: #5448
This ticket has been closed( resolved)
Input code
const mockVariable = jest.fn().mockReturnValue(2020);
jest.mock('test', () => {
return({
getTest: jest.fn(() => ({
test: mockVariable,
})),
})});Config
{
"$schema": "https://swc.rs/schema.json",
"sourceMaps": "inline",
"env": {
},
"jsc": {
"minify": {
"compress": false,
"mangle": false
},
"externalHelpers": true,
"loose": false,
"baseUrl": "./",
"parser": {
"syntax": "ecmascript",
"jsx": true
},
"transform": {
"decoratorMetadata": false,
"react": {
"development": true
},
"hidden":{
"jest": true
}
},
"experimental": {
"plugins": [
[
"swc_mut_cjs_exports",
{}
]
]
}
},
"module": {
"type": "commonjs"
}
}Playground link (or link to the minimal reproduction)
SWC Info output
Operating System:
Platform: darwin
Arch: arm64
Machine Type: arm64
Version: Darwin Kernel Version 24.1.0: Fri Aug 16 19:18:30 PDT 2024; root:xnu-11215.40.42~4/RELEASE_ARM64_T6000
CPU: (8 cores)
Models: Apple M1 Pro
Binaries:
Node: 20.13.1
npm: 10.5.2
Yarn: 4.1.1
pnpm: 9.1.3
Relevant Packages:
@swc/core: 1.5.24
@swc/helpers: 0.5.11
@swc/types: 0.1.7
SWC Config:
output: N/A
.swcrc path: N/A
Next.js info:
output: N/A
Expected behavior
based on jest and babel documentation, variable starting with mock should be hoisted before jest.mock
using https://github.com/jestjs/jest/blob/main/packages/babel-plugin-jest-hoist/README.md ( Included in babel-jest by default)
Actual behavior
not hoisted before jest.mock
Version
^0.2.36
Additional context
No response