File size: 3,210 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# Experimental test mode for Playwright
### Prerequisites
You have a Next.js project.
### Update `next.config.js`
```javascript
module.exports = {
experimental: {
testProxy: true,
},
}
```
### 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 run dev',
url: 'http://localhost:3000',
},
})
```
### Use the `next/experimental/testmode/playwright` to create tests
```javascript
// Place this file in the `app` directory and name it with `.spec.ts`.
// To customize where to put tests, add `testMatch` to `playwright.config.ts`.
import { test, expect } from 'next/experimental/testmode/playwright'
test('/product/shoe', async ({ page, next }) => {
// NOTE: `next.onFetch` only intercepts external `fetch` requests (for both client and server).
// For example, if you `fetch` a relative URL (e.g. `/api/hello`) from the client
// that's handled by a Next.js route handler (e.g. `app/api/hello/route.ts`),
// it won't be intercepted.
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,
http,
HttpResponse,
passthrough,
} from 'next/experimental/testmode/playwright/msw'
test.use({
mswHandlers: [
[
http.get('http://my-db/product/shoe', () => {
return HttpResponse.json({
title: 'A shoe',
})
}),
// allow all non-mocked routes to pass through
http.all('*', () => {
return passthrough()
}),
],
{ scope: 'test' }, // or 'worker'
],
})
test('/product/shoe', async ({ page, msw }) => {
msw.use(
http.get('http://my-db/product/boot', () => {
return HttpResponse.json({
title: 'A boot',
})
})
)
await page.goto('/product/boot')
await expect(page.locator('body')).toHaveText(/Boot/)
})
```
### Or use your favorite Fetch mocking library
The "fetch loopback" mode can be configured in the `playwright.config.ts` or
via `test.use()` with a test module. This option loops `fetch()` calls via
the `fetch()` of the current test's worker.
```javascript
import { test, expect } from 'next/experimental/testmode/playwright'
import { myFetchMocker } from 'my-fetch-mocker'
test.use({ nextOptions: { fetchLoopback: true } })
test('/product/shoe', async ({ page, next }) => {
myFetchMocker.mock('http://my-db/product/shoe', {
title: 'A shoe',
})
await page.goto('/product/shoe')
await expect(page.locator('body')).toHaveText(/Shoe/)
})
```
|