listHandlers()

Return the list of current request handlers.

Call signature

worker.listHandlers()

This method accepts no arguments and returns a list of all handlers present on the worker object. It’s primarily designed for debugging and introspection purposes.

Filtering handlers

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.

For example, here’s how to log only HTTP handlers:

import { RequestHandler } from 'msw'
 
const handlers = worker.listHandlers().filter((handler) => {
  // This will include both `http.*` and `graphql.*` handlers
  // while omitting and `ws.*` handlers.
  return handler instanceof RequestHandler
})
 
console.log(handlers)

Respectively, handler instanceof WebSocketHandler returns only ws handlers.