listHandlers()
返回当前请求处理程序的列表。
调用签名
¥Call signature
server.listHandlers()此方法不接受任何参数,并返回 server 对象上存在的所有处理程序的列表。它主要用于调试和自省目的。
¥This method accepts no arguments and returns a list of all handlers present on the server object. It’s primarily designed for debugging and introspection purposes.
过滤处理程序
¥Filtering handlers
你可以通过对 .listHandlers() 函数调用的结果实现 instanceof 检查,将处理程序过滤为特定类型(例如,仅 HTTP 或仅 WebSocket 处理程序)。
¥You can filter out the handlers to a particular type (e.g. only HTTP or only WebSocket handlers) by implementing the instanceof check on the result of the .listHandlers() function call.
例如,以下是仅记录 HTTP 处理程序的方法:
¥For example, here’s how to log only HTTP handlers:
import { RequestHandler } from 'msw'
const handlers = server.listHandlers().filter((handler) => {
// This will include both `http.*` and `graphql.*` handlers
// while omitting and `ws.*` handlers.
return handler instanceof RequestHandler
})
console.log(handlers)分别地,
handler instanceof WebSocketHandler仅返回ws处理程序。¥Respectively,
handler instanceof WebSocketHandlerreturns onlywshandlers.