mock-service-worker-tool
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

a tool for msw https://mswjs.io/

with it u can just write arrays to mock all the rest api in your app

Start

npx mock-service-worker-tool

It will auto create mockServiceWorker in your /public

Use

in your index.js

import { msw } from 'mock-service-worker-tool'

msw(yourRestApiArray)

in your tests

import { server } from 'mock-service-worker-tool/node'

server(yourRestApiArray)

// Establish API mocking before all tests.
beforeAll(() => server.listen())
// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => server.resetHandlers())
// Clean up after the tests are finished.
afterAll(() => server.close())
apiArray is like:

[
  {
    method: 'get',
    url: '/user',
    func: (req, res, ctx) => {
      return res(
        ctx.status(200),
        ctx.json({
          username: 'admin',
        })
      )
    },
  },
  ...
]

then call it with baseUrl http://localhost:yourlocalport
enjoy!

Package Sidebar

Install

npm i mock-service-worker-tool

Weekly Downloads

77

Version

1.1.1

License

MIT

Unpacked Size

2.47 MB

Total Files

187

Last publish

Collaborators

  • dellenshang