Skip to content

Vitest isolation breaks setupWorker in firefox #2757

Description

@hjri

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 20 or higher

Browsers

Firefox

Reproduction repository

https://github.com/hjri/msw-examples/tree/main/examples/with-vitest

Reproduction steps

Following this recipe:
https://mswjs.io/docs/recipes/vitest-browser-mode

In linked repository there are two tests - "Endpoint A" and "Endpoint B", both use extended test that exposes worker. Both tests do worker.use() to set up mocking for the endpoint they're testing. Expectation is that both tests pass, but reality is that one tests passes and other fails with 404. Which one fails depends on test execution order - first one passes, second fails.

My assumption is that setupWorker() should only create and register serviceworker once, but somehow in firefox with isolation it creates it for every test file (because of isolation) and second setupWorker() fails to register new serviceworker.

P.S. in linked repository somehow using headless: true fixes the problem, however in actual project it doesn't. Only real solution is to use chromium instead of firefox.

Current behavior

First test to execute succeeds, second fails.

Expected behavior

Both succeed

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds:triageIssues that have not been investigated yet.scope:browserRelated to MSW running in a browser

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions