Skip to content

Commit b04d1ca

Browse files
committed
test: move middleware-i18n to test variants
1 parent 45dacbb commit b04d1ca

File tree

9 files changed

+195
-122
lines changed

9 files changed

+195
-122
lines changed

tests/e2e/edge-middleware.test.ts

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ExtendedWindow = Window & {
1111
type ExtendedFixtures = {
1212
edgeOrNodeMiddleware: Fixture
1313
edgeOrNodeMiddlewarePages: Fixture
14+
edgeOrNodeMiddlewareI18n: Fixture
1415
}
1516

1617
for (const { expectedRuntime, label, testWithSwitchableMiddlewareRuntime } of [
@@ -34,6 +35,14 @@ for (const { expectedRuntime, label, testWithSwitchableMiddlewareRuntime } of [
3435
scope: 'worker',
3536
},
3637
],
38+
edgeOrNodeMiddlewareI18n: [
39+
async ({ middlewareI18n }, use) => {
40+
await use(middlewareI18n)
41+
},
42+
{
43+
scope: 'worker',
44+
},
45+
],
3746
}),
3847
},
3948
hasNodeMiddlewareSupport()
@@ -57,6 +66,14 @@ for (const { expectedRuntime, label, testWithSwitchableMiddlewareRuntime } of [
5766
scope: 'worker',
5867
},
5968
],
69+
edgeOrNodeMiddlewareI18n: [
70+
async ({ middlewareI18nNode }, use) => {
71+
await use(middlewareI18nNode)
72+
},
73+
{
74+
scope: 'worker',
75+
},
76+
],
6077
}),
6178
}
6279
: undefined,
@@ -73,6 +90,8 @@ for (const { expectedRuntime, label, testWithSwitchableMiddlewareRuntime } of [
7390

7491
const h1 = page.locator('h1')
7592
await expect(h1).toHaveText('Other')
93+
94+
expect(await res?.headerValue('x-runtime')).toEqual(expectedRuntime)
7695
})
7796

7897
test('Does not run middleware at the origin', async ({ page, edgeOrNodeMiddleware }) => {
@@ -101,12 +120,15 @@ for (const { expectedRuntime, label, testWithSwitchableMiddlewareRuntime } of [
101120
expect(await rewritten?.headerValue('x-added-rewrite-target')).toBeNull()
102121
const h1 = page.locator('h1')
103122
await expect(h1).toHaveText('Hello rewrite')
123+
124+
expect(await direct?.headerValue('x-runtime')).toEqual(expectedRuntime)
104125
})
105126

106127
test('Supports CJS dependencies in Edge Middleware', async ({ page, edgeOrNodeMiddleware }) => {
107128
const res = await page.goto(`${edgeOrNodeMiddleware.url}/test/next`)
108129

109130
expect(await res?.headerValue('x-cjs-module-works')).toEqual('true')
131+
expect(await res?.headerValue('x-runtime')).toEqual(expectedRuntime)
110132
})
111133

112134
if (expectedRuntime !== 'node') {
@@ -212,62 +234,67 @@ for (const { expectedRuntime, label, testWithSwitchableMiddlewareRuntime } of [
212234
})
213235
}
214236
})
215-
if (expectedRuntime !== 'node') {
216-
test.describe('with 18n', () => {
217-
for (const testConfig of testConfigs) {
218-
test.describe(testConfig.describeLabel, () => {
219-
for (const { localeLabel, pageWithLinksPathname } of [
220-
{ localeLabel: 'implicit default locale', pageWithLinksPathname: '/link' },
221-
{ localeLabel: 'explicit default locale', pageWithLinksPathname: '/en/link' },
222-
{ localeLabel: 'explicit non-default locale', pageWithLinksPathname: '/fr/link' },
223-
]) {
224-
test.describe(localeLabel, () => {
225-
test('json data fetch', async ({ middlewareI18n, page }) => {
226-
const dataFetchPromise = new Promise<Response>((resolve) => {
227-
page.on('response', (response) => {
228-
if (response.url().includes(testConfig.jsonPathMatcher)) {
229-
resolve(response)
230-
}
231-
})
237+
238+
test.describe('with 18n', () => {
239+
for (const testConfig of testConfigs) {
240+
test.describe(testConfig.describeLabel, () => {
241+
for (const { localeLabel, pageWithLinksPathname } of [
242+
{ localeLabel: 'implicit default locale', pageWithLinksPathname: '/link' },
243+
{ localeLabel: 'explicit default locale', pageWithLinksPathname: '/en/link' },
244+
{ localeLabel: 'explicit non-default locale', pageWithLinksPathname: '/fr/link' },
245+
]) {
246+
test.describe(localeLabel, () => {
247+
test('json data fetch', async ({ edgeOrNodeMiddlewareI18n, page }) => {
248+
const dataFetchPromise = new Promise<Response>((resolve) => {
249+
page.on('response', (response) => {
250+
if (response.url().includes(testConfig.jsonPathMatcher)) {
251+
resolve(response)
252+
}
232253
})
254+
})
233255

234-
await page.goto(`${middlewareI18n.url}${pageWithLinksPathname}`)
256+
const pageResponse = await page.goto(
257+
`${edgeOrNodeMiddlewareI18n.url}${pageWithLinksPathname}`,
258+
)
259+
expect(await pageResponse?.headerValue('x-runtime')).toEqual(expectedRuntime)
235260

236-
await page.hover(`[data-link="${testConfig.selector}"]`)
261+
await page.hover(`[data-link="${testConfig.selector}"]`)
237262

238-
const dataResponse = await dataFetchPromise
263+
const dataResponse = await dataFetchPromise
239264

240-
expect(dataResponse.ok()).toBe(true)
241-
})
265+
expect(dataResponse.ok()).toBe(true)
266+
})
242267

243-
test('navigation', async ({ middlewareI18n, page }) => {
244-
await page.goto(`${middlewareI18n.url}${pageWithLinksPathname}`)
268+
test('navigation', async ({ edgeOrNodeMiddlewareI18n, page }) => {
269+
const pageResponse = await page.goto(
270+
`${edgeOrNodeMiddlewareI18n.url}${pageWithLinksPathname}`,
271+
)
272+
expect(await pageResponse?.headerValue('x-runtime')).toEqual(expectedRuntime)
245273

246-
await page.evaluate(() => {
247-
// set some value to window to check later if browser did reload and lost this state
248-
;(window as ExtendedWindow).didReload = false
249-
})
274+
await page.evaluate(() => {
275+
// set some value to window to check later if browser did reload and lost this state
276+
;(window as ExtendedWindow).didReload = false
277+
})
250278

251-
await page.click(`[data-link="${testConfig.selector}"]`)
279+
await page.click(`[data-link="${testConfig.selector}"]`)
252280

253-
// wait for page to be rendered
254-
await page.waitForSelector(`[data-page="${testConfig.selector}"]`)
281+
// wait for page to be rendered
282+
await page.waitForSelector(`[data-page="${testConfig.selector}"]`)
255283

256-
// check if browser navigation worked by checking if state was preserved
257-
const browserNavigationWorked =
258-
(await page.evaluate(() => {
259-
return (window as ExtendedWindow).didReload
260-
})) === false
284+
// check if browser navigation worked by checking if state was preserved
285+
const browserNavigationWorked =
286+
(await page.evaluate(() => {
287+
return (window as ExtendedWindow).didReload
288+
})) === false
261289

262-
// we expect client navigation to work without browser reload
263-
expect(browserNavigationWorked).toBe(true)
264-
})
290+
// we expect client navigation to work without browser reload
291+
expect(browserNavigationWorked).toBe(true)
265292
})
266-
}
267-
})
268-
}
269-
})
270-
}
293+
})
294+
}
295+
})
296+
}
297+
})
271298
})
272299

273300
if (expectedRuntime !== 'node') {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { middleware } from './middleware-shared'
2+
3+
export const config = {
4+
runtime: 'nodejs',
5+
}

tests/fixtures/middleware-i18n/middleware.js renamed to tests/fixtures/middleware-i18n/middleware-shared.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
import type { NextRequest } from 'next/server'
12
import { NextResponse } from 'next/server'
23

3-
export async function middleware(request) {
4+
export async function middleware(request: NextRequest) {
5+
const response = getResponse(request)
6+
7+
if (response) {
8+
response.headers.append('Deno' in globalThis ? 'x-deno' : 'x-node', Date.now().toString())
9+
// report Next.js Middleware Runtime (not the execution runtime, but target runtime)
10+
// @ts-expect-error EdgeRuntime global not declared
11+
response.headers.append('x-runtime', typeof EdgeRuntime !== 'undefined' ? EdgeRuntime : 'node')
12+
response.headers.set('x-hello-from-middleware-res', 'hello')
13+
}
14+
}
15+
16+
const getResponse = (request: NextRequest) => {
417
const url = request.nextUrl
518

619
// this is needed for tests to get the BUILD_ID
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { middleware } from './middleware-shared'

tests/fixtures/middleware-i18n/next.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
output: 'standalone',
3+
distDir: process.env.NEXT_DIST_DIR ?? '.next',
34
eslint: {
45
ignoreDuringBuilds: true,
56
},
@@ -10,6 +11,7 @@ module.exports = {
1011
experimental: {
1112
clientRouterFilter: true,
1213
clientRouterFilterRedirects: true,
14+
nodeMiddleware: true,
1315
},
1416
redirects() {
1517
return [

tests/fixtures/middleware-i18n/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"postinstall": "next build",
6+
"postinstall": "npm run build",
77
"dev": "next dev",
8-
"build": "next build"
8+
"build": "node ../../utils/build-variants.mjs"
99
},
1010
"dependencies": {
1111
"next": "latest",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"node-middleware": {
3+
"distDir": ".next-node-middleware",
4+
"files": {
5+
"middleware.ts": "middleware-node.ts"
6+
},
7+
"test": {
8+
"dependencies": {
9+
"next": [
10+
{
11+
"versionConstraint": ">=15.2.0",
12+
"canaryOnly": true
13+
},
14+
{
15+
"versionConstraint": ">=15.5.0"
16+
}
17+
]
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)