Skip to content

Conversation

@jeripeierSBB
Copy link
Contributor

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Given a package.json like following:

{
  "name": "browser-condition",
  "type": "module",
  "exports": {
    ".": {
      "browser": {
        "default": "./browser.js"
      },
      "node": {
        "default": "./node.js"
      },
      "default": "./browser.js"
    }
  }
}

Currently, when using vitest, even when testing in real browser, it takes the "node.js" module.
This, for example, happens when using lit components, which are depending on isServer const in tests as they load different modules depending on the environment.

Issue Number: N/A

What is the new behavior?

Respects the "browser" condition when testing in a real browser and chooses "browser.js" in the above example.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

We face difficulties, trying to create a test case.
We tried to add a test like following, but the problem is that the dependency @vitest/browser-playwright is missing in unit tests. Should it be installed? Or more general, what would be the best way to create a test case?.

    it('should resolve browser condition', async () => {
      await harness.modifyFile('tsconfig.json', (content) => {
        const tsConfig = JSON.parse(content);
        tsConfig.compilerOptions.paths = {
          'browser-condition': ['browser-condition'],
        };

        return JSON.stringify(tsConfig);
      });

      await harness.writeFile('browser-condition/browser.js', "export const value = 'browser';");
      await harness.writeFile('browser-condition/node.js', "export const value = 'node';");

      const packageJson = {
        name: 'browser-condition',
        type: 'module',
        exports: {
          '.': {
            browser: {
              default: './browser.js',
            },
            node: {
              default: './node.js',
            },
            default: './browser.js',
          },
        },
      };

      await harness.writeFile(
        'browser-condition/package.json',
        JSON.stringify(packageJson, null, 2),
      );

      await harness.modifyFile('src/app/app.component.ts', (content) => {
        return (
          `import { value } from 'browser-condition';\n` +
          content.replace(`title = 'app';`, 'title = value;')
        );
      });

      await harness.modifyFile('src/app/app.component.spec.ts', (content) => {
        return content.replace(
          `expect(app.title).toEqual('app');`,
          `expect(app.title).toEqual('browser');`,
        );
      });

      harness.useTarget('test', {
        ...BASE_OPTIONS,
        browsers: ['chrome'],
      });

      const { result } = await harness.executeOnce();
      expect(result?.success).toBeTrue();
    })

@alan-agius4 alan-agius4 requested a review from clydin December 9, 2025 17:16
@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Dec 9, 2025
@clydin clydin added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Dec 10, 2025
@hybrist
Copy link
Contributor

hybrist commented Dec 10, 2025

Sorry for missing this one in today's release a few hours ago!

@hybrist hybrist merged commit 98c207b into angular:main Dec 10, 2025
38 checks passed
@hybrist
Copy link
Contributor

hybrist commented Dec 10, 2025

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants