Skip to content

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

@desout

Description

@desout

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)

https://play.swc.rs/?version=1.7.26&code=H4sIAAAAAAAAA0vOzysuUcjNT84OSyzKTEzKSVWwVchKLS7RS8vT0NQDSQSllpQW5YUl5pSmahgZGBloWnOBFYDkNNRLgEx1HQUNTQVbO4VqLgUgKAJr0IBw0lNLQoBKrOCGQlRCZUGgBCyN7AQdsFytpqYOV61mraY1ABQS7tClAAAA&config=H4sIAAAAAAAAA2VRu27DMAzc%2FRWG0DGI93xBl4ydiiJgZCaRoRdIOg8E%2FvdKjqMqqRaJx7sjRd6btlUfrE%2FoQG1adRKJvOk6vug1cfdIrAcOXq0ylcNIGrcQObONt8bjI4P%2BnKB7k97TDAysZ6BNRznjzeFW4oTo4CIhZ58DWMbVM%2BHAHy0%2B4RmdHkmFV0HyYD%2FRRqQsFRoXpbIhML66qT0wfpHNva47tYARiJHqXvjmBa6ZhdoBazJRVGlo4OtS6aUZIfB8CORqpx51IJBAWxToQeDf9whBSyWZRWe0ITr0UhcqpRLlZPoevdpUqgH5nf42rDQjk03B1i1GOx6Nz8P7LmZ%2FrzyNi965UXZ64F0yCSRchpHPfSrBT1PfU9m9C%2F0473BZv9xijvLSXfADq8xspl9K98GzfgIAAA%3D%3D

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions