Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
playwright-msw
Advanced tools
A Mock Service Worker API for Playwright
test.ts
import { test as base, expect } from "@playwright/test";
import { rest } from "msw";
import type { MockServiceWorker } from "playwright-msw";
import { createServer } from "playwright-msw";
const handlers = [
rest.get(
"/api/user/profile",
(_, response, context) =>
response(
context.delay(250),
context.status(200),
context.json({
firstName: "Joe",
lastName: "Smith",
favVegetable: "🍆"
});
),
),
];
export const test = base.extend<{
msw: MockServiceWorker;
rest: typeof rest;
}>({
msw: [
async ({ page }, use, info): Promise<void> => {
const server = await createServer({
page,
info,
handlers,
webServerPort: 8080,
baseWorkerServerPort: 9000,
url: "/**",
});
await server.listen();
// Test has not started to execute
await use(server);
// Test has finished executing
await server.close();
},
{
/**
* Scope this fixture on a per test basis to ensure that each test has a
* fresh copy of MSW.
*/
scope: "test",
/**
* By default, fixtures are lazy; they will not be initalised unless they're
* used by the test. Setting `true` here means that the fixture will be auto-
* initialised even if the test doesn't use it.
*/
auto: true,
},
],
rest,
});
export { expect };
profile-page.spec.ts
import { test, expect } from "./test";
test.describe.parallel("A demo of playwright-msw's functionality", () => {
test("should use the default handlers without requiring handlers to be specified on a per-test basis", async ({
page
}) => {
await page.goto("/");
await expect(page.locator('[data-testid="favVegetable"]')).toHaveText("🍆");
});
test("should allow mocks to be overridden on a per test basis", async ({
page
msw,
rest,
}) => {
await msw.use(
rest.get(
"/api/user/profile",
(_, response, context) =>
response(
context.delay(250),
context.status(200),
context.json({
firstName: "Joe",
lastName: "Smith",
favVegetable: "🥔"
});
),
),
);
await page.goto("/");
await expect(page.locator('[data-testid="favVegetable"]')).toHaveText("🥔");
});
});
FAQs
A Mock Service Worker API for Playwright.
The npm package playwright-msw receives a total of 41,715 weekly downloads. As such, playwright-msw popularity was classified as popular.
We found that playwright-msw demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.