Skip to content
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

Variables with names starting with mock are not considered when hoisting jest.mock() calls #5448

Closed
jackyef opened this issue Sep 30, 2021 · 14 comments

Comments

@jackyef
Copy link

jackyef commented Sep 30, 2021

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: kulshekhar/ts-jest#1088 (comment)

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.

@jackyef jackyef changed the title Variables with names starting with mock are not hoisted Variables with names starting with mock are not considered when hoisting jest.mock() calls Oct 5, 2021
@useafterfree
Copy link

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?

@caribou-code
Copy link

Just came across this problem too. Without this feature working correctly it makes certain test scenarios either very difficult or impossible.

@kj455
Copy link

kj455 commented Mar 15, 2022

As for my own workaround, I used doMock instead of mock, and it worked fine.
https://jestjs.io/docs/jest-object#jestdomockmodulename-factory-options

@caribou-code
Copy link

@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?

@caribou-code
Copy link

Couple of months later and this still seems to be a major problem with no acknowledgement...

@wight554
Copy link

this is swc/core upstream regression it seems, worked fine last time I tried (^1.2.160) @kdy1 can you please check?

@damiangreen
Copy link

thanks @wight554 can confirm reverting to "@swc/core": "1.2.160", has got me switched away from ts-jest

@wight554
Copy link

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?

@kdy1 kdy1 added this to the Planned milestone Aug 11, 2022
@kdy1 kdy1 transferred this issue from swc-project/jest Aug 11, 2022
@emmmarosewalker
Copy link

Same issue, confirming the error is gone when downgrading to "@swc/core": "1.2.160" (was on 1.2.232)

@RobinTail
Copy link

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",

@kdy1 kdy1 self-assigned this Sep 27, 2022
@aleksandr-sidorov
Copy link

aleksandr-sidorov commented Jan 18, 2023

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",

@kdy1 kdy1 removed their assignment Aug 24, 2023
@b-smets
Copy link

b-smets commented Jan 4, 2024

Any updates on this? Can confirm this is still an issue with @swc/jest 0.2.29.

@kdy1
Copy link
Member

kdy1 commented Jan 31, 2024

jest.mock('twilio', ...) does not conform to the ESM standard.

Please see #5205

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2024
@kdy1 kdy1 removed this from the Planned milestone Jan 31, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Mar 1, 2024

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests