@msw/playwright 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/build/index.d.ts +25 -4
- package/build/index.js +25 -6
- package/package.json +1 -1
- package/src/index.ts +36 -14
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ await page.evaluate(() => {
|
|
|
13
13
|
// functions from the `msw` package you want to use since
|
|
14
14
|
// you cannot reference them in `page.evaluate` directly.
|
|
15
15
|
const { worker, http, graphql } = window.msw
|
|
16
|
-
worker.use(...)
|
|
16
|
+
worker.use(...overrides)
|
|
17
17
|
})
|
|
18
18
|
```
|
|
19
19
|
|
|
@@ -30,16 +30,16 @@ npm i msw @msw/playwright
|
|
|
30
30
|
```ts
|
|
31
31
|
// playwright.setup.ts
|
|
32
32
|
import { test as testBase } from '@playwright/test'
|
|
33
|
-
import {
|
|
33
|
+
import { createNetworkFixture, type NetworkFixture } from '@msw/playwright'
|
|
34
34
|
import { handlers } from '../mocks/handlers.js'
|
|
35
35
|
|
|
36
36
|
interface Fixtures {
|
|
37
|
-
|
|
37
|
+
network: NetworkFixture
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
export const test = testBase.extend<Fixtures>({
|
|
41
|
-
// Create
|
|
42
|
-
|
|
41
|
+
// Create a fixture that will control the network in your tests.
|
|
42
|
+
network: createNetworkFixture({
|
|
43
43
|
initialHandlers: handlers,
|
|
44
44
|
}),
|
|
45
45
|
})
|
|
@@ -49,10 +49,10 @@ export const test = testBase.extend<Fixtures>({
|
|
|
49
49
|
import { http, HttpResponse } from 'msw'
|
|
50
50
|
import { test } from './playwright.setup.js'
|
|
51
51
|
|
|
52
|
-
test('displays the user dashboard', async ({
|
|
53
|
-
// Access and use
|
|
52
|
+
test('displays the user dashboard', async ({ network, page }) => {
|
|
53
|
+
// Access the network fixture and use it as the `setupWorker()` API.
|
|
54
54
|
// No more disrupted context between processes.
|
|
55
|
-
|
|
55
|
+
network.use(
|
|
56
56
|
http.get('/user', () => {
|
|
57
57
|
return HttpResponse.json({
|
|
58
58
|
id: 'abc-123',
|
package/build/index.d.ts
CHANGED
|
@@ -2,11 +2,32 @@ import { LifeCycleEventsMap, RequestHandler, SetupApi, WebSocketHandler } from "
|
|
|
2
2
|
import { Page, TestFixture } from "@playwright/test";
|
|
3
3
|
|
|
4
4
|
//#region src/index.d.ts
|
|
5
|
-
interface
|
|
5
|
+
interface CreateNetworkFixtureArgs {
|
|
6
6
|
initialHandlers: Array<RequestHandler | WebSocketHandler>;
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Creates a fixture that controls the network in your tests.
|
|
10
|
+
*
|
|
11
|
+
* @note The returned fixture already has the `auto` option set to `true`.
|
|
12
|
+
*
|
|
13
|
+
* **Usage**
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { test as testBase } from '@playwright/test'
|
|
16
|
+
* import { createNetworkFixture, type WorkerFixture } from '@msw/playwright'
|
|
17
|
+
*
|
|
18
|
+
* interface Fixtures {
|
|
19
|
+
* network: WorkerFixture
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* export const test = testBase.extend<Fixtures>({
|
|
23
|
+
* network: createNetworkFixture()
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare function createNetworkFixture(args?: CreateNetworkFixtureArgs): [TestFixture<NetworkFixture, any>, {
|
|
28
|
+
auto: boolean;
|
|
29
|
+
}];
|
|
30
|
+
declare class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
|
|
10
31
|
#private;
|
|
11
32
|
constructor(args: {
|
|
12
33
|
page: Page;
|
|
@@ -16,4 +37,4 @@ declare class WorkerFixture extends SetupApi<LifeCycleEventsMap> {
|
|
|
16
37
|
stop(): Promise<void>;
|
|
17
38
|
}
|
|
18
39
|
//#endregion
|
|
19
|
-
export {
|
|
40
|
+
export { CreateNetworkFixtureArgs, NetworkFixture, createNetworkFixture };
|
package/build/index.js
CHANGED
|
@@ -3,18 +3,37 @@ import { RequestHandler, SetupApi, WebSocketHandler, getResponse } from "msw";
|
|
|
3
3
|
import { CancelableCloseEvent, CancelableMessageEvent } from "@mswjs/interceptors/WebSocket";
|
|
4
4
|
|
|
5
5
|
//#region src/index.ts
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Creates a fixture that controls the network in your tests.
|
|
8
|
+
*
|
|
9
|
+
* @note The returned fixture already has the `auto` option set to `true`.
|
|
10
|
+
*
|
|
11
|
+
* **Usage**
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { test as testBase } from '@playwright/test'
|
|
14
|
+
* import { createNetworkFixture, type WorkerFixture } from '@msw/playwright'
|
|
15
|
+
*
|
|
16
|
+
* interface Fixtures {
|
|
17
|
+
* network: WorkerFixture
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* export const test = testBase.extend<Fixtures>({
|
|
21
|
+
* network: createNetworkFixture()
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function createNetworkFixture(args) {
|
|
26
|
+
return [async ({ page }, use) => {
|
|
27
|
+
const worker = new NetworkFixture({
|
|
9
28
|
page,
|
|
10
29
|
initialHandlers: args?.initialHandlers || []
|
|
11
30
|
});
|
|
12
31
|
await worker.start();
|
|
13
32
|
await use(worker);
|
|
14
33
|
await worker.stop();
|
|
15
|
-
};
|
|
34
|
+
}, { auto: true }];
|
|
16
35
|
}
|
|
17
|
-
var
|
|
36
|
+
var NetworkFixture = class extends SetupApi {
|
|
18
37
|
#page;
|
|
19
38
|
constructor(args) {
|
|
20
39
|
super(...args.initialHandlers);
|
|
@@ -203,4 +222,4 @@ var PlaywrightWebSocketServerConnection = class {
|
|
|
203
222
|
};
|
|
204
223
|
|
|
205
224
|
//#endregion
|
|
206
|
-
export {
|
|
225
|
+
export { NetworkFixture, createNetworkFixture };
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -17,27 +17,49 @@ import {
|
|
|
17
17
|
WebSocketServerConnectionProtocol,
|
|
18
18
|
} from '@mswjs/interceptors/WebSocket'
|
|
19
19
|
|
|
20
|
-
export interface
|
|
20
|
+
export interface CreateNetworkFixtureArgs {
|
|
21
21
|
initialHandlers: Array<RequestHandler | WebSocketHandler>
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Creates a fixture that controls the network in your tests.
|
|
26
|
+
*
|
|
27
|
+
* @note The returned fixture already has the `auto` option set to `true`.
|
|
28
|
+
*
|
|
29
|
+
* **Usage**
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { test as testBase } from '@playwright/test'
|
|
32
|
+
* import { createNetworkFixture, type WorkerFixture } from '@msw/playwright'
|
|
33
|
+
*
|
|
34
|
+
* interface Fixtures {
|
|
35
|
+
* network: WorkerFixture
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* export const test = testBase.extend<Fixtures>({
|
|
39
|
+
* network: createNetworkFixture()
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function createNetworkFixture(
|
|
44
|
+
args?: CreateNetworkFixtureArgs,
|
|
26
45
|
/** @todo `onUnhandledRequest`? */
|
|
27
|
-
): TestFixture<
|
|
28
|
-
return
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
): [TestFixture<NetworkFixture, any>, { auto: boolean }] {
|
|
47
|
+
return [
|
|
48
|
+
async ({ page }, use) => {
|
|
49
|
+
const worker = new NetworkFixture({
|
|
50
|
+
page,
|
|
51
|
+
initialHandlers: args?.initialHandlers || [],
|
|
52
|
+
})
|
|
33
53
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
54
|
+
await worker.start()
|
|
55
|
+
await use(worker)
|
|
56
|
+
await worker.stop()
|
|
57
|
+
},
|
|
58
|
+
{ auto: true },
|
|
59
|
+
]
|
|
38
60
|
}
|
|
39
61
|
|
|
40
|
-
export class
|
|
62
|
+
export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
|
|
41
63
|
#page: Page
|
|
42
64
|
|
|
43
65
|
constructor(args: {
|