swc icon indicating copy to clipboard operation
swc copied to clipboard

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

Open jackyef opened this issue 3 years ago • 10 comments

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.

jackyef avatar Sep 30 '21 10:09 jackyef

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?

useafterfree avatar Oct 20 '21 23:10 useafterfree

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

caribou-code avatar Feb 23 '22 09:02 caribou-code

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 avatar Mar 15 '22 00:03 kj455

@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 avatar May 05 '22 16:05 caribou-code

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

caribou-code avatar Jul 05 '22 16:07 caribou-code

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

wight554 avatar Jul 26 '22 13:07 wight554

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

damiangreen avatar Aug 11 '22 12:08 damiangreen

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?

wight554 avatar Aug 11 '22 12:08 wight554

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

emmmarosewalker avatar Aug 15 '22 07:08 emmmarosewalker

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

RobinTail avatar Sep 17 '22 21:09 RobinTail

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

aleksandr-sidorov avatar Jan 18 '23 12:01 aleksandr-sidorov

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

b-smets avatar Jan 04 '24 18:01 b-smets

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

Please see https://github.com/swc-project/swc/issues/5205

kdy1 avatar Jan 31 '24 04:01 kdy1

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-bot avatar Mar 01 '24 12:03 swc-bot