-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Concept: test mode for Playwright and similar integration tools
- Loading branch information
Showing
27 changed files
with
6,714 additions
and
6,876 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../../dist/experimental/testmode/playwright' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../dist/experimental/testmode/playwright') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../../../dist/experimental/testmode/playwright/msw' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../../dist/experimental/testmode/playwright/msw') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/next/src/experimental/testmode/playwright/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Experimental test mode for Playwright | ||
|
||
### Prerequisites | ||
|
||
You have a Next.js project. | ||
|
||
### Install `@playwright/test` in your project | ||
|
||
```sh | ||
npm install -D @playwright/test | ||
``` | ||
|
||
### Optionally install MSW in your project | ||
|
||
[MSW](https://mswjs.io/) can be helpful for fetch mocking. | ||
|
||
```sh | ||
npm install -D msw | ||
``` | ||
|
||
### Update `playwright.config.ts` | ||
|
||
```javascript | ||
import { defineConfig } from 'next/experimental/testmode/playwright' | ||
|
||
export default defineConfig({ | ||
webServer: { | ||
command: 'pnpm dev -- --experimental-test-proxy', | ||
url: 'http://localhost:3000', | ||
}, | ||
}) | ||
``` | ||
|
||
### Use the `next/experimental/testmode/playwright` to create tests | ||
|
||
```javascript | ||
import { test, expect } from 'next/experimental/testmode/playwright' | ||
|
||
test('/product/shoe', async ({ page, next }) => { | ||
next.onFetch((request) => { | ||
if (request.url === 'http://my-db/product/shoe') { | ||
return new Response( | ||
JSON.stringify({ | ||
title: 'A shoe', | ||
}), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
) | ||
} | ||
return 'abort' | ||
}) | ||
|
||
await page.goto('/product/shoe') | ||
|
||
await expect(page.locator('body')).toHaveText(/Shoe/) | ||
}) | ||
``` | ||
|
||
### Or use the `next/experimental/testmode/playwright/msw` | ||
|
||
```javascript | ||
import { test, expect, rest } from 'next/experimental/testmode/playwright/msw' | ||
|
||
test.use({ | ||
mswHandlers: [ | ||
rest.get('http://my-db/product/shoe', (req, res, ctx) => { | ||
return res( | ||
ctx.status(200), | ||
ctx.json({ | ||
title: 'A shoe', | ||
}) | ||
) | ||
}), | ||
], | ||
}) | ||
|
||
test('/product/shoe', async ({ page, msw }) => { | ||
msw.use( | ||
rest.get('http://my-db/product/boot', (req, res, ctx) => { | ||
return res.once( | ||
ctx.status(200), | ||
ctx.json({ | ||
title: 'A boot', | ||
}) | ||
) | ||
}) | ||
) | ||
|
||
await page.goto('/product/boot') | ||
|
||
await expect(page.locator('body')).toHaveText(/Boot/) | ||
}) | ||
``` |
34 changes: 34 additions & 0 deletions
34
packages/next/src/experimental/testmode/playwright/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { test as base } from '@playwright/test' | ||
import type { NextFixture } from './next-fixture' | ||
import type { NextWorkerFixture } from './next-worker-fixture' | ||
import { applyNextWorkerFixture } from './next-worker-fixture' | ||
import { applyNextFixture } from './next-fixture' | ||
|
||
export * from '@playwright/test' | ||
|
||
export type { NextFixture } | ||
export type { FetchHandlerResult } from '../proxy' | ||
|
||
export const test = base.extend< | ||
{ next: NextFixture }, | ||
{ _nextWorker: NextWorkerFixture } | ||
>({ | ||
_nextWorker: [ | ||
// eslint-disable-next-line no-empty-pattern | ||
async ({}, use) => { | ||
await applyNextWorkerFixture(use) | ||
}, | ||
{ scope: 'worker', auto: true }, | ||
], | ||
|
||
next: async ({ _nextWorker, page, extraHTTPHeaders }, use, testInfo) => { | ||
await applyNextFixture(use, { | ||
testInfo, | ||
nextWorker: _nextWorker, | ||
page, | ||
extraHTTPHeaders, | ||
}) | ||
}, | ||
}) | ||
|
||
export default test |
111 changes: 111 additions & 0 deletions
111
packages/next/src/experimental/testmode/playwright/msw.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { test as base } from './index' | ||
import type { NextFixture } from './next-fixture' | ||
import { | ||
type RequestHandler, | ||
type MockedResponse, | ||
MockedRequest, | ||
handleRequest, | ||
} from 'msw' | ||
import { Emitter } from 'strict-event-emitter' | ||
|
||
export * from 'msw' | ||
export * from '@playwright/test' | ||
export type { NextFixture } | ||
|
||
export interface MswFixture { | ||
use: (...handlers: RequestHandler[]) => void | ||
} | ||
|
||
export const test = base.extend<{ | ||
msw: MswFixture | ||
mswHandlers: RequestHandler[] | ||
}>({ | ||
mswHandlers: [], | ||
|
||
msw: [ | ||
async ({ next, mswHandlers }, use) => { | ||
const handlers: RequestHandler[] = [...mswHandlers] | ||
const emitter = new Emitter() | ||
|
||
next.onFetch(async (request) => { | ||
const { | ||
body, | ||
method, | ||
headers, | ||
credentials, | ||
cache, | ||
redirect, | ||
integrity, | ||
keepalive, | ||
mode, | ||
destination, | ||
referrer, | ||
referrerPolicy, | ||
} = request | ||
const mockedRequest = new MockedRequest(new URL(request.url), { | ||
body: body ? await request.arrayBuffer() : undefined, | ||
method, | ||
headers: Object.fromEntries(headers), | ||
credentials, | ||
cache, | ||
redirect, | ||
integrity, | ||
keepalive, | ||
mode, | ||
destination, | ||
referrer, | ||
referrerPolicy, | ||
}) | ||
let isPassthrough = false | ||
let mockedResponse: MockedResponse | undefined | ||
await handleRequest( | ||
mockedRequest, | ||
handlers.slice(0), | ||
{ onUnhandledRequest: 'error' }, | ||
emitter as any, | ||
{ | ||
onPassthroughResponse: () => { | ||
isPassthrough = true | ||
}, | ||
onMockedResponse: (r) => { | ||
mockedResponse = r | ||
}, | ||
} | ||
) | ||
|
||
if (isPassthrough) { | ||
return 'continue' | ||
} | ||
|
||
if (mockedResponse) { | ||
const { | ||
status, | ||
headers: responseHeaders, | ||
body: responseBody, | ||
delay, | ||
} = mockedResponse | ||
if (delay) { | ||
await new Promise((resolve) => setTimeout(resolve, delay)) | ||
} | ||
return new Response(responseBody, { | ||
status, | ||
headers: new Headers(responseHeaders), | ||
}) | ||
} | ||
|
||
return 'abort' | ||
}) | ||
|
||
await use({ | ||
use: (...newHandlers) => { | ||
handlers.unshift(...newHandlers) | ||
}, | ||
}) | ||
|
||
handlers.length = 0 | ||
}, | ||
{ auto: true }, | ||
], | ||
}) | ||
|
||
export default test |
56 changes: 56 additions & 0 deletions
56
packages/next/src/experimental/testmode/playwright/next-fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Page, TestInfo } from '@playwright/test' | ||
import type { NextWorkerFixture, FetchHandler } from './next-worker-fixture' | ||
import { handleRoute } from './page-route' | ||
|
||
export interface NextFixture { | ||
onFetch: (handler: FetchHandler) => void | ||
} | ||
|
||
class NextFixtureImpl implements NextFixture { | ||
private fetchHandler: FetchHandler | null = null | ||
|
||
constructor( | ||
public testId: string, | ||
private worker: NextWorkerFixture, | ||
private page: Page | ||
) { | ||
this.page.route('**', (route) => | ||
handleRoute(route, page, this.fetchHandler) | ||
) | ||
} | ||
|
||
teardown(): void { | ||
this.worker.cleanupTest(this.testId) | ||
} | ||
|
||
onFetch(handler: FetchHandler): void { | ||
this.fetchHandler = handler | ||
this.worker.onFetch(this.testId, handler) | ||
} | ||
} | ||
|
||
export async function applyNextFixture( | ||
use: (fixture: NextFixture) => Promise<void>, | ||
{ | ||
testInfo, | ||
nextWorker, | ||
page, | ||
extraHTTPHeaders, | ||
}: { | ||
testInfo: TestInfo | ||
nextWorker: NextWorkerFixture | ||
page: Page | ||
extraHTTPHeaders: Record<string, string> | undefined | ||
} | ||
): Promise<void> { | ||
const fixture = new NextFixtureImpl(testInfo.testId, nextWorker, page) | ||
page.setExtraHTTPHeaders({ | ||
...extraHTTPHeaders, | ||
'Next-Test-Proxy-Port': String(nextWorker.proxyPort), | ||
'Next-Test-Data': fixture.testId, | ||
}) | ||
|
||
await use(fixture) | ||
|
||
fixture.teardown() | ||
} |
Oops, something went wrong.