MSW v1 to v2 Migration
MSW v2 brings the biggest API change to the library since its inception. Alongside the new API, it includes various features, such as ReadableStream
support, ESM-compatibility, and countless bug fixes.
Codemod supports a mostly automated MSW v2 upgrade experience. This page provides tips and resources you may need for a successful migration.
Getting started
Migration Steps
Installation
npm install msw@latest
Run codemods
Inside your project’s root directory, run the MSW v2 upgrade recipe:
npx codemod@latest msw/2/upgrade-recipe
Fix false negatives
The upgrade recipe does not change the signatures of MSW handlers, if they were called using a custom factory function, for example to provide more type-safety or else. For example, the following code will only be partially updated:
export function mockFactory<T extends MyComplexType>(
export function mockFactory<T extends MyComplexType>(
url: string,
resolver: MyResolverType,
) {
return rest.get(url, resolver);
}
const handlers = [
mockFactory('/some/url', (req, res, ctx) => {
return res(ctx.status(200));
}),
];
Additionally, if you were using req.body
in your interceptors, this codemod will blindly assume you want await request.json()
instead of any other type. You will have to correct that manually.
Was this page helpful?