Version
29.7.0
Steps to reproduce
- Create a TypeScript file with an intentional error (
src/module-with-error.ts):
// This unused constant causes a TS error when noUnusedLocals is enabled
const UNUSED_CONSTANT = 'trigger-ts-error'
export class MyClass {
doSomething() {
return 'hello'
}
}
- Create a test file (
test/blank-error.spec.ts):
jest.mock('some-external-module', () => ({}))
describe('Blank error reproduction', () => {
beforeEach(() => {
jest.resetModules()
})
it('shows blank error when dynamic import has TS compilation error', async () => {
// Dynamic import after resetModules
const { MyClass } = await import('../src/module-with-error')
expect(MyClass).toBeDefined()
})
})
tsconfig.json must have:
{
"compilerOptions": {
"noUnusedLocals": true
}
}
- Run the test:
npx jest --testPathPattern="blank-error"
Expected behavior
FAIL test/example.spec.ts
Example Test Suite
✕ test name (93 ms)
● Example Test Suite › test name
src/module-with-error.ts:2:7 - error TS6133: 'UNUSED_CONSTANT' is declared but its value is never read.
22 const UNUSED_CONSTANT = 'trigger-ts-error'
~~~~~~~~~~~~~~~
Actual behavior
FAIL test/example.spec.ts
Example Test Suite
✕ test name (93 ms)
● Example Test Suite › test name
(Note the 4 blank lines where the error should be)
Additional context
Workaround is to not call jest.resetModules() in beforeEach(), or use a static import. The issue only manifests with the combination of resetModules + dynamic import.
The error seems to be thrown since the test fails, it's just not being displayed.
Environment
System:
OS: Linux 6.8 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
CPU: (5) x64
Binaries:
Node: 20.19.5 - /usr/local/bin/node
Yarn: 1.22.22 - /usr/local/bin/yarn
npm: 10.8.2 - /usr/local/bin/npm
npmPackages:
jest: ^29.5.0 => 29.7.0
Version
29.7.0
Steps to reproduce
src/module-with-error.ts):test/blank-error.spec.ts):tsconfig.jsonmust have:{ "compilerOptions": { "noUnusedLocals": true } }npx jest --testPathPattern="blank-error"Expected behavior
FAIL test/example.spec.ts Example Test Suite ✕ test name (93 ms) ● Example Test Suite › test name src/module-with-error.ts:2:7 - error TS6133: 'UNUSED_CONSTANT' is declared but its value is never read. 22 const UNUSED_CONSTANT = 'trigger-ts-error' ~~~~~~~~~~~~~~~Actual behavior
FAIL test/example.spec.ts Example Test Suite ✕ test name (93 ms) ● Example Test Suite › test name(Note the 4 blank lines where the error should be)
Additional context
Workaround is to not call
jest.resetModules()inbeforeEach(), or use a static import. The issue only manifests with the combination of resetModules + dynamic import.The error seems to be thrown since the test fails, it's just not being displayed.
Environment
System: OS: Linux 6.8 Debian GNU/Linux 11 (bullseye) 11 (bullseye) CPU: (5) x64 Binaries: Node: 20.19.5 - /usr/local/bin/node Yarn: 1.22.22 - /usr/local/bin/yarn npm: 10.8.2 - /usr/local/bin/npm npmPackages: jest: ^29.5.0 => 29.7.0