Skip to content

Commit f8bab04

Browse files
committed
playwright: Implement msw fixture
This can be used as an alternative to the `mirage` fixture and uses `@crates-io/msw` instead.
1 parent 9275507 commit f8bab04

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

e2e/helper.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { test as base } from '@playwright/test';
2+
import type { MockServiceWorker } from 'playwright-msw';
3+
import { http, HttpResponse } from 'msw';
4+
import { createWorker } from 'playwright-msw';
5+
import { db, handlers } from '@crates-io/msw';
6+
27
import { FakeTimers, FakeTimersOptions } from './fixtures/fake-timers';
38
import { MiragePage } from './fixtures/mirage';
49
import { PercyPage } from './fixtures/percy';
@@ -13,6 +18,11 @@ export type AppOptions = {
1318
export interface AppFixtures {
1419
clock: FakeTimers;
1520
mirage: MiragePage;
21+
msw: {
22+
worker: MockServiceWorker;
23+
db: typeof db;
24+
authenticateAs: (user: any) => Promise<void>;
25+
};
1626
ember: EmberPage;
1727
percy: PercyPage;
1828
a11y: A11yPage;
@@ -39,6 +49,21 @@ export const test = base.extend<AppOptions & AppFixtures>({
3949
},
4050
{ scope: 'test' },
4151
],
52+
// MockServiceWorker integration via `playwright-msw`.
53+
//
54+
// We are explicitly not using the `createWorkerFixture()`function, because
55+
// uses `auto: true`, and we want to be explicit about our usage of the fixture.
56+
msw: async ({ page }, use) => {
57+
const worker = await createWorker(page, handlers);
58+
const authenticateAs = async function (user) {
59+
db.mswSession.create({ user });
60+
await page.addInitScript("globalThis.localStorage.setItem('isLoggedIn', '1')");
61+
};
62+
63+
await use({ worker, db, authenticateAs });
64+
db.reset();
65+
worker.resetCookieStore();
66+
},
4267
ember: [
4368
async ({ page, emberOptions }, use) => {
4469
let ember = new EmberPage(page);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
"msw": "2.7.0",
134134
"normalize.css": "8.0.1",
135135
"nyc": "17.1.0",
136+
"playwright-msw": "3.0.1",
136137
"postcss-preset-env": "10.1.3",
137138
"prettier": "3.4.2",
138139
"qunit": "2.24.1",

pnpm-lock.yaml

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)