Skip to content
项目

关于

在 Playwright 中使用 Mock Service Worker 模拟 API。

系统与基础设施

这些数字如何统计

实测星标增长

24小时
0
7天
+1
15天
+1
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

@msw/playwright

Mock Service Worker binding for Playwright.

Motivation

While you can use MSW in Playwright following the default Browser integration, the cross-process messaging in Playwright makes it clunky to work with the worker instance in one process (your tests; Node.js) to affect another (your app; browser).

await page.evaluate(() => {
  // In order to reference the worker instance in your tests,
  // you have to set it on `window` alongside any other
  // functions from the `msw` package you want to use since
  // you cannot reference them in `page.evaluate` directly.
  const { worker, http, graphql } = window.msw
  worker.use(...overrides)
})

This package aims to provide a better developer experience when mocking APIs in Playwright.

Until we ship cross-process request interception, @msw/playwright will rely on the page.route() API to provision the request interception in your tests. That means you don't have to initialize the worker script to use this package. That also means that any page.route() limitations now affect this library. Treat this as an implementation detail that is likely to change in the future.

Usage

npm i msw @msw/playwright
// playwright.setup.ts
import { test as testBase } from '@playwright/test'
import { type AnyHandler } from 'msw'
import { defineNetworkFixture, type NetworkFixture } from '@msw/playwright'
import { handlers } from '../mocks/handlers.js'

interface Fixtures {
  handlers: Array<AnyHandler>
  network: NetworkFixture
}

const test = testBase.extend<Fixtures>({
  // Initial list of the network handlers.
  handlers: [handlers, { option: true }],

  // A fixture you use to control the network in your tests.
  network: [
    async ({ context, handlers }, use) => {
      const network = defineNetworkFixture({
        context,
        handlers,
      })

      await network.enable()
      await use(network)
      await network.disable()
    },
    { auto: true },
  ],
})
import { http, HttpResponse } from 'msw'
import { test } from './playwright.setup.js'

test('displays the user dashboard', async ({ network, page }) => {
  // Access the network fixture and use it as the `setupWorker()` API.
  // No more disrupted context between processes.
  network.use(
    http.get('/user', () => {
      return HttpResponse.json({
        id: 'abc-123',
        firstName: 'John',
        lastName: 'Maverick',
      })
    }),
  )

  await page.goto('/dashboard')
})

Limitations

  • Since context.routeWebSocket() provides no means of knowing which page triggered a WebSocket connection, relative WebSocket URLs in ws.link(url) will be resolved against the latest created page in the browser context.

Comparison

playwright-msw

playwright-msw is a community package that, just like @msw/playwright, aims to provide a better experience when mocking APIs in your Playwright tests.

While playwright-msw is a fantastic tool and a huge inspiration for this package to exist, I believe it approaches the idea at a rather complex angle. That introduces a layer of abstraction that is subjected to the "left behind" problem as it needs to map to any MSW changes explicitly.

playwright-msw @msw/playwright
Initialization createWorkerFixture() is used as a part of your custom fixture. defineNetworkFixture() creates the entire fixture for you, pre-configured.
Implementation Uses a custom router to match handlers and a custom wrapper around SetupWorker API. Uses MSW directly. Uses page.route() as the source of the network to route through the handlers.
Feature set Supports http and graphql namespaces. Supports all namespaces (http, graphql, ws, any other APIs exposed by MSW in the future).

探索相关项目

加载中…