Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

`@angular/build:unit-test` with Vitest test runner throws `Failed to import test file` error

未關閉
#31,993 6 則留言 30 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
52/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
冷清
技術堆疊
angular, typescript

研究方向

先從檢查 @angular/build:unit-test runner 對 providersFile 的處理以及產生的 init-testbed.js 開始,然後使用連結的最小儲存庫重現該失敗。透過執行 ng test(其中 TestService 注入 Router),並確認所有 spec 都能在不出現 JIT 編譯器錯誤的情況下匯入並通過,來驗證修正。

由索引模型根據 Issue 內容生成。

描述

angular/build:unit-test area: @angular/build freq1: low severity3: broken type: bug/fix
Command

test

Is this a regression?
  • Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was

No response

Description

Steps to reproduce:

  1. Create a TestService that will contain a single property router = inject(Router)
  2. Configure @angular/build:unit-test runner to include providersFile
  3. Export [TestService] from it
  4. Create a Test component that will inject this service: service = inject(TestService)

ng test will fail with the following error for all specs:

FAIL   project (chromium)  path/to/some.spec.ts [ path/to/some.spec.ts ]
Error: Failed to import test file /project/root/init-testbed.js
Caused by: Error: The injectable '_PlatformLocation' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available.

The injectable is part of a library that has been partially compiled.
However, the Angular Linker has not processed the library such that JIT compilation is used as fallback.

Ideally, the library is processed using the Angular Linker to become fully AOT compiled.
Alternatively, the JIT compiler should be loaded by bootstrapping using '@angular/platform-browser-dynamic' or '@angular/platform-server',
or manually provide the compiler with 'import "@angular/compiler";' before bootstrapping.
 ❯ <static_initializer> node_modules/.vite/vitest/dc540563f7f56f2cac74b38788615874d1da1c10/deps/chunk-42LDSDVL.js?v=049c4943:83:17

If you remove injection of the TestService from the Test component all the specs will start to pass.

Minimal Reproduction

https://github.com/th0r/ng-vitest-test-providers-bug

Your Environment
Angular CLI       : 21.0.1
Angular           : 21.0.2
Node.js           : 24.8.0
Package Manager   : npm 11.6.0
Operating System  : darwin arm64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.0.1            │ ^21.0.1           │
│ @angular/cli              │ 21.0.1            │ ^21.0.1           │
│ @angular/common           │ 21.0.2            │ ^21.0.0           │
│ @angular/compiler         │ 21.0.2            │ ^21.0.0           │
│ @angular/compiler-cli     │ 21.0.2            │ ^21.0.0           │
│ @angular/core             │ 21.0.2            │ ^21.0.0           │
│ @angular/forms            │ 21.0.2            │ ^21.0.0           │
│ @angular/platform-browser │ 21.0.2            │ ^21.0.0           │
│ @angular/router           │ 21.0.2            │ ^21.0.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.0.14            │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘
主要語言
TypeScript
星號
27k
分支
11.8k
平均合併
17 小時 21 分鐘
30 天內合併 PR
181

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。