Skip to content

Unit Test using Vitest for service class that inject HttpClient will hit JIT Compilation fail error #32095

Description

@kjteh

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

v21, No issue if using karma as test runner

Description

Refer to repo: https://github.com/kjteh/vitest-repro

This errors happens whenever there is an injection of the HttpClient. Comments out the HttpClient will able to resolve the issue.

For example: in projects\my-lib\core\my-service\my-service.ts

import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from "@angular/core";

@Injectable({ providedIn: 'root', useFactory: () => new ChildService() })
export abstract class BaseService {
  abstract foo(): string;
}

@Injectable({ providedIn: 'root' })
export class ChildService extends BaseService {

  // Comment the following injection to see the error disappear
  protected httpClient = inject(HttpClient);

  override foo(): string {
    return 'bar';
  }
}

Another the way to get rid of this error is not to use the providersFile. However, this is needed for me to override all the BaseService so that it does not depends on HttpClient when running unit test.

Minimal Reproduction

  1. Clone repo from: https://github.com/kjteh/vitest-repro
  2. Run pnpm install
  3. Run ng test my-lib

Exception or Error

stderr | projects/my-lib/ui/my-lib/my-lib.spec.ts
JIT compilation failed for injectable [class PlatformLocation]

                                                                                                                                                  
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
                                                                                                                                                  
 FAIL   my-lib  projects/my-lib/ui/my-lib/my-lib.spec.ts [ projects/my-lib/ui/my-lib/my-lib.spec.ts ]                                             
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.
 ❯ getCompilerFacade node_modules/.pnpm/@angular+core@21.0.3_@angular+compiler@21.0.3_rxjs@7.8.2/node_modules/@angular/core/fesm2022/_debug_node-chunk.mjs:143:11
 ❯ Module.ɵɵngDeclareFactory node_modules/.pnpm/@angular+core@21.0.3_@angular+compiler@21.0.3_rxjs@7.8.2/node_modules/@angular/core/fesm2022/core.mjs:2594:20
 ❯ Function.<static_initializer> node_modules/.pnpm/@angular+common@21.0.3_@ang_e9b7452ef12a5f71d197dda1c8d449ff/node_modules/@angular/common/fesm2022/_platform_location-chunk.mjs:23:20
 ❯ node_modules/.pnpm/@angular+common@21.0.3_@ang_e9b7452ef12a5f71d197dda1c8d449ff/node_modules/@angular/common/fesm2022/_platform_location-chunk.mjs:17:1
 ❯ projects/my-lib/core/my-service/my-service.ts:1:1

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯


 Test Files  1 failed (1)
      Tests  no tests
   Start at  11:36:33
   Duration  986ms (transform 31ms, setup 0ms, import 0ms, tests 0ms, environment 625ms)

Your Environment

Angular CLI       : 21.0.2
Angular           : 21.0.3
Node.js           : 22.16.0
Package Manager   : pnpm 10.23.0
Operating System  : win32 x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.0.2            │ ^21.0.2           │
│ @angular/cli              │ 21.0.2            │ ^21.0.2           │
│ @angular/common           │ 21.0.3            │ ^21.0.0           │
│ @angular/compiler         │ 21.0.3            │ ^21.0.0           │
│ @angular/compiler-cli     │ 21.0.3            │ ^21.0.0           │
│ @angular/core             │ 21.0.3            │ ^21.0.0           │
│ @angular/forms            │ 21.0.3            │ ^21.0.0           │
│ @angular/platform-browser │ 21.0.3            │ ^21.0.0           │
│ @angular/router           │ 21.0.3            │ ^21.0.0           │
│ ng-packagr                │ 21.0.0            │ ^21.0.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.0.15            │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else relevant?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions