Skip to content

ts-jest ignores injectGlobals and leaks @types/jest into all files #2411

@biro456

Description

@biro456

🐛 Bug Report

I'm writing TypeScript code and I want to use jest with injectGlobals turned off, direclty importing from @jest/global. But when using ts-jest all globals end up being available in the global context anyway.

That makes auto-complete suggest the global version and makes it possible compile any code in the project with the the globals.

Step to Reproduce

https://gist.github.com/biro456/40501e3bc024ba92cd3113a7e99d5ebe

  1. Install dependencies
  2. Run build script
  3. Run node sample.js
  4. Run script test

TypeScript will compile without errors but sample.js will throw a ReferenceError when executed.
Tests will not error though, which means injectGlobals is being ignored.

Expected behaviour

TypeScript should error when a Jest global is used in any file.

envinfo

System:
    OS: Ubuntu 20.04.2 LTS on WSL 2 on Windows 10 Pro 20H2 19042.804
    Node version.: 14.15.4

Npm packages:
    jest: 26.6.3
    ts-jest: 26.5.2
    typescript: 4.1.5

The solution would be to remove @types/jest from dependencies, allowing package users to import it only if necessary, and change ts-jest to respect injectGlobals.

I saw that it was #1859 that moved @types/jest to dependencies, so an alternative solution would be needed to avoid breaking Yarn 2 again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions