Repository navigation
@angular/build:unit-test with Vitest test runner throws Failed to import test file error #31993
Copy link
Copy link
Labels
angular/build:unit-testarea: @angular/buildfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarelyseverity3: brokentype: bug/fix
Description
Activity
- addedfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarely
on Dec 2, 2025 - marked Unit Test using Vitest for service class that inject HttpClient will hit JIT Compilation fail error #32095 as a duplicate of this issue
on Dec 11, 2025 @alan-agius4 is there any information on when this bug can be expected to be fixed? Thanks!
Reacted by Jan Fajfr- added a commit that references this issue
on Jan 15, 2026 I have taken opportunity to fix this issue, PR #32304 for the same.
Reacted by Vojtěch Vokálek and Daniel Beland@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.patchdiff --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,Reacted by Vojtěch Vokálek and Ján Kučerabased on @mistrykaran91's PR I was able to resolve this by adding
@angular/compilerto test polyfills inangular.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" } } } } } }Reacted by Shawn Ostler, dbPietah and UzairReacted by Florent HastoyConfirming that @dsebata 's solution fixes the issue.
- added a commit that references this issue
on Aug 21, 2026
Metadata
Metadata
Assignees
Labels
angular/build:unit-testarea: @angular/buildfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarelyseverity3: brokentype: bug/fix

Command
test
Is this a regression?
The previous version in which this bug was not present was
No response
Description
Steps to reproduce:
TestServicethat will contain a single propertyrouter = inject(Router)@angular/build:unit-testrunner to includeprovidersFile[TestService]from itTestcomponent that will inject this service:service = inject(TestService)ng testwill fail with the following error for all specs:If you remove injection of the
TestServicefrom theTestcomponent all the specs will start to pass.Minimal Reproduction
https://github.com/th0r/ng-vitest-test-providers-bug
Your Environment