You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"}
Version
29.1.2
Steps to reproduce
Clone my repo and run the demo tests
This repo contains two test suites. One where
jest.mock('uuid')
is called before the module is imported usingrequire('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
The text was updated successfully, but these errors were encountered: