angular / angular/angular-cli

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

Đang mở
#31,993 6 bình luận 30 reaction 0 người được giao Xem trên GitHub
angular/build:unit-test area: @angular/build freq1: low severity3: broken type: bug/fix
Ngôn ngữ chính
TypeScript
Star
27k
Fork
11.8k
Merge trung bình
14 giờ 23 phút
Pull request đã merge (30 ngày)
162

Mô tả

### 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.
❯ 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 │
└───────────────────────────┴───────────────────┴───────────────────┘
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với việc xử lý providersFile của runner @angular/build:unit-test và init-testbed.js được tạo ra, sau đó tái hiện lỗi bằng repository tối thiểu được liên kết. Xác minh bản sửa bằng cách chạy ng test với TestService inject Router và xác nhận rằng tất cả các spec đều được import và pass mà không gặp lỗi trình biên dịch JIT.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
angular, typescript
Lĩnh vực
build-system, testing
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.