Skip to content

Latest commit

 

History

History

with-karma

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Karma + MSW

Mock Service Worker usage example with Karma.

Edit in CodeSandbox

Key points

1. Generate the worker script

npx msw init ./test

2. Configure Karma

Specify the base path

In karma.conf.js, add set the basePath property to ./test (the test directory). This has to be the test runner's base path in order for it to serve the worker script generated in the previous step.

Proxy the worker script request

In karma.conf.js, add a proxies property and list the /mockServiceWorker.js resource path to proxy the worker script generated in the ./test directory.

This is necessary so that root-level requests to /mockServiceWorker.js are resolved relatively to the Karma's prepended /base/ path prefix.