playwright-msw
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -1,3 +0,5 @@ | ||
export declare type Config = { | ||
export type Config = { | ||
graphqlUrl?: string; | ||
waitForPageLoad?: boolean; | ||
}; | ||
export declare const DEFAULT_CONFIG: Config; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DEFAULT_CONFIG = void 0; | ||
exports.DEFAULT_CONFIG = { | ||
graphqlUrl: '/graphql', | ||
waitForPageLoad: true, | ||
}; |
@@ -11,10 +11,8 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.handleRoute = void 0; | ||
const msw_1 = require("msw"); | ||
const events_1 = __importDefault(require("events")); | ||
const emitter = new events_1.default(); | ||
const strict_event_emitter_1 = require("strict-event-emitter"); | ||
const utils_1 = require("./utils"); | ||
const emitter = new strict_event_emitter_1.Emitter(); | ||
const handleRoute = (route, handlers) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -31,16 +29,17 @@ const request = route.request(); | ||
}); | ||
const handleMockResponse = ({ status, headers, body }) => { | ||
var _a; | ||
route.fulfill({ | ||
const handleMockResponse = ({ status, headers, body, delay, }) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _b; | ||
if (delay) { | ||
yield (0, utils_1.wait)(delay); | ||
} | ||
return route.fulfill({ | ||
status, | ||
body: body !== null && body !== void 0 ? body : undefined, | ||
contentType: (_a = headers.get('content-type')) !== null && _a !== void 0 ? _a : undefined, | ||
contentType: (_b = headers.get('content-type')) !== null && _b !== void 0 ? _b : undefined, | ||
headers: headers.all(), | ||
}); | ||
}; | ||
}); | ||
try { | ||
yield (0, msw_1.handleRequest)(mockedRequest, handlers.slice().reverse(), { | ||
onUnhandledRequest: () => { | ||
route.continue(); | ||
}, | ||
onUnhandledRequest: () => route.continue(), | ||
}, emitter, { | ||
@@ -55,5 +54,5 @@ resolutionContext: { | ||
catch (_a) { | ||
route.abort('error'); | ||
yield route.abort(); | ||
} | ||
}); | ||
exports.handleRoute = handleRoute; |
@@ -0,2 +1,3 @@ | ||
export { Config } from './config'; | ||
export { createWorkerFixture } from './fixture'; | ||
export { MockServiceWorker, createWorker } from './worker'; |
import { RequestHandler, Path } from 'msw'; | ||
import { Page, Route, Request } from '@playwright/test'; | ||
import { Config } from './config'; | ||
export declare type RouteHandler = (route: Route, request: Request) => void; | ||
export declare type RouteData = { | ||
export type RouteUrl = string | RegExp; | ||
export type RouteHandler = (route: Route, request: Request) => void; | ||
export type RouteData = { | ||
readonly path: Path; | ||
@@ -16,2 +17,3 @@ readonly routeHandler: RouteHandler; | ||
private isStarted; | ||
private isPageLoaded; | ||
constructor(page: Page, requestHandlers?: RequestHandler[], config?: Config); | ||
@@ -18,0 +20,0 @@ start(): Promise<void>; |
@@ -25,6 +25,4 @@ "use strict"; | ||
const utils_1 = require("./utils"); | ||
const config_1 = require("./config"); | ||
const handler_1 = require("./handler"); | ||
const DEFAULT_CONFIG = { | ||
graphqlUrl: '/graphql', | ||
}; | ||
class Router { | ||
@@ -34,5 +32,6 @@ constructor(page, requestHandlers, config) { | ||
this.isStarted = false; | ||
this.isPageLoaded = false; | ||
this.page = page; | ||
this.initialRequestHandlers = (requestHandlers !== null && requestHandlers !== void 0 ? requestHandlers : []).slice().reverse(); | ||
this.config = Object.assign(Object.assign({}, DEFAULT_CONFIG), (config !== null && config !== void 0 ? config : {})); | ||
this.config = Object.assign(Object.assign({}, config_1.DEFAULT_CONFIG), (config !== null && config !== void 0 ? config : {})); | ||
} | ||
@@ -47,2 +46,5 @@ start() { | ||
} | ||
this.page.on('load', () => { | ||
this.isPageLoaded = true; | ||
}); | ||
this.isStarted = true; | ||
@@ -114,4 +116,7 @@ }); | ||
var _a, _b; | ||
if (this.config.waitForPageLoad && !this.isPageLoaded) { | ||
return route.continue(); | ||
} | ||
const requestHandlers = (_b = (_a = this.getRouteData(path)) === null || _a === void 0 ? void 0 : _a.requestHandlers) !== null && _b !== void 0 ? _b : []; | ||
(0, handler_1.handleRoute)(route, requestHandlers); | ||
return (0, handler_1.handleRoute)(route, requestHandlers); | ||
}; | ||
@@ -126,3 +131,3 @@ yield this.page.route((0, utils_1.convertMswPathToPlaywrightUrl)(path), routeHandler); | ||
if (data) { | ||
this.page.unroute((0, utils_1.convertMswPathToPlaywrightUrl)(data.path), data.routeHandler); | ||
yield this.page.unroute((0, utils_1.convertMswPathToPlaywrightUrl)(data.path), data.routeHandler); | ||
} | ||
@@ -129,0 +134,0 @@ }); |
import { Path, RequestHandler } from 'msw'; | ||
import { Config } from './config'; | ||
export declare type SerializedPathType = 'regexp' | 'string'; | ||
export declare type SerializedPath = `${SerializedPathType}:${string}`; | ||
export type SerializedPathType = 'regexp' | 'string'; | ||
export type SerializedPath = `${SerializedPathType}:${string}`; | ||
export declare const serializePath: (path: Path) => SerializedPath; | ||
@@ -10,1 +10,2 @@ export declare const deserializePath: (serializedPath: SerializedPath) => Path; | ||
export declare const convertMswPathToPlaywrightUrl: (path: Path) => RegExp; | ||
export declare const wait: (delay: number) => Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertMswPathToPlaywrightUrl = exports.getHandlerPath = exports.getHandlerType = exports.deserializePath = exports.serializePath = void 0; | ||
exports.wait = exports.convertMswPathToPlaywrightUrl = exports.getHandlerPath = exports.getHandlerType = exports.deserializePath = exports.serializePath = void 0; | ||
const serializePath = (path) => path instanceof RegExp ? `regexp:${path.source}` : `string:${path}`; | ||
@@ -44,1 +44,7 @@ exports.serializePath = serializePath; | ||
exports.convertMswPathToPlaywrightUrl = convertMswPathToPlaywrightUrl; | ||
const wait = (delay) => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, delay); | ||
}); | ||
}; | ||
exports.wait = wait; |
import type { Page } from '@playwright/test'; | ||
import type { RequestHandler } from 'msw'; | ||
import { Config } from './config'; | ||
export declare type MockServiceWorker = { | ||
export type MockServiceWorker = { | ||
use: (...customHandlers: RequestHandler[]) => Promise<void>; | ||
@@ -6,0 +6,0 @@ resetHandlers: (...customHandlers: RequestHandler[]) => Promise<void>; |
{ | ||
"name": "playwright-msw", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A Mock Service Worker API for Playwright.", | ||
@@ -31,23 +31,24 @@ "main": "lib/index.js", | ||
"@playwright/test": ">=1.20.0", | ||
"msw": ">=0.44.0" | ||
"msw": ">=0.47.3" | ||
}, | ||
"dependencies": { | ||
"@mswjs/cookies": "^0.2.2" | ||
"@mswjs/cookies": "^0.2.2", | ||
"strict-event-emitter": "^0.4.4" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.20.2", | ||
"@babel/core": "^7.20.12", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@jest/globals": "^29.2.2", | ||
"@playwright/test": "^1.27.1", | ||
"@types/babel__core": "^7.1.19", | ||
"@jest/globals": "^29.4.1", | ||
"@playwright/test": "^1.30.0", | ||
"@types/babel__core": "^7.20.0", | ||
"@types/babel__preset-env": "^7.9.2", | ||
"@types/jest": "^29.2.2", | ||
"@types/node": "^18.11.9", | ||
"@types/rimraf": "^3.0.2", | ||
"babel-jest": "^29.2.2", | ||
"jest": "^29.2.2", | ||
"msw": "0.47.4", | ||
"rimraf": "^3.0.2", | ||
"typescript": "4.8.4" | ||
"@types/jest": "^29.4.0", | ||
"@types/node": "^18.11.18", | ||
"@types/rimraf": "3.0.2", | ||
"babel-jest": "^29.4.1", | ||
"jest": "^29.4.1", | ||
"msw": "1.0.0", | ||
"rimraf": "3.0.2", | ||
"typescript": "4.9.5" | ||
}, | ||
@@ -54,0 +55,0 @@ "tags": [ |
@@ -147,6 +147,9 @@ <h1 align="center">Playwright MSW</h1> | ||
| key | required | default | description | | ||
| ---------- | -------- | ---------- | ---------------------------------------------------- | | ||
| graphqlUrl | false | `/graphql` | The URL of the GraphQL endpoint to send requests to. | | ||
| key | required | default | description | | ||
| --------------- | -------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| graphqlUrl | false | `"/graphql"` | The URL of the GraphQL endpoint to send requests to. | | ||
| waitForPageLoad | false | `true` | Waits for the page to load before mocking API calls. When enabled, it allows `playwright-msw` to mirror the behaviour of `msw` when it is running in the browser, where the initial static resource requests will not be mocked because `msw` will have only been initialized until after page load. | | ||
When enabled, it allows `playwright-msw` to emulate the behavior of `msw` when running in the browser, i.e. initialize after page load | | ||
### `createWorker` | ||
@@ -153,0 +156,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
375
235
30060
4
16
+ Addedstrict-event-emitter@^0.4.4
+ Addedstrict-event-emitter@0.4.6(transitive)