setupServer

在 Node.js 中配置请求拦截。

用于在 Node.js 进程中配置请求拦截的函数。

¥A function that configures the interception of requests in a Node.js process.

尽管名称中有 “server” 一词,但它不会建立任何服务器,完全在进程的线程中运行。

¥Despite the word “server” in the name, it does not establish any servers, operating entirely in the thread of your process.

Node.js 细节

¥Node.js specifics

setupServer 函数充当在 Node.js 中应用相同请求处理程序的桥梁,其中 Service Worker 无法运行。相反,它增强了标准请求模块(如 http),以便对传出的请求做出反应并从模拟定义中对它们做出响应。

¥The setupServer function acts as a bridge to apply the same request handlers in Node.js, where Service Workers cannot run. Instead, it augments the standard request modules like http in order to react to outgoing requests and respond to them from your mock definitions.

注意事项

¥Precautions

在浏览器和 Node.js 之间重用相同的处理程序可能会有一定的限制。你可能在处理程序中使用的浏览器特定 API(如 locationlocalStorage)在 Node.js 中不可用。你有责任在测试中填充这些 API 或使用提供它们的适当测试环境。

¥Reusing the same handlers between the browser and Node.js may have certain limitations. The browser-specific API you may use in your handlers, like location or localStorage, will not be available in Node.js. It is your responsibility to polyfill those APIs in tests or use an appropriate testing environment that provides them.

用法

¥Usage

使用 setupServersetupWorker 类似。归根结底,它向其提供请求处理程序列表,并启动请求拦截。

¥Using setupServer is similar to setupWorker. It comes down to providing it with the list of request handlers, and starting the request interception.

import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
 
// Provide the server-side API with the request handlers.
const server = setupServer(
  http.get('/user', () => {
    return HttpResponse.json({
      id: '15d42a4d-1948-4de4-ba78-b8a893feaf45',
      firstName: 'John',
    })
  })
)
 
// Start the interception.
server.listen()

Node.js 中 Mock Service Worker 的最常见用法之一是在集成测试期间。将 API 模拟作为测试设置的一部分进行集成,以确保你适当地启动和清理请求拦截逻辑。

¥One of the most common usages of Mock Service Worker in Node.js is during integration tests. Integrate API mocking as a part of your testing setup to make sure you start and clean up the request interception logic appropriately.

看看使用 Jest 测试框架的测试设置示例:

¥Take a look at the testing setup example using Jest testing framework:

// jest.setup.js
import { setupServer } from 'msw/node'
import { handlers } from './handlers'
 
const server = setupServer(...handlers)
 
beforeAll(() => {
  // Start the interception.
  server.listen()
})
 
afterEach(() => {
  // Remove any handlers you may have added
  // in individual tests (runtime handlers).
  server.resetHandlers()
})
 
afterAll(() => {
  // Disable request interception and clean up.
  server.close()
})

虽然我们建议全局设置请求拦截,作为测试设置的一部分,但如果你愿意,你也可以在单个测试中使用 setupServer。只需确保选择一种模式并在整个测试过程中遵循它 - 多次 setupServer 调用不是一个好主意!

¥While we commend setting up request interception globally, as a part of your testing setup, you may also use setupServer in individual tests, if you want to. Just make sure to choose one pattern and follow it throughout your tests—multiple setupServer calls is not a good idea!