mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

Support option to use customized script to `server.listen()` for consistency with `worker.start()` #2214

Closed 2wheeh closed 4 months ago

2wheeh commented 4 months ago

Scope

Improves an existing behavior

Compatibility

It's going to be additional option so it would be backward compatible without breaking changes.

Feature description

I'm using custom path for the script which importing auto-generated mockServiceWorker.js:

// customServiceWorker.js

importScripts('./mockServiceWorker.js');

// ... additional handling for worker
worker.start({
    serviceWorker: {
      url: '/customServiceWorker.js',
    },
});

And I'd love to mock this on the unit test with msw/node.

But it's not possible to load customServiceWorker.js in same manner with worker since server's listen method doesn't support this option inconsistently.

Should this option be supported for the consistency?

i.e.

server.listen({
    mockScript: {
      url: '/customServiceWorker.js',
    },
})

This would allow for consistent behavior between browser and node environments when using custom worker scripts, improving the flexibility and ease of use of MSW in various testing scenarios.