-
Notifications
You must be signed in to change notification settings - Fork 27k
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 (#52520)
An experimental test mode that enables integration tests to mock server-side fetch requests in Playwright tests. For explanation on how this works, see the [`next-playwright/README.md`](https://github.com/vercel/next.js/pull/52520/files#diff-3b8da7782c16f015df5afafe0ac11247f5b8e5a1c0dbede341ca2b5124dfd924).
- Loading branch information
Showing
28 changed files
with
1,071 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../../dist/experimental/testmode/proxy' |
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/proxy') |
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: 'npm 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/) | ||
}) | ||
``` |
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
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' | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
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 |
115 changes: 115 additions & 0 deletions
115
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,115 @@ | ||
import { test as base } from './index' | ||
import type { NextFixture } from './next-fixture' | ||
import { | ||
type RequestHandler, | ||
type MockedResponse, | ||
MockedRequest, | ||
handleRequest, | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
} from 'msw' | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { Emitter } from 'strict-event-emitter' | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
export * from 'msw' | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
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 |
Oops, something went wrong.