jest
jest copied to clipboard
[Bug]: @jest/globals pollutes global namespace with node types
Version
29.7.0
Steps to reproduce
- Clone my repo at https://github.com/jfirebaugh/jest-globals-bug
- Run
npm i && npm run check
Expected behavior
Should typecheck successfully.
Actual behavior
> [email protected] check
> tsc
browser.test.ts:4:11 - error TS2322: Type 'Timeout' is not assignable to type 'number'.
4 const timeout: number = setTimeout(() => {}, 0);
~~~~~~~
Found 1 error in browser.test.ts:4
Additional context
This issue precludes the use of @jest/globals in any project that wants to test browser libraries written in TypeScript, or any other environment where node global types would conflict with other global types (typically DOM types). It most often surfaces as a type error in code that assigns the result of setTimeout to a number-typed variable, which will fail if the node global types (from the @types/node package) have been added to the environment. In DOM type definitions setTimeout returns number but in node type definitions it returns NodeJS.Timeout. Other conflicts exist, e.g. setInterval has a similar issue.
Via the output of npm run check -- --traceResolution | grep -C10 @types/node/, you can locate the sources of global type pollution via @jest/globals:
@jest/environment/build/index.d.tshas/// <reference types="node" />@jest/types/build/index.d.tshas/// <reference types="node" />
Environment
System:
OS: macOS 13.6.3
CPU: (10) arm64 Apple M1 Max
Binaries:
Node: 18.17.1 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 9.6.7 - /usr/local/bin/npm
pnpm: 7.33.6 - /usr/local/bin/pnpm
npmPackages:
jest: ^29.7.0 => 29.7.0
See https://github.com/jestjs/jest/issues/14418 for a similar recent bug. The same reasoning in https://github.com/jestjs/jest/issues/14418#issuecomment-1679888967 applies here.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale.