Skip to content

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

Description

@th0r

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            │
└───────────────────────────┴───────────────────┴───────────────────┘

Activity

  1. added theissue type on Dec 2, 2025
  2. th0r commented on Dec 18, 2025

    @th0r
    Author

    @alan-agius4 is there any information on when this bug can be expected to be fixed? Thanks!

  3. sqrter commented on Jan 3, 2026

    @sqrter

    I'm not sure if it helps, but in my case, adding setupFiles: ['./test-setup.ts'] with just one line import "@angular/compiler"; into my vite.config.mts solved a similar error.

    Image
  4. added a commit that references this issue on Jan 15, 2026
    de8f5a7
  5. mistrykaran91 commented on Jan 15, 2026

    @mistrykaran91
    Contributor

    I have taken opportunity to fix this issue, PR #32304 for the same.

  6. dcendents commented on Feb 10, 2026

    @dcendents

    @mistrykaran91 thanks your PR fixed it for me.

    For anybody else that can't wait for the PR, here is the patch file to apply using patch-package: https://www.npmjs.com/package/patch-package

    @angular+build+21.1.3.patch

    diff --git a/node_modules/@angular/build/src/builders/unit-test/options.js b/node_modules/@angular/build/src/builders/unit-test/options.js
    index b168d54..44e08ad 100755
    --- a/node_modules/@angular/build/src/builders/unit-test/options.js
    +++ b/node_modules/@angular/build/src/builders/unit-test/options.js
    @@ -112,7 +112,19 @@ async function normalizeOptions(context, projectName, options) {
                 : runnerConfig,
         };
     }
    -function injectTestingPolyfills(polyfills = []) {
    -    return polyfills.includes('zone.js') ? [...polyfills, 'zone.js/testing'] : polyfills;
    +function injectTestingPolyfills(
    +    polyfills = [],
    +    runner,
    +    hasProvidersFile,
    +) {
    +    const testPolyfills = polyfills.includes('zone.js')
    +    ? [...polyfills, 'zone.js/testing']
    +    : polyfills;
    +
    +    if (runner === (0, schema_1.Runner).Vitest && hasProvidersFile) {
    +        testPolyfills.push('@angular/compiler');
    +    }
    +
    +    return testPolyfills;
     }
     //# sourceMappingURL=options.js.map
    \ No newline at end of file
    diff --git a/node_modules/@angular/build/src/builders/unit-test/runners/vitest/build-options.js b/node_modules/@angular/build/src/builders/unit-test/runners/vitest/build-options.js
    index 568a596..5650166 100755
    --- a/node_modules/@angular/build/src/builders/unit-test/runners/vitest/build-options.js
    +++ b/node_modules/@angular/build/src/builders/unit-test/runners/vitest/build-options.js
    @@ -15,6 +15,7 @@ const node_path_1 = __importDefault(require("node:path"));
     const path_1 = require("../../../../utils/path");
     const schema_1 = require("../../../application/schema");
     const options_1 = require("../../options");
    +const schema_2 = require('../../schema');
     const test_discovery_1 = require("../../test-discovery");
     function createTestBedInitVirtualFile(providersFile, projectSourceRoot, teardown, polyfills = []) {
         const usesZoneJS = polyfills.includes('zone.js');
    @@ -114,7 +115,11 @@ async function getVitestBuildOptions(options, baseBuildOptions) {
             externalPackages: true,
             externalDependencies,
         };
    -    buildOptions.polyfills = (0, options_1.injectTestingPolyfills)(buildOptions.polyfills);
    +    buildOptions.polyfills = (0, options_1.injectTestingPolyfills)(
    +        buildOptions.polyfills,
    +        (0, schema_2.Runner.Vitest),
    +        !!providersFile,
    +    );
         const testBedInitContents = createTestBedInitVirtualFile(providersFile, projectSourceRoot, !options.debug, buildOptions.polyfills);
         return {
             buildOptions,
    
  7. dsebata commented on Mar 9, 2026

    @dsebata

    based on @mistrykaran91's PR I was able to resolve this by adding @angular/compiler to test polyfills in angular.json. At least for now till the PR makes it to production

    {
      "projects": {
        "my-project": {
          "architect": {
            "build": {
              "configurations": {
                "unit-tests": {
                  "polyfills":  ["@angular/compiler"]
                }
              }
            },
            "test": {
              "builder": "@angular/build:unit-test",
              "options": {
                "buildTarget": "my-project:build:unit-tests"
              }
            }
          }
        }
      }
    }
  8. dbPietah commented on May 13, 2026

    @dbPietah

    Confirming that @dsebata 's solution fixes the issue.

  9. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions