Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

playwright-msw

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

playwright-msw - npm Package Compare versions

Comparing version 1.0.3 to 2.0.0-beta.0

lib/handler.d.ts

2

lib/fixture.js

@@ -16,3 +16,3 @@ "use strict";

({ page }, use) => __awaiter(void 0, void 0, void 0, function* () {
const server = yield (0, server_1.createServer)(page, ...handlers);
const server = yield (0, server_1.setupServer)(page, ...handlers);
yield use(server);

@@ -19,0 +19,0 @@ }),

import type { Page } from "@playwright/test";
import type { RequestHandler } from "msw";
import { MockServiceWorker } from "./types";
export declare const createServer: (page: Page, ...originalHandlers: RequestHandler[]) => Promise<MockServiceWorker>;
export declare const setupServer: (page: Page, ...initialRequestHandlers: RequestHandler[]) => Promise<MockServiceWorker>;

@@ -11,65 +11,53 @@ "use strict";

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createServer = void 0;
const msw_1 = require("msw");
const events_1 = __importDefault(require("events"));
const emitter = new events_1.default();
const handleRoute = (route, handlers) => __awaiter(void 0, void 0, void 0, function* () {
const request = route.request();
const method = request.method();
const url = new URL(request.url());
const headers = yield request.allHeaders();
const postData = request.postData();
const mockedRequest = new msw_1.MockedRequest(url, {
method,
headers,
body: postData ? Buffer.from(postData) : undefined,
});
const handleMockResponse = ({ status, headers, body }) => {
var _a;
route.fulfill({
status,
body: body !== null && body !== void 0 ? body : undefined,
contentType: (_a = headers.get("content-type")) !== null && _a !== void 0 ? _a : undefined,
headers: headers.all(),
});
};
yield (0, msw_1.handleRequest)(mockedRequest, handlers, {
onUnhandledRequest: () => {
route.continue();
},
}, emitter, {
resolutionContext: {
baseUrl: url.origin,
},
onMockedResponse: handleMockResponse,
onMockedResponseSent: handleMockResponse,
});
});
const createServer = (page, ...originalHandlers) => __awaiter(void 0, void 0, void 0, function* () {
let cachedHandlers = originalHandlers;
yield page.route("**/*", (route) => __awaiter(void 0, void 0, void 0, function* () {
exports.setupServer = void 0;
const handler_1 = require("./handler");
const utils_1 = require("./utils");
const registerRestHandler = (page, requestHandler) => __awaiter(void 0, void 0, void 0, function* () {
const routeUrl = (0, utils_1.getHandlerUrl)(requestHandler);
const routeHandler = (route) => __awaiter(void 0, void 0, void 0, function* () {
try {
yield handleRoute(route, cachedHandlers);
yield (0, handler_1.handleRoute)(route, requestHandler);
}
catch (error) {
void route.fulfill({
status: 502,
body: error.message,
});
void route.abort("failed");
}
}));
});
page.route(routeUrl, routeHandler);
return { routeUrl, routeHandler, requestHandler };
});
const registerGraphQLHandler = (page, handler) => __awaiter(void 0, void 0, void 0, function* () {
console.log(page, handler);
return Promise.reject(new Error("Support for GraphQL is not yet implemented."));
});
const registerRequestHandler = (page, handler) => __awaiter(void 0, void 0, void 0, function* () {
return (0, utils_1.isRestHandler)(handler)
? registerRestHandler(page, handler)
: registerGraphQLHandler(page, handler);
});
const registerRequestHandlers = (page, handlers) => __awaiter(void 0, void 0, void 0, function* () {
return yield Promise.all(handlers.map((handler) => registerRequestHandler(page, handler)));
});
const setupServer = (page, ...initialRequestHandlers) => __awaiter(void 0, void 0, void 0, function* () {
yield registerRequestHandlers(page, initialRequestHandlers);
let extraRegisteredHandlers = [];
return {
use: (...customHandlers) => __awaiter(void 0, void 0, void 0, function* () {
cachedHandlers = [...customHandlers, ...cachedHandlers];
use: (...newRequestHandlers) => __awaiter(void 0, void 0, void 0, function* () {
const recentlyRegisteredHandlers = yield registerRequestHandlers(page, newRequestHandlers);
extraRegisteredHandlers = [
...extraRegisteredHandlers,
...recentlyRegisteredHandlers,
];
}),
resetHandlers: (...customHandlers) => __awaiter(void 0, void 0, void 0, function* () {
cachedHandlers =
customHandlers.length > 0 ? customHandlers : originalHandlers;
resetHandlers: (...specificHandlers) => __awaiter(void 0, void 0, void 0, function* () {
if (specificHandlers.length > 0) {
throw new Error("Resetting specific handlers is not yet implemented.");
}
yield Promise.all(extraRegisteredHandlers.map(({ routeUrl, routeHandler }) => {
page.unroute(routeUrl, routeHandler);
}));
extraRegisteredHandlers = [];
}),
};
});
exports.createServer = createServer;
exports.setupServer = setupServer;
import { RequestHandler } from "msw";
import type { Route } from "@playwright/test";
export declare type MockServiceWorker = {

@@ -6,1 +7,8 @@ use: (...customHandlers: RequestHandler[]) => Promise<void>;

};
export declare type RouteUrl = string | RegExp;
export declare type RouteHandler = (route: Route) => Promise<void>;
export declare type RegisteredHandler = {
routeUrl: RouteUrl;
routeHandler: RouteHandler;
requestHandler: RequestHandler;
};
{
"name": "playwright-msw",
"version": "1.0.3",
"version": "2.0.0-beta.0",
"description": "A Mock Service Worker API for Playwright.",

@@ -24,3 +24,4 @@ "main": "lib/index.js",

"clean": "rimraf lib",
"prepack": "yarn clean && yarn build"
"prepack": "yarn clean && yarn build",
"test": "jest"
},

@@ -33,8 +34,11 @@ "peerDependencies": {

"@playwright/test": "^1.26.1",
"@types/jest": "^29.2.0",
"@types/node": "^18.8.2",
"@types/rimraf": "^3.0.2",
"jest": "^29.2.2",
"msw": "0.47.4",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
"typescript": "4.7.4"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc