![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
A wrapper for writing more reusable and cleaner mocks using mswjs.io.
MSW is a great API mocking library for creating API mocks that you can use for testing, development, and debugging. However, as the project grows, it will be increasingly difficult to handle multiple mocks (e.g., different error responses) for the same endpoint. You can't enable Multiple handlers for a single endpoint at once, so you may have to define your handlers as functions and then import them and use them as you need. As a result, there will be lots of imports throughout the project, which can lead to maintainability issues. And in the case of using mocks for manual testing in the browser, this solution is not going to work.
Additionally, the MSW's API is a bit verbose. There is no doubt that it's very flexible, but in most cases, you won't need that much flexibility and, this can end up with lots of duplication and less readable code.
Long story short, Lyrebird is a simple wrapper for MSW that makes it easy to create and manage more declarative and cleaner mock handlers by providing an abstracted interface for MSW's API and useful utilities for creating and managing mock handlers.
First, install msw
and lyrebird
:
# NPM
npm install msw lyrebird --save-dev
# Yarn
yarn add msw lyrebird --dev
# PNPM
pnpm add msw lyrebird --save-dev
Next, create a HandlerCollection
and define your mocks using RestHandler
:
// handlers.ts
import { HandlerCollection, RestHandler } from 'lyrebird'
const handlerCollection = new HandlerCollection()
handlerCollection.collect(
new RestHandler()
.onGet('/users')
.reply(200, {
users: [],
})
.as('getAllUsers'), // Add a name for the handler
new RestHandler()
.onPost('/register')
.reply(200, {
success: true,
message: 'users registered successfully.',
})
.as('register'),
)
export { handlerCollection }
Then set up the mock server using setupServer()
or setupWorker()
and
instantiate Lyrebird's MockServer
passing the handlerCollection
to it:
// mockServer.ts
import { setupServer } from 'msw/node'
import { MockServer } from 'lyrebird'
import { handlerCollection } from './handlers'
const mockServer = setupServer()
// In case that you are using jest for testing
beforeAll(() => mockServer.listen())
afterAll(() => mockServer.close())
afterEach(() => mockServer.resetHandlers())
export const server = new MockServer(mockServer, {
collection: handlerCollection,
})
Now use server.use()
or server.enable()
to enable your predefined mocks by
their name:
import { server } from './mockServer'
test('should fetch all users', () => {
server.use('getAllUsers')
// ...
})
test('new users can register', () => {
server.enable('register')
// ...
})
In some cases, you may need to create a one-time mock in your tests without
using the HandlerCollection
. You can do this by creating a new handler and
passing it directly to the server.use() method.
server.use(
new RestHandler().onGet('/users').reply(200, {
users: [],
}),
)
Occasionally, you may need to check request parameters to match a specific
value. To do so, you can use the withParams()
method of the Handler instance:
// Only the requests with 'active=true' query parameter will be handled
const handler = new RestHandler()
.onGet('/users')
.withParams({ active: 'true' })
.reply(200, response)
Use the withPayload()
method of the Handler instance to ensure that incoming
requests contain a specific payload.
interface RegistrationRequest {
email: string
password: string
}
interface Response {
success: boolean
message: string
}
const handler = new RestHandler()
.onPost('/register')
.withPayload<RegistrationRequest>({
email: 'user@example.com',
password: 'secret',
})
.reply<Response>(200, {
success: true,
message: 'User registered successfully.',
})
If you need full control over the handler's functionality, you can directly
define the MSW resolver function using the resolve()
method of the Handler
instance. The resolver callback receives an object with the same utilities as
the resolver's parameters in MSW
Please keep in mind that when you use the resolve() method, Lyrebird will pass
the resolver function directly to the MSW server, so methods that control the
behavior of the handler like withParams()
, withPayload()
, or reply()
won't
work, and you need to take care of its functionality.
interface LoginRequest {
username: string
password: string
}
interface LoginResponse {
username: string
firstName: string
}
const handler = new RestHandler()
.onPost('/login')
.resolve<LoginRequest, LoginResponse>(({ response, request, context }) => {
const { username } = request.body
return response(
context.status(200),
context.json({
username,
firstName: 'John',
}),
)
})
MIT License © 2021 Mohammad Ataei
FAQs
A wrapper for writing more reusable and cleaner mocks using mswjs.io.
The npm package lyrebird receives a total of 0 weekly downloads. As such, lyrebird popularity was classified as not popular.
We found that lyrebird demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.