jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: @jest/globals pollutes global namespace with node types

Open jfirebaugh opened this issue 1 year ago • 34 comments

Version

29.7.0

Steps to reproduce

  1. Clone my repo at https://github.com/jfirebaugh/jest-globals-bug
  2. 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.ts has /// <reference types="node" />
  • @jest/types/build/index.d.ts has /// <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

jfirebaugh avatar Jan 04 '24 22:01 jfirebaugh

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.

jfirebaugh avatar Jan 04 '24 23:01 jfirebaugh

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.

github-actions[bot] avatar Feb 04 '24 00:02 github-actions[bot]

Not stale.

jfirebaugh avatar Feb 04 '24 02:02 jfirebaugh

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.

github-actions[bot] avatar Mar 05 '24 03:03 github-actions[bot]

Not stale.

jfirebaugh avatar Mar 05 '24 12:03 jfirebaugh

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.

github-actions[bot] avatar Apr 04 '24 12:04 github-actions[bot]

Not stale.

jfirebaugh avatar Apr 04 '24 15:04 jfirebaugh

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.

github-actions[bot] avatar May 04 '24 17:05 github-actions[bot]

Not stale.

jfirebaugh avatar May 06 '24 15:05 jfirebaugh

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.

github-actions[bot] avatar Jun 05 '24 17:06 github-actions[bot]

Not stale.

jfirebaugh avatar Jun 05 '24 17:06 jfirebaugh

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.

github-actions[bot] avatar Jul 05 '24 18:07 github-actions[bot]

Not stale.

jfirebaugh avatar Jul 05 '24 22:07 jfirebaugh

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.

github-actions[bot] avatar Aug 04 '24 23:08 github-actions[bot]

Not stale.

jfirebaugh avatar Aug 05 '24 15:08 jfirebaugh