Skip to content

refactor(schematics): use explicit jest imports in templates#2329

Closed
Generalsimus wants to merge 1 commit intonestjs:masterfrom
Generalsimus:refactor-jest-globals
Closed

refactor(schematics): use explicit jest imports in templates#2329
Generalsimus wants to merge 1 commit intonestjs:masterfrom
Generalsimus:refactor-jest-globals

Conversation

@Generalsimus
Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Currently, when generating components using the Nest CLI (e.g., nest g resource), the generated .spec.ts files rely on implicit Jest global variables (describe, beforeEach, it, expect). The default package.json schematic also installs @types/jest to support this. This often results in type errors in test files right out of the box in strictly configured environments, as the dependencies are not explicitly declared.

Issue Number: N/A

What is the new behavior?

The templates have been refactored to prioritize explicit dependencies:

  1. The package.json schematic now installs @jest/types instead of @types/jest.
  2. All generated .spec.ts files now include explicit imports at the top of the file (e.g., import { describe, beforeEach, it, expect } from '@jest/globals';).
    This eliminates type errors in newly generated components and aligns the boilerplate with strict clean-code practices by avoiding global namespace pollution.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This change only affects newly scaffolded files and projects. Existing tests using implicit globals will continue to work normally.

@kamilmysliwiec
Copy link
Copy Markdown
Member

We're about to migrate to vitest in the next release #2302

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants