Skip to content

dellenshang/mock-service-worker-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

Use Mock Service Worker in your frontend App

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published