Skip to content

MSW fails to import when running unit tests in browser mode #32523

Open

Description

@prowe

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

Yes

Description

In Angular 21, when running unit tests using the below configuration the run fails with "The entry point "msw" cannot be marked as external"

Setup:

  1. Create a new angular project.
  2. Set unit tests to use the chromium browser
  3. Install MSW
  4. Attempt to import anything from msw in a test case, the above error is thrown.

This appears to be related to a disconnect between the package.json configuration of MSW and the "resolve conditions" that are being passed to Vitest by the angular unit test runner.

When running the tests in jsdom (not browser) we do not encounter this issue.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI       : 21.1.4
Angular           : 21.1.5
Node.js           : 22.18.0
Package Manager   : npm 11.5.2
Operating System  : win32 x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.1.4            │ ^21.1.4           │
│ @angular/cli              │ 21.1.4            │ ^21.1.4           │
│ @angular/common           │ 21.1.5            │ ^21.1.0           │
│ @angular/compiler         │ 21.1.5            │ ^21.1.0           │
│ @angular/compiler-cli     │ 21.1.5            │ ^21.1.0           │
│ @angular/core             │ 21.1.5            │ ^21.1.0           │
│ @angular/forms            │ 21.1.5            │ ^21.1.0           │
│ @angular/platform-browser │ 21.1.5            │ ^21.1.0           │
│ @angular/router           │ 21.1.5            │ ^21.1.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.0.18            │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Activity

  1. transferred this issue fromangular/angularon Feb 20, 2026
  2. alan-agius4 commented on Mar 5, 2026

    @alan-agius4
    Collaborator

    @prowe, are you importing the browser variant of msw?

  3. prowe commented on Mar 5, 2026

    @prowe
    Author

    We are installing msw via npm and then importing from browser for setup. All the setup typs (such as HttpReponse, http, graphql etc) are only available by importing msw.

    This used to work in angular 20 under the experimental UnitTest support

  4. added
    needs: repro stepsWe cannot reproduce the issue with the information given
    and removed on Mar 19, 2026
  5. alan-agius4 commented on Mar 19, 2026

    @alan-agius4
    Collaborator

    This seems like a bug but we'll need to look at a reproduction to find and fix the problem. Can you setup a minimal repro please?

    You can read here why this is needed. A good way to make a minimal repro is to create a new app via ng new repro-app and adding the minimum possible code to show the problem. Then you can push this repository to github and link it here.

    This might be related to your directory structure so its really important to get an accurate repro to diagnose this.

  6. JamRenDoc commented on Mar 20, 2026

    @JamRenDoc

    https://github.com/JamRenDoc/angular-repro-32523
    Here is a minimally viable project that reproduces this issue. You can see it when running the tests via 'npm run test'. Thanks!

  7. alan-agius4 commented on Mar 20, 2026

    @alan-agius4
    Collaborator

    I am not seeing an msw usage in the reproduction.

  8. JamRenDoc commented on Mar 20, 2026

    @JamRenDoc

    It's in the test. app.spec.ts.

  9. prowe commented on Apr 1, 2026

    @prowe
    Author

    @alan-agius4 I was wondering if there were any other ideas on this.. We would gladly submit a PR to fix it if we had some direction on what might be the culprit.

  10. added
    needs: investigationRequires some digging to determine if action is needed
    and removed
    needs: repro stepsWe cannot reproduce the issue with the information given
    on Apr 1, 2026
  11. jrdockendorff commented on Jun 4, 2026

    @jrdockendorff

    FYI, this is still an issue on Angular 22. I can create a branch of the example repo with it in Angular 22 if you'd like, but all I did was run the update script on the existing repo.

  12. rkrisztian commented on Jun 4, 2026

    @rkrisztian

    I followed the Vitest Browser Mode documentation on an Angular 21.2.15 project, and I'm getting the same issue.

    Update: For reproducibility, this is not a minimal project, but here is my branch where I tried to integrate MSW:

  13. rkrisztian commented on Jun 10, 2026

    @rkrisztian

    Thanks, @lacolaco, I used your workaround successfully in my Angular 21 project (see vitest-base.config.ts):
    rkrisztian/contenttree@7d56ac5
    (I'm going to delete my old branch soon.)
    I'm not ready to upgrade yet due to certain dependencies that did not catch up with Angular 22 yet, so I'm glad this also works in Angular 21.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs: investigationRequires some digging to determine if action is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions