Repository navigation
Description
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
Description
@angular/build:unit-test with the vitest runner builds tests with externalPackages: true, so Vite resolves package imports when the tests run. Vite's resolve.conditions is hard-coded to ['es2015', 'es2020', 'module', 'browser'] in runners/vitest/plugins.ts. The build target's conditions only reach the esbuild step, so packages resolve their default exports in tests even though ng build resolves the conditional ones.
Adding the conditions to resolve.conditions in a runnerConfig works around it.
#33246 proposed forwarding conditions to resolve.conditions and was closed as stalled.
Minimal Reproduction
Repository: https://github.com/damienwebdev/angular-cli-conditions-repro/tree/vitest
-
ng new repro-app --ssr --defaults -
Add a local package with a conditional export, for example
node_modules/cond-pkg:package.json{ "name": "cond-pkg", "version": "1.0.0", "type": "module", "exports": { ".": { "my-condition": { "types": "./custom.d.ts", "default": "./custom.mjs" }, "types": "./default.d.ts", "default": "./default.mjs" } } }custom.mjs:export const VALUE = 'RESOLVED_CUSTOM';
default.mjs:export const VALUE = 'RESOLVED_DEFAULT';
custom.d.ts:export declare const VALUE: string; export declare const customOnly: string;
default.d.ts:export declare const VALUE: string; -
In
angular.json, set"conditions": ["my-condition"]in the build target'soptionsand in itsdevelopmentconfiguration. -
Replace
src/app/app.spec.tswith:import { VALUE } from 'cond-pkg'; describe('cond-pkg', () => { it('resolves the custom condition', () => { expect(VALUE).toBe('RESOLVED_CUSTOM'); }); });
-
ng test --watch=false
Expected: the test passes, matching ng build.
Exception or Error
AssertionError: expected 'RESOLVED_DEFAULT' to be 'RESOLVED_CUSTOM' // Object.is equality
Expected: "RESOLVED_CUSTOM"
Received: "RESOLVED_DEFAULT"
Your Environment
Angular CLI : 22.3.0
Angular : 22.3.2
Node.js : 24.19.0
Package Manager : npm 11.17.0
Operating System : linux x64
@angular/build 22.3.0
@angular/cli 22.3.0
@angular/core 22.3.2
@angular/ssr 22.3.0
typescript 6.0.3
vitest 5.1.2
Anything else relevant?
The same code is on main (637f4b6).
Activity