-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jest.mock
is not hoisted if jest is imported from @jest/globals
#310
Comments
Imports are hoisted because you used ESM syntax |
@kdy1 Can you explain in a bit more detail? I am not sure I understand what you meant. The input file is a typescript file. I am expecting it to be compiled to CJS. I was expecting
|
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. |
This matter may be beyond the scope of the ESM specification. ESM is the standard we adhere to. Additionally, our plugin has also done similar things, but it does not cover everything. plugins/packages/jest/src/lib.rs Line 60 in 7aca008
So this should be a technically feasible solution. We can fix this issue in the swc jest plugin. |
The core issue lies here: In Babel, the passes are structured like an onion model, allowing plugins to define both pre and post phases. In SWC, passes follows the pipeline model, and re-entering a plugin after the import phase (CommonJS) is a challenge. |
**Description:** - We have two `jest` pass. One in `@swc/core` (via _hidden.jest flag), and one in the plugin. This PR fixes only the core one. However, the linked issue is wrong because the code tries to break the rules of ESM specification. So, although this PR closes the issue, the final form is not the issue the author wanted. **Related issue:** - Closes swc-project/plugins#310
Describe the bug
If I configure jest to not inject globals (
injectGlobals: false
) and instead import it explicitly:import { describe, expect, it, jest } from '@jest/globals';
Calls to
jest.mock(...)
are no longer hoisted to the top of the file.Input code
Config
Playground link
https://play.swc.rs/?version=1.3.10&code=H4sIAAAAAAAAA2WQT0%2FDMAzF7%2FkUvtURpdsZNMQB7pPghjikqVcFkqZLXMQ09buTpBrlT6Qcnp%2F9%2FEuMG31gOEMkfjaO%2FMQwwyF4BxUnHeJmTMJEitWtEObS3lHUwbRUA32OpLkGk%2B4bxXX8PqtNb32rbBnOunFev%2BO%2F6BpQwu4O8CzgB8pNSWwOAy72fulvAkVvPwilFLOUKfqCg1WcDNN3Xo4zjJU7XXNK%2BlUH0H5IvOxZ2Qey6gS7sq94yym41OGKJP%2B6jVbWxoTUTZoQVd%2FXkEtlT1JwBZj1y%2FYVVIRhci0FWcM2ceeQ5f9wpZAN%2BycORvPjcVIWl8Y5P1V8AS8JMp6vAQAA&config=H4sIAAAAAAAAAz2QMW7DMAxF70J0DOw2o9d06ZAuRQ7AKowj16IEkkJrGL57abvp9vn4%2BSVyhicNd0oIHdzNinZtO2jmZqdqWajJ0rf6HSTAAQYN0M1QUJRkVTqx4Y%2BP21RIg8RibjN1ZFLpANeJMcXwlkoW2%2BHiBkHWW5a0ZghhsE1UtpjI07BaTmgxwOLuT1S6yOi8add0lJ58AEiPz8cXJ19E5TSi6jsm0v9XNFcJdMbiCCKPkcnNu%2FjYenrKbMT2%2BG3Uc77WkR51%2BqvmbT9PCTmlzIOuOT37eV73%2FaC74ai0LL%2BwXm0pUwEAAA%3D%3D
Expected behavior
Compiled output to match
ts-jest
:jest.mock(...)
call should be hoisted to the top of the file together with its import statement.Actual behavior
The code is compiled as is, and
jest.mock(...)
calls are not hoisted. This prevents mocks from working, since the non-mocked module is imported before jest had a chance to mock it.Version
1.3.10
Additional context
When there are no imports from
@jest/globals
, compiled output fromswc
is correct -jest.mock(...)
calls are hoisted to the top of the file.The text was updated successfully, but these errors were encountered: