Skip to content

unit-test (vitest) resolves packages without the build target's conditions #34835

Backlog

Description

@damienwebdev

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

  1. ng new repro-app --ssr --defaults

  2. 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;

  3. In angular.json, set "conditions": ["my-condition"] in the build target's options and in its development configuration.

  4. Replace src/app/app.spec.ts with:

    import { VALUE } from 'cond-pkg';
    
    describe('cond-pkg', () => {
      it('resolves the custom condition', () => {
        expect(VALUE).toBe('RESOLVED_CUSTOM');
      });
    });
  5. 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).

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

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions