Vitest: link libraries with `linkerJitMode: true` and define `ngJitMode` as `true`, so TestBed overrides keep NgModule scopes

Open
#28 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
angular, typescript, vite

Research direction

Start by reading the linker setup in dist/linker.js and the Vite config code that defines ngJitMode; the issue says both settings need to work in Vitest. Trace how the plugin identifies test mode and review the related JIT compatibility work in #27 for a shared option. Done means Vitest keeps NgModule scopes available after TestBed overrides, with the behavior gated by the plugin's test mode or shared option.

Written by the indexing model from the issue text.

Description

feature

Part of #27.

Problem

TestBed.overrideComponent, overrideModule and overrideTemplate recompile the class with JIT. JIT needs the NgModule scopes a library declares (ɵɵsetNgModuleScope).

  • The linker runs with linkerJitMode: false, which strips those scopes from linked libraries.
  • ngp defines ngJitMode as 'false', which strips (… || ngJitMode) && ɵɵsetNgModuleScope(…) from compiled app NgModules.

After an override the component compiles with nothing in scope, and every element from an imported NgModule is "not a known element".

Proposed change

Under Vitest, link with linkerJitMode: true (dist/linker.js) and define ngJitMode: 'true'. Tests load the JIT compiler through @angular/core/testing anyway, so this costs nothing at runtime.

Our patch gates this on process.env.VITEST, which is the wrong switch for a library. In ngp it belongs in its test mode, or behind one option shared by the JIT-compatibility changes in #27 (for example test: { jitCompat: true }).

Today: linkerJitMode is a yarn patch on 0.3.0; ngJitMode is a define override in our Vite config.

Setup: ngp 0.3.0, @angular/compiler/compiler-cli 22.3.0-next.4, Vite 8, Vitest 5.1.0 (pool: 'forks'), a Nx monorepo with 1,035 Angular projects, one Vitest config each, specs written for TestBed under JIT (Analog).

Dominant language
TypeScript
Stars
7
Forks
1
Avg merge
12h 24m
Merged PRs (30d)
13

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from push-based/vite-plugin-ngp

All issues in push-based/vite-plugin-ngp

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.