Unverified Commit 646ae98d authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Skip more production tests for turbopack (#56084)

Follow-up to #56045.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent ef4faa5f
...@@ -1369,20 +1369,72 @@ export const setupTests = (ctx) => { ...@@ -1369,20 +1369,72 @@ export const setupTests = (ctx) => {
runTests(curCtx) runTests(curCtx)
}) })
;(process.env.TURBOPACK ? describe.skip : describe)(
'Production Mode Server support w/o next.config.js',
() => {
if (ctx.nextConfigImages) {
// skip this test because it requires next.config.js
return
}
const size = 384 // defaults defined in server/config.ts
const curCtx = {
...ctx,
w: size,
isDev: false,
}
beforeAll(async () => {
curCtx.nextOutput = ''
await nextBuild(curCtx.appDir)
await cleanImagesDir(ctx)
curCtx.appPort = await findPort()
curCtx.app = await nextStart(curCtx.appDir, curCtx.appPort, {
onStderr(msg) {
curCtx.nextOutput += msg
},
env: {
NEXT_SHARP_PATH: curCtx.isSharp
? join(curCtx.appDir, 'node_modules', 'sharp')
: '',
},
cwd: curCtx.appDir,
})
})
afterAll(async () => {
if (curCtx.app) await killApp(curCtx.app)
})
describe('Server support w/o next.config.js', () => { runTests(curCtx)
if (ctx.nextConfigImages) {
// skip this test because it requires next.config.js
return
} }
const size = 384 // defaults defined in server/config.ts )
}
;(process.env.TURBOPACK ? describe.skip : describe)(
'Production Mode Server support with next.config.js',
() => {
const size = 399
const curCtx = { const curCtx = {
...ctx, ...ctx,
w: size, w: size,
isDev: false, isDev: false,
nextConfigImages: {
domains: [
'localhost',
'127.0.0.1',
'example.com',
'assets.vercel.com',
'image-optimization-test.vercel.app',
],
formats: ['image/avif', 'image/webp'],
deviceSizes: [size, largeSize],
...ctx.nextConfigImages,
},
} }
beforeAll(async () => { beforeAll(async () => {
const json = JSON.stringify({
images: curCtx.nextConfigImages,
})
curCtx.nextOutput = '' curCtx.nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json)
await nextBuild(curCtx.appDir) await nextBuild(curCtx.appDir)
await cleanImagesDir(ctx) await cleanImagesDir(ctx)
curCtx.appPort = await findPort() curCtx.appPort = await findPort()
...@@ -1399,58 +1451,11 @@ export const setupTests = (ctx) => { ...@@ -1399,58 +1451,11 @@ export const setupTests = (ctx) => {
}) })
}) })
afterAll(async () => { afterAll(async () => {
nextConfig.restore()
if (curCtx.app) await killApp(curCtx.app) if (curCtx.app) await killApp(curCtx.app)
}) })
runTests(curCtx) runTests(curCtx)
})
}
describe('Server support with next.config.js', () => {
const size = 399
const curCtx = {
...ctx,
w: size,
isDev: false,
nextConfigImages: {
domains: [
'localhost',
'127.0.0.1',
'example.com',
'assets.vercel.com',
'image-optimization-test.vercel.app',
],
formats: ['image/avif', 'image/webp'],
deviceSizes: [size, largeSize],
...ctx.nextConfigImages,
},
} }
beforeAll(async () => { )
const json = JSON.stringify({
images: curCtx.nextConfigImages,
})
curCtx.nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json)
await nextBuild(curCtx.appDir)
await cleanImagesDir(ctx)
curCtx.appPort = await findPort()
curCtx.app = await nextStart(curCtx.appDir, curCtx.appPort, {
onStderr(msg) {
curCtx.nextOutput += msg
},
env: {
NEXT_SHARP_PATH: curCtx.isSharp
? join(curCtx.appDir, 'node_modules', 'sharp')
: '',
},
cwd: curCtx.appDir,
})
})
afterAll(async () => {
nextConfig.restore()
if (curCtx.app) await killApp(curCtx.app)
})
runTests(curCtx)
})
} }
...@@ -12,528 +12,543 @@ let nextApp ...@@ -12,528 +12,543 @@ let nextApp
let appPort let appPort
let buildId let buildId
let requiredFilesManifest let requiredFilesManifest
;(process.env.TURBOPACK ? describe.skip : describe)(
'Required Server Files',
() => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir, undefined, {
env: {
NOW_BUILDER: '1',
},
})
describe('Required Server Files', () => { buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
beforeAll(async () => { requiredFilesManifest = await fs.readJSON(
await fs.remove(join(appDir, '.next')) join(appDir, '.next/required-server-files.json')
await nextBuild(appDir, undefined, { )
env: {
NOW_BUILDER: '1', let files = await fs.readdir(join(appDir, '.next'))
},
}) for (const file of files) {
if (
file === 'server' ||
file === 'required-server-files.json' ||
requiredFilesManifest.files.includes(join('.next', file))
) {
continue
}
console.log('removing', join('.next', file))
await fs.remove(join(appDir, '.next', file))
}
await fs.rename(join(appDir, 'pages'), join(appDir, 'pages-bak'))
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') nextApp = nextServer({
requiredFilesManifest = await fs.readJSON( conf: {},
join(appDir, '.next/required-server-files.json') dir: appDir,
) quiet: false,
minimalMode: true,
})
let files = await fs.readdir(join(appDir, '.next')) server = await startApp(nextApp)
appPort = server.address().port
for (const file of files) { console.log(`Listening at ::${appPort}`)
if ( })
file === 'server' || afterAll(async () => {
file === 'required-server-files.json' || if (server) server.close()
requiredFilesManifest.files.includes(join('.next', file)) await fs.rename(join(appDir, 'pages-bak'), join(appDir, 'pages'))
) { })
continue
it('should output required-server-files manifest correctly', async () => {
expect(requiredFilesManifest.version).toBe(1)
expect(Array.isArray(requiredFilesManifest.files)).toBe(true)
expect(Array.isArray(requiredFilesManifest.ignore)).toBe(true)
expect(requiredFilesManifest.files.length).toBeGreaterThan(0)
expect(requiredFilesManifest.ignore.length).toBeGreaterThan(0)
expect(typeof requiredFilesManifest.config.configFile).toBe('undefined')
expect(typeof requiredFilesManifest.config.trailingSlash).toBe('boolean')
expect(typeof requiredFilesManifest.appDir).toBe('string')
for (const file of requiredFilesManifest.files) {
console.log('checking', file)
expect(await fs.exists(join(appDir, file))).toBe(true)
} }
console.log('removing', join('.next', file))
await fs.remove(join(appDir, '.next', file)) expect(await fs.exists(join(appDir, '.next/server'))).toBe(true)
}
await fs.rename(join(appDir, 'pages'), join(appDir, 'pages-bak'))
nextApp = nextServer({
conf: {},
dir: appDir,
quiet: false,
minimalMode: true,
}) })
server = await startApp(nextApp) it('should render SSR page correctly', async () => {
appPort = server.address().port const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
console.log(`Listening at ::${appPort}`) const data = JSON.parse($('#props').text())
})
afterAll(async () => { expect($('#index').text()).toBe('index page')
if (server) server.close() expect(data.hello).toBe('world')
await fs.rename(join(appDir, 'pages-bak'), join(appDir, 'pages'))
}) const html2 = await renderViaHTTP(appPort, '/')
const $2 = cheerio.load(html2)
it('should output required-server-files manifest correctly', async () => { const data2 = JSON.parse($2('#props').text())
expect(requiredFilesManifest.version).toBe(1)
expect(Array.isArray(requiredFilesManifest.files)).toBe(true) expect($2('#index').text()).toBe('index page')
expect(Array.isArray(requiredFilesManifest.ignore)).toBe(true) expect(isNaN(data2.random)).toBe(false)
expect(requiredFilesManifest.files.length).toBeGreaterThan(0) expect(data2.random).not.toBe(data.random)
expect(requiredFilesManifest.ignore.length).toBeGreaterThan(0)
expect(typeof requiredFilesManifest.config.configFile).toBe('undefined')
expect(typeof requiredFilesManifest.config.trailingSlash).toBe('boolean')
expect(typeof requiredFilesManifest.appDir).toBe('string')
for (const file of requiredFilesManifest.files) {
console.log('checking', file)
expect(await fs.exists(join(appDir, file))).toBe(true)
}
expect(await fs.exists(join(appDir, '.next/server'))).toBe(true)
})
it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#index').text()).toBe('index page')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/')
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#index').text()).toBe('index page')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
it('should render dynamic SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/dynamic/first')
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#dynamic').text()).toBe('dynamic page')
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/dynamic/second')
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#dynamic').text()).toBe('dynamic page')
expect($2('#slug').text()).toBe('second')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
it('should render fallback page correctly', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first')
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#fallback').text()).toBe('fallback page')
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
await waitFor(2000)
const html2 = await renderViaHTTP(appPort, '/fallback/first')
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#fallback').text()).toBe('fallback page')
expect($2('#slug').text()).toBe('first')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
const html3 = await renderViaHTTP(appPort, '/fallback/second')
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
expect($3('#fallback').text()).toBe('fallback page')
expect($3('#slug').text()).toBe('second')
expect(isNaN(data3.random)).toBe(false)
const { pageProps: data4 } = JSON.parse(
await renderViaHTTP(appPort, `/_next/data/${buildId}/fallback/third.json`)
)
expect(data4.hello).toBe('world')
expect(data4.slug).toBe('third')
})
it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/',
},
}) })
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#index').text()).toBe('index page') it('should render dynamic SSR page correctly', async () => {
expect(data.hello).toBe('world') const html = await renderViaHTTP(appPort, '/dynamic/first')
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#dynamic').text()).toBe('dynamic page')
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/dynamic/second')
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, { expect($2('#dynamic').text()).toBe('dynamic page')
headers: { expect($2('#slug').text()).toBe('second')
'x-matched-path': '/', expect(isNaN(data2.random)).toBe(false)
}, expect(data2.random).not.toBe(data.random)
}) })
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text()) it('should render fallback page correctly', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first')
expect($2('#index').text()).toBe('index page') const $ = cheerio.load(html)
expect(isNaN(data2.random)).toBe(false) const data = JSON.parse($('#props').text())
expect(data2.random).not.toBe(data.random)
}) expect($('#fallback').text()).toBe('fallback page')
expect($('#slug').text()).toBe('first')
it('should render dynamic SSR page correctly with x-matched-path', async () => { expect(data.hello).toBe('world')
const html = await renderViaHTTP(
appPort, await waitFor(2000)
'/some-other-path?nxtPslug=first', const html2 = await renderViaHTTP(appPort, '/fallback/first')
undefined, const $2 = cheerio.load(html2)
{ const data2 = JSON.parse($2('#props').text())
headers: {
'x-matched-path': '/dynamic/[slug]', expect($2('#fallback').text()).toBe('fallback page')
}, expect($2('#slug').text()).toBe('first')
} expect(isNaN(data2.random)).toBe(false)
) expect(data2.random).not.toBe(data.random)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text()) const html3 = await renderViaHTTP(appPort, '/fallback/second')
const $3 = cheerio.load(html3)
expect($('#dynamic').text()).toBe('dynamic page') const data3 = JSON.parse($3('#props').text())
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world') expect($3('#fallback').text()).toBe('fallback page')
expect($3('#slug').text()).toBe('second')
const html2 = await renderViaHTTP( expect(isNaN(data3.random)).toBe(false)
appPort,
'/some-other-path?slug=second', const { pageProps: data4 } = JSON.parse(
undefined, await renderViaHTTP(
{ appPort,
`/_next/data/${buildId}/fallback/third.json`
)
)
expect(data4.hello).toBe('world')
expect(data4.slug).toBe('third')
})
it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: { headers: {
'x-matched-path': '/dynamic/[slug]', 'x-matched-path': '/',
}, },
} })
) const $ = cheerio.load(html)
const $2 = cheerio.load(html2) const data = JSON.parse($('#props').text())
const data2 = JSON.parse($2('#props').text())
expect($('#index').text()).toBe('index page')
expect($2('#dynamic').text()).toBe('dynamic page') expect(data.hello).toBe('world')
expect($2('#slug').text()).toBe('second')
expect(isNaN(data2.random)).toBe(false) const html2 = await renderViaHTTP(
expect(data2.random).not.toBe(data.random) appPort,
}) '/some-other-path',
undefined,
it('should render fallback page correctly with x-matched-path and routes-matches', async () => { {
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, { headers: {
headers: { 'x-matched-path': '/',
'x-matched-path': '/fallback/first', },
'x-now-route-matches': '1=first', }
}, )
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#index').text()).toBe('index page')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
}) })
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text()) it('should render dynamic SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(
expect($('#fallback').text()).toBe('fallback page') appPort,
expect($('#slug').text()).toBe('first') '/some-other-path?nxtPslug=first',
expect(data.hello).toBe('world') undefined,
{
const html2 = await renderViaHTTP(appPort, `/fallback/[slug]`, undefined, { headers: {
headers: { 'x-matched-path': '/dynamic/[slug]',
'x-matched-path': '/fallback/[slug]', },
'x-now-route-matches': '1=second', }
}, )
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#dynamic').text()).toBe('dynamic page')
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(
appPort,
'/some-other-path?slug=second',
undefined,
{
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#dynamic').text()).toBe('dynamic page')
expect($2('#slug').text()).toBe('second')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
}) })
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text()) it('should render fallback page correctly with x-matched-path and routes-matches', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, {
expect($2('#fallback').text()).toBe('fallback page')
expect($2('#slug').text()).toBe('second')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/dynamic/first.json?nxtPslug=first`,
undefined,
{
headers: { headers: {
'x-matched-path': '/dynamic/[slug]', 'x-matched-path': '/fallback/first',
'x-now-route-matches': '1=first',
}, },
} })
) const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
const { pageProps: data } = await res.json()
expect($('#fallback').text()).toBe('fallback page')
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(
appPort,
`/fallback/[slug]`,
undefined,
{
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': '1=second',
},
}
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#fallback').text()).toBe('fallback page')
expect($2('#slug').text()).toBe('second')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
expect(data.slug).toBe('first') it('should return data correctly with x-matched-path', async () => {
expect(data.hello).toBe('world') const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/dynamic/first.json?nxtPslug=first`,
undefined,
{
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
)
const { pageProps: data } = await res.json()
expect(data.slug).toBe('first')
expect(data.hello).toBe('world')
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/fallback/[slug].json`,
undefined,
{
headers: {
'x-matched-path': `/_next/data/${buildId}/fallback/[slug].json`,
'x-now-route-matches': '1=second',
},
}
)
const { pageProps: data2 } = await res2.json()
expect(data2.slug).toBe('second')
expect(data2.hello).toBe('world')
})
const res2 = await fetchViaHTTP( it('should render fallback optional catch-all route correctly with x-matched-path and routes-matches', async () => {
appPort, const html = await renderViaHTTP(
`/_next/data/${buildId}/fallback/[slug].json`, appPort,
undefined, '/catch-all/[[...rest]]',
{ undefined,
headers: { {
'x-matched-path': `/_next/data/${buildId}/fallback/[slug].json`, headers: {
'x-now-route-matches': '1=second', 'x-matched-path': '/catch-all/[[...rest]]',
}, 'x-now-route-matches': '',
} },
) }
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#catch-all').text()).toBe('optional catch-all page')
expect(data.params).toEqual({})
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(
appPort,
'/catch-all/[[...rest]]',
undefined,
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '1=hello&catchAll=hello',
},
}
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#catch-all').text()).toBe('optional catch-all page')
expect(data2.params).toEqual({ rest: ['hello'] })
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
const html3 = await renderViaHTTP(
appPort,
'/catch-all/[[..rest]]',
undefined,
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '1=hello/world&catchAll=hello/world',
},
}
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
expect($3('#catch-all').text()).toBe('optional catch-all page')
expect(data3.params).toEqual({ rest: ['hello', 'world'] })
expect(isNaN(data3.random)).toBe(false)
expect(data3.random).not.toBe(data.random)
})
const { pageProps: data2 } = await res2.json() it('should return data correctly with x-matched-path for optional catch-all route', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all.json`,
undefined,
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
)
const { pageProps: data } = await res.json()
expect(data.params).toEqual({})
expect(data.hello).toBe('world')
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined,
{
headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': '1=hello&rest=hello',
},
}
)
const { pageProps: data2 } = await res2.json()
expect(data2.params).toEqual({ rest: ['hello'] })
expect(data2.hello).toBe('world')
const res3 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined,
{
headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': '1=hello/world&rest=hello/world',
},
}
)
const { pageProps: data3 } = await res3.json()
expect(data3.params).toEqual({ rest: ['hello', 'world'] })
expect(data3.hello).toBe('world')
})
expect(data2.slug).toBe('second') it('should not apply trailingSlash redirect', async () => {
expect(data2.hello).toBe('world') for (const path of [
}) '/',
'/dynamic/another/',
'/dynamic/another',
'/fallback/first/',
'/fallback/first',
'/fallback/another/',
'/fallback/another',
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
it('should render fallback optional catch-all route correctly with x-matched-path and routes-matches', async () => { expect(res.status).toBe(200)
const html = await renderViaHTTP(
appPort,
'/catch-all/[[...rest]]',
undefined,
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '',
},
}
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#catch-all').text()).toBe('optional catch-all page')
expect(data.params).toEqual({})
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(
appPort,
'/catch-all/[[...rest]]',
undefined,
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '1=hello&catchAll=hello',
},
} }
) })
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text()) it('should normalize catch-all rewrite query values correctly', async () => {
const html = await renderViaHTTP(
expect($2('#catch-all').text()).toBe('optional catch-all page') appPort,
expect(data2.params).toEqual({ rest: ['hello'] }) '/some-catch-all/hello/world',
expect(isNaN(data2.random)).toBe(false) {
expect(data2.random).not.toBe(data.random) path: 'hello/world',
const html3 = await renderViaHTTP(
appPort,
'/catch-all/[[..rest]]',
undefined,
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '1=hello/world&catchAll=hello/world',
},
}
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
expect($3('#catch-all').text()).toBe('optional catch-all page')
expect(data3.params).toEqual({ rest: ['hello', 'world'] })
expect(isNaN(data3.random)).toBe(false)
expect(data3.random).not.toBe(data.random)
})
it('should return data correctly with x-matched-path for optional catch-all route', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all.json`,
undefined,
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
}, },
} {
) headers: {
'x-matched-path': '/',
},
}
)
const $ = cheerio.load(html)
expect(JSON.parse($('#router').text()).query).toEqual({
path: ['hello', 'world'],
})
})
const { pageProps: data } = await res.json() it('should bubble error correctly for gip page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
expect(data.params).toEqual({}) it('should bubble error correctly for gssp page', async () => {
expect(data.hello).toBe('world') const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
const res2 = await fetchViaHTTP( it('should bubble error correctly for gsp page', async () => {
appPort, const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
`/_next/data/${buildId}/catch-all/[[...rest]].json`, expect(res.status).toBe(500)
undefined, expect(await res.text()).toBe('Internal Server Error')
{ })
headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`, it('should normalize optional values correctly for SSP page', async () => {
'x-now-route-matches': '1=hello&rest=hello', const res = await fetchViaHTTP(
}, appPort,
} '/optional-ssp',
) { rest: '', another: 'value' },
{
headers: {
'x-matched-path': '/optional-ssp/[[...rest]]',
},
}
)
const html = await res.text()
const $ = cheerio.load(html)
const props = JSON.parse($('#props').text())
expect(props.params).toEqual({})
expect(props.query).toEqual({ another: 'value' })
})
const { pageProps: data2 } = await res2.json() it('should normalize optional values correctly for SSG page', async () => {
const res = await fetchViaHTTP(
appPort,
'/optional-ssg',
{ rest: '', another: 'value' },
{
headers: {
'x-matched-path': '/optional-ssg/[[...rest]]',
},
}
)
const html = await res.text()
const $ = cheerio.load(html)
const props = JSON.parse($('#props').text())
expect(props.params).toEqual({})
})
expect(data2.params).toEqual({ rest: ['hello'] }) it('should normalize optional values correctly for API page', async () => {
expect(data2.hello).toBe('world') const res = await fetchViaHTTP(
appPort,
'/api/optional',
{ rest: '', another: 'value' },
{
headers: {
'x-matched-path': '/api/optional/[[...rest]]',
},
}
)
const json = await res.json()
expect(json.query).toEqual({ another: 'value' })
expect(json.url).toBe('/api/optional?another=value')
})
const res3 = await fetchViaHTTP( it('should match the index page correctly', async () => {
appPort, const res = await fetchViaHTTP(appPort, '/', undefined, {
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined,
{
headers: { headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`, 'x-matched-path': '/index',
'x-now-route-matches': '1=hello/world&rest=hello/world',
}, },
}
)
const { pageProps: data3 } = await res3.json()
expect(data3.params).toEqual({ rest: ['hello', 'world'] })
expect(data3.hello).toBe('world')
})
it('should not apply trailingSlash redirect', async () => {
for (const path of [
'/',
'/dynamic/another/',
'/dynamic/another',
'/fallback/first/',
'/fallback/first',
'/fallback/another/',
'/fallback/another',
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual', redirect: 'manual',
}) })
expect(res.status).toBe(200) const html = await res.text()
} const $ = cheerio.load(html)
}) expect($('#index').text()).toBe('index page')
it('should normalize catch-all rewrite query values correctly', async () => {
const html = await renderViaHTTP(
appPort,
'/some-catch-all/hello/world',
{
path: 'hello/world',
},
{
headers: {
'x-matched-path': '/',
},
}
)
const $ = cheerio.load(html)
expect(JSON.parse($('#router').text()).query).toEqual({
path: ['hello', 'world'],
}) })
})
it('should match the root dynamic page correctly', async () => {
it('should bubble error correctly for gip page', async () => { const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
it('should bubble error correctly for gssp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
it('should bubble error correctly for gsp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
it('should normalize optional values correctly for SSP page', async () => {
const res = await fetchViaHTTP(
appPort,
'/optional-ssp',
{ rest: '', another: 'value' },
{
headers: {
'x-matched-path': '/optional-ssp/[[...rest]]',
},
}
)
const html = await res.text()
const $ = cheerio.load(html)
const props = JSON.parse($('#props').text())
expect(props.params).toEqual({})
expect(props.query).toEqual({ another: 'value' })
})
it('should normalize optional values correctly for SSG page', async () => {
const res = await fetchViaHTTP(
appPort,
'/optional-ssg',
{ rest: '', another: 'value' },
{
headers: {
'x-matched-path': '/optional-ssg/[[...rest]]',
},
}
)
const html = await res.text()
const $ = cheerio.load(html)
const props = JSON.parse($('#props').text())
expect(props.params).toEqual({})
})
it('should normalize optional values correctly for API page', async () => {
const res = await fetchViaHTTP(
appPort,
'/api/optional',
{ rest: '', another: 'value' },
{
headers: { headers: {
'x-matched-path': '/api/optional/[[...rest]]', 'x-matched-path': '/[slug]',
}, },
} redirect: 'manual',
) })
const json = await res.json()
expect(json.query).toEqual({ another: 'value' })
expect(json.url).toBe('/api/optional?another=value')
})
it('should match the index page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
'x-matched-path': '/index',
},
redirect: 'manual',
})
const html = await res.text() const html = await res.text()
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#index').text()).toBe('index page') expect($('#slug-page').text()).toBe('[slug] page')
})
it('should match the root dynamic page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
}) })
const html = await res.text() it('should handle 404s properly', async () => {
const $ = cheerio.load(html) for (const pathname of [
expect($('#slug-page').text()).toBe('[slug] page') '/_next/static/chunks/pages/index-abc123.js',
}) '/_next/static/some-file.js',
'/static/some-file.js',
it('should handle 404s properly', async () => { '/non-existent',
for (const pathname of [ '/404',
'/_next/static/chunks/pages/index-abc123.js', ]) {
'/_next/static/some-file.js', const res = await fetchViaHTTP(appPort, pathname, undefined, {
'/static/some-file.js', headers: {
'/non-existent', 'x-matched-path': '/404',
'/404', redirect: 'manual',
]) { },
const res = await fetchViaHTTP(appPort, pathname, undefined, { })
headers: { expect(res.status).toBe(404)
'x-matched-path': '/404', expect(await res.text()).toContain('custom 404')
redirect: 'manual', }
}, })
}) }
expect(res.status).toBe(404) )
expect(await res.text()).toContain('custom 404')
}
})
})
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