Skip to content

[Bug]: Blank error message with dynamic imports when TypeScript errors are present in imported module #15924

@loucadufault

Description

@loucadufault

Version

29.7.0

Steps to reproduce

  1. 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'
  }
}
  1. 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()
  })
})
  1. tsconfig.json must have:
{
  "compilerOptions": {
    "noUnusedLocals": true
  }
}
  1. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions