Commit f2839a13 authored by Cursor Agent's avatar Cursor Agent

Combine dynamic-routing tests into single file with describe.each

- Merged index.test.ts and middleware.test.ts into a single test file
- Used describe.each([{ withMiddleware: false }, { withMiddleware: true }]) to run tests with and without middleware
- Replaced process.env.__MIDDLEWARE_TEST checks with withMiddleware parameter
- This eliminates test duplication in CI and makes the test suite structure clearer
Co-authored-by: 's avatarSebastian "Sebbie" Silbermann <silbermann.sebastian@gmail.com>
parent 7429f7ce
...@@ -26,7 +26,7 @@ let buildId ...@@ -26,7 +26,7 @@ let buildId
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const buildIdPath = join(appDir, '.next/BUILD_ID') const buildIdPath = join(appDir, '.next/BUILD_ID')
function runTests({ dev }) { function runTests({ dev, withMiddleware }: { dev: boolean; withMiddleware: boolean }) {
if (!dev) { if (!dev) {
it('should have correct cache entries on prefetch', async () => { it('should have correct cache entries on prefetch', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
...@@ -46,7 +46,7 @@ function runTests({ dev }) { ...@@ -46,7 +46,7 @@ function runTests({ dev }) {
const cacheKeys = await getCacheKeys() const cacheKeys = await getCacheKeys()
expect(cacheKeys).toEqual( expect(cacheKeys).toEqual(
process.env.__MIDDLEWARE_TEST withMiddleware
? [ ? [
'/_next/data/BUILD_ID/[name].json?another=value&name=%5Bname%5D', '/_next/data/BUILD_ID/[name].json?another=value&name=%5Bname%5D',
'/_next/data/BUILD_ID/added-later/first.json?name=added-later&comment=first', '/_next/data/BUILD_ID/added-later/first.json?name=added-later&comment=first',
...@@ -117,7 +117,7 @@ function runTests({ dev }) { ...@@ -117,7 +117,7 @@ function runTests({ dev }) {
const newCacheKeys = await getCacheKeys() const newCacheKeys = await getCacheKeys()
expect(newCacheKeys).toEqual( expect(newCacheKeys).toEqual(
[ [
...(process.env.__MIDDLEWARE_TEST ...(withMiddleware
? // data route is fetched with middleware due to query hydration ? // data route is fetched with middleware due to query hydration
// since middleware matches the index route // since middleware matches the index route
['/_next/data/BUILD_ID/index.json'] ['/_next/data/BUILD_ID/index.json']
...@@ -1205,7 +1205,7 @@ function runTests({ dev }) { ...@@ -1205,7 +1205,7 @@ function runTests({ dev }) {
expect(text).toBe('slug: first') expect(text).toBe('slug: first')
}) })
if (!process.env.__MIDDLEWARE_TEST) { if (!withMiddleware) {
it('should show error when interpolating fails for href', async () => { it('should show error when interpolating fails for href', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
await browser await browser
...@@ -1583,11 +1583,13 @@ function runTests({ dev }) { ...@@ -1583,11 +1583,13 @@ function runTests({ dev }) {
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
describe('Dynamic Routing', () => { describe.each([{ withMiddleware: false }, { withMiddleware: true }])(
if (process.env.__MIDDLEWARE_TEST) { 'Dynamic Routing (withMiddleware: $withMiddleware)',
({ withMiddleware }) => {
const middlewarePath = join(__dirname, '../middleware.js') const middlewarePath = join(__dirname, '../middleware.js')
beforeAll(async () => { beforeAll(async () => {
if (withMiddleware) {
await fs.writeFile( await fs.writeFile(
middlewarePath, middlewarePath,
` `
...@@ -1597,9 +1599,14 @@ describe('Dynamic Routing', () => { ...@@ -1597,9 +1599,14 @@ describe('Dynamic Routing', () => {
} }
` `
) )
}
}) })
afterAll(() => fs.remove(middlewarePath))
afterAll(async () => {
if (withMiddleware) {
await fs.remove(middlewarePath)
} }
})
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)( ;(process.env.TURBOPACK_BUILD ? describe.skip : describe)(
'development mode', 'development mode',
...@@ -1613,7 +1620,7 @@ describe('Dynamic Routing', () => { ...@@ -1613,7 +1620,7 @@ describe('Dynamic Routing', () => {
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
runTests({ dev: true }) runTests({ dev: true, withMiddleware })
} }
) )
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
...@@ -1630,7 +1637,8 @@ describe('Dynamic Routing', () => { ...@@ -1630,7 +1637,8 @@ describe('Dynamic Routing', () => {
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
runTests({ dev: false }) runTests({ dev: false, withMiddleware })
} }
) )
}) }
)
/* eslint-env jest */
process.env.__MIDDLEWARE_TEST = '1'
// run all existing tests from ./index.test.js with middleware
// setup enabled via the above env variable
require('./index.test')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment