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

[Bug]: jest.mock calls are not hoisted in CommonJS test suites #4280

Open
JonathanHolvey opened this issue Mar 18, 2024 · 0 comments
Open

[Bug]: jest.mock calls are not hoisted in CommonJS test suites #4280

JonathanHolvey opened this issue Mar 18, 2024 · 0 comments

Comments

@JonathanHolvey
Copy link

Version

29.1.2

Steps to reproduce

Clone my repo and run the demo tests

git clone https://github.com/JonathanHolvey/ts-jest-no-hoist.git
cd ts-jest-no-hoist
npm clean-install
npm run test

This repo contains two test suites. One where jest.mock('uuid') is called before the module is imported using require('uuid'), and one where it is called after. This second test suite will require the mock call to be hoisted before the import for the auto-mock to work.

Expected behavior

Both demo tests should pass. Auto mocks using jest.mock are hoisted above the module import.

Actual behavior

Hoisting does not work if the test suite is a CommonJS module. The jest.mock must appear above the module import or the module will not be mocked, and use of any mock functions will cause the test suite to fail.

CommonJS modules obviously aren't the focus of this project, but I have legacy tests written in JavaScript that won't be migrated to TypeScript for a while. Moving the jest.mock calls to the top of the file is a reasonable workaround, but I want to check if the hoisting is intended to work before I make changes.

Debug log

{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"29.1.2"},"message":"creating jest presets not handling JavaScript files","sequence":1,"time":"2024-03-18T05:48:13.170Z"}
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"29.1.2"},"message":"creating jest presets not handling JavaScript files","sequence":2,"time":"2024-03-18T05:48:13.188Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"29.1.2"},"message":"creating Importer singleton","sequence":3,"time":"2024-03-18T05:48:13.877Z"}
{"context":{"actualVersion":"29.7.0","expectedVersion":">=29.0.0 <30","logLevel":20,"namespace":"versions","package":"ts-jest","version":"29.1.2"},"message":"checking version of jest: OK","sequence":4,"time":"2024-03-18T05:48:13.901Z"}
{"context":{"logLevel":20,"namespace":"ts-jest-transformer","package":"ts-jest","version":"29.1.2"},"message":"created new transformer","sequence":5,"time":"2024-03-18T05:48:13.901Z"}
{"context":{"logLevel":20,"namespace":"ts-jest-transformer","package":"ts-jest","version":"29.1.2"},"message":"created new transformer","sequence":6,"time":"2024-03-18T05:48:13.918Z"}
{"context":{"logLevel":20,"namespace":"ts-jest-transformer","package":"ts-jest","version":"29.1.2"},"message":"created new transformer","sequence":7,"time":"2024-03-18T05:48:14.299Z"}
{"context":{"logLevel":20,"namespace":"ts-jest-transformer","package":"ts-jest","version":"29.1.2"},"message":"created new transformer","sequence":8,"time":"2024-03-18T05:48:14.455Z"}

Additional context

No response

Environment

System:
    OS: Windows 10 10.0.19045
  Binaries:
    Node: 20.2.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.6.6 - C:\Program Files\nodejs\npm.CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant