Releases: mswjs/msw
Releases · mswjs/msw
v0.19.0
Features
- The library now defers all the client-side requests that happen between calling
worker.start()
and successful worker activation. This eliminates race condition between the two, and now comes as the default behavior. (#190, #196)
The behavior of deferring requests can be configured using the waitUntilReady: boolean
option on worker.start()
:
import { setupWorker, rest } from 'msw'
const worker = setupWorker(/* request handlers */)
worker.start({
// You can opt-out of the deferred network requests behavior.
waitUntilReady: false
})
v0.18.1
v0.18.0
Features
- Adds support for declaring runtime request handlers (#128, #187):
.use()
to prepend any request handlers. Any prepended request handlers take priority over existing ones, if their predicate match..restoreHandlers()
to mark any used one-time request handlers as unused, allowing them to affect network again..resetHandlers()
to remove any request handlers added on runtime. Optionally accepts a list of next request handlers that replace the initial handlers passed tosetupServer
/setupWorker
call.
- Adds support for one-time request handlers using
res.once()
. Such handles will respond with a given mocked response only once upon request match:
rest.get('/books', (req, res, ctx) => {
return res.once(ctx.json([1, 2, 3]))
})
Bug fixes
TS7016: Could not find a declaration file for module 'cookie'
- Fixes an outdated GitHub repository link in the
mockServiceWorker.js
file.
v0.17.2
v0.17.1
v0.17.0
Features
- Custom request handlers have reworked lifecycle and accept new methods:
parse()
, to retrieve additional information about the request before the predicate.getPublicRequest()
, to modify the original request with public information (i.e. addingreq.params
orreq.variables
).
Bug Fixes
v0.16.2
Features
- GraphQL requests are now being printed into browser’s console using a proper format (operation name / response status) (#161, #174)
- Each custom request handler can have its own
log
function that controls what and how gets printed into browser’s console for introspection:
// my-handler.js
export const myHandler = () => {
return {
log(req, res, handler) {
console.log('%s %s', req.method, req.url.href)
}
}
}
The information available in the log
function:
req
, intercepted request instance;res
, resolved mocked response object;handler
, exact request handler that captured this request.
Bug fixes
v0.16.1
v0.16.0
This release introduces bug fixes to the Service Worker file. Please follow the update instructions in your browser's console. Thanks.
Breaking changes
rest.get('/user', (req) => {
- req.url // "/user"
+ req.url.href // "/user"
})
Features
- Adds support for running in NodeJS (#104, #146, docs). This makes it possible to use the same mock definition for unit and integration tests.
// test/LoginForm.test.js
import { rest } from 'msw'
import { setupServer } from 'msw/node'
describe('LoginForm', () => {
const server = setupServer(
rest.post('/login', (req, res, ctx) => {
return res(ctx.json({ success: true }))
})
)
beforeAll(() => {
server.listen()
})
afterAll(() => {
server.close()
})
it('allows a user to log in', () => {
// your assertions here...
})
})
Bug fixes
- Fixes an issue that resulted into the last value of a multi-value header being available in
req.headers
(#154)
v0.15.9
This release contains changes to
mockServiceWorker.js
file. Please follow the instructions in your browser's console to update the worker file.
Bug fixes
- Fixes an issue that resulted into failed mocking with multiple pages open after page refresh (#139, #155).
- Fixes an issue that resulted into the following exception when using
ctx.fetch()
(#150, #151):
Failed to execute 'text' on 'Response': body stream is locked