MSW V2 - Replace MSW Imports

verified codemod icon

This codemod replaces MSW v1 with v2 imports.

MSW
ts-morph
Estimated time saving
10 minutes/occurrence
Change mode
Autonomous
Applicability criteria

MSW version >= 1.0.0

Made by
Codemod.com
Codemod.com

Usage →

Codemod CLI:

intalling vs code extension tooltip icon
codemod msw/2/imports
copy CLI command icon

Codemod VS Code extension:

intalling vs code extension tooltip icon
vs code logo
Run in VS Code

Use/edit codemod in Codemod Studio:

intuita logo without text
Open in Codemod Studio

Description

Following the original msw upgrade guide, there are certain imports that changed their location and/or naming. This codemod will import correct objects from appropriate paths to start your msw migration path.

  • setupWorker is now imported from msw/browser - rest from msw is now named http - RestHandler from msw is now named HttpHandler

Example

Before

import { setupWorker, rest as caller, RestHandler } from 'msw';
const handlers: RestHandler[] = [
  caller.get('/user', (req, res, ctx) => {
    return res(ctx.json({ firstName: 'John' }));
  }),
]

After

import { setupWorker } from 'msw/browser'; import { http as caller, HttpHandler } from 'msw';
const handlers: HttpHandler[] = [
  caller.get('/user', (req, res, ctx) => {
    return res(ctx.json({ firstName: 'John' }));
  }),
]
``` ### Links for more info
-   [msw v1 to v2 migration guide -> imports](https://mswjs.io/docs/migrations/1.x-to-2.x/#imports)