Unverified Commit ba1dd267 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Improve tests (#84374)

- should encode chunk path correctly
    - Run a base version for all bundlers
    - Use `beforePageLoad`
- nonce test:
    - open page once and wait for scripts to load
    - doesn't make sense to retry the whole thing
parent 40f32904
...@@ -10,7 +10,9 @@ export function ShowScriptOrder() { ...@@ -10,7 +10,9 @@ export function ShowScriptOrder() {
<button <button
id="get-order" id="get-order"
onClick={() => { onClick={() => {
setOrder(window._script_order) // Copy the array, otherwise React won't rerender after the
// `window._script_order.push()` call
setOrder([...(window._script_order || [])])
}} }}
> >
get order get order
......
...@@ -10,7 +10,9 @@ export function ShowScriptOrder() { ...@@ -10,7 +10,9 @@ export function ShowScriptOrder() {
<button <button
id="get-order" id="get-order"
onClick={() => { onClick={() => {
setOrder(window._script_order) // Copy the array, otherwise React won't rerender after the
// `window._script_order.push()` call
setOrder([...(window._script_order || [])])
}} }}
> >
get order get order
......
...@@ -13,26 +13,25 @@ import { ...@@ -13,26 +13,25 @@ import {
const isPPREnabledByDefault = process.env.__NEXT_EXPERIMENTAL_PPR === 'true' const isPPREnabledByDefault = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
describe('app dir - basic', () => { describe('app dir - basic', () => {
const { next, isNextDev, isNextStart, isNextDeploy, isTurbopack } = const { next, isNextDev, isNextStart, isNextDeploy } = nextTestSetup({
nextTestSetup({ files: __dirname,
files: __dirname, buildCommand: process.env.NEXT_EXPERIMENTAL_COMPILE
buildCommand: process.env.NEXT_EXPERIMENTAL_COMPILE ? 'pnpm compile-mode'
? 'pnpm compile-mode' : undefined,
: undefined, packageJson: {
packageJson: { scripts: {
scripts: { 'compile-mode': process.env.NEXT_EXPERIMENTAL_COMPILE
'compile-mode': process.env.NEXT_EXPERIMENTAL_COMPILE ? `next build --experimental-build-mode=compile && next build --experimental-build-mode=generate-env`
? `next build --experimental-build-mode=compile && next build --experimental-build-mode=generate-env` : undefined,
: undefined,
},
},
dependencies: {
nanoid: '4.0.1',
},
env: {
NEXT_PUBLIC_TEST_ID: Date.now() + '',
}, },
}) },
dependencies: {
nanoid: '4.0.1',
},
env: {
NEXT_PUBLIC_TEST_ID: Date.now() + '',
},
})
if (isNextStart) { if (isNextStart) {
it('should have correct cache-control for SSR routes', async () => { it('should have correct cache-control for SSR routes', async () => {
...@@ -169,32 +168,32 @@ describe('app dir - basic', () => { ...@@ -169,32 +168,32 @@ describe('app dir - basic', () => {
}) })
} }
// Turbopack has different chunking in dev/production which results in the entrypoint name not being included in the outputs. it('should encode chunk path correctly', async () => {
if (!process.env.IS_TURBOPACK_TEST) { const requests = []
it('should encode chunk path correctly', async () => { const browser = await next.browser('/dynamic-client/first/second', {
await next.fetch('/dynamic-client/first/second') beforePageLoad(page) {
const browser = await next.browser('/') page.on('request', (req) => {
const requests = [] requests.push(req.url())
browser.on('request', (req) => { })
requests.push(req.url()) },
}) })
await browser.eval( await browser.waitForElementByCss('#id-page-params')
'window.location.href = "/dynamic-client/first/second"'
)
await browser.waitForElementByCss('#id-page-params') expect(requests).not.toEqual(
expect.arrayContaining([expect.stringContaining('[category]')])
)
expect( // Turbopack doesn't recreate the original folder structure for the output chunks
requests.some( if (!process.env.IS_TURBOPACK_TEST) {
(req) => expect(requests).toEqual(
req.includes( // e.g. _next/static/chunks/app/dynamic-client/%5Bcategory%5D/%5Bid%5D/page-6e188d657a208f8e.js?dpl=...
encodeURI(isTurbopack ? '[category]_[id]' : '/[category]/[id]') expect.arrayContaining([
) && req.includes('.js') expect.stringMatching(/.*%5Bcategory%5D\/%5Bid%5D.*\.js/),
) ])
).toBe(true) )
}) }
} })
it.each([ it.each([
{ pathname: '/redirect-1' }, { pathname: '/redirect-1' },
...@@ -393,7 +392,7 @@ describe('app dir - basic', () => { ...@@ -393,7 +392,7 @@ describe('app dir - basic', () => {
it('should serve polyfills for browsers that do not support modules', async () => { it('should serve polyfills for browsers that do not support modules', async () => {
const html = await next.render('/dashboard/index') const html = await next.render('/dashboard/index')
expect(html).toMatch( expect(html).toMatch(
isTurbopack process.env.IS_TURBOPACK_TEST
? /<script src="\/_next\/static\/chunks\/([\w-]*polyfill-nomodule|[0-9a-f]+)\.js" noModule="">/ ? /<script src="\/_next\/static\/chunks\/([\w-]*polyfill-nomodule|[0-9a-f]+)\.js" noModule="">/
: /<script src="\/_next\/static\/chunks\/polyfills(-\w+)?\.js" noModule="">/ : /<script src="\/_next\/static\/chunks\/polyfills(-\w+)?\.js" noModule="">/
) )
...@@ -1721,12 +1720,12 @@ describe('app dir - basic', () => { ...@@ -1721,12 +1720,12 @@ describe('app dir - basic', () => {
}) })
if (!isNextDev) { if (!isNextDev) {
// Fails in dev due to CSP header
const browser = await next.browser('/script-nonce') const browser = await next.browser('/script-nonce')
await retry(async () => { await retry(async () => {
await browser.elementByCss('#get-order').click() await browser.elementByCss('#get-order').click()
const order = JSON.parse(await browser.elementByCss('#order').text()) const order = JSON.parse(await browser.elementByCss('#order').text())
expect(order?.length).toBe(2) expect(order).toEqual([2, 1])
}) })
} }
}) })
...@@ -1746,15 +1745,12 @@ describe('app dir - basic', () => { ...@@ -1746,15 +1745,12 @@ describe('app dir - basic', () => {
expect(element.attribs.nonce).toBeTruthy() expect(element.attribs.nonce).toBeTruthy()
}) })
if (!isNextDev) { const browser = await next.browser('/script-manual-nonce')
const browser = await next.browser('/script-manual-nonce') await retry(async () => {
await browser.elementByCss('#get-order').click()
await retry(async () => { const order = JSON.parse(await browser.elementByCss('#order').text())
await browser.elementByCss('#get-order').click() expect(order).toEqual([2, 1])
const order = JSON.parse(await browser.elementByCss('#order').text()) })
expect(order?.length).toBe(2)
})
}
}) })
it('should pass manual `nonce` pages', async () => { it('should pass manual `nonce` pages', async () => {
...@@ -1772,14 +1768,12 @@ describe('app dir - basic', () => { ...@@ -1772,14 +1768,12 @@ describe('app dir - basic', () => {
expect(element.attribs.nonce).toBeTruthy() expect(element.attribs.nonce).toBeTruthy()
}) })
if (!isNextDev) { const browser = await next.browser('/pages-script-manual-nonce')
await retry(async () => { await retry(async () => {
const browser = await next.browser('/pages-script-manual-nonce') await browser.elementByCss('#get-order').click()
await browser.elementByCss('#get-order').click() const order = JSON.parse(await browser.elementByCss('#order').text())
const order = JSON.parse(await browser.elementByCss('#order').text()) expect(order).toEqual([2, 1])
expect(order?.length).toBe(2) })
})
}
}) })
it('should pass nonce when using next/font', async () => { it('should pass nonce when using next/font', async () => {
......
...@@ -111,6 +111,13 @@ export class NextDeployInstance extends NextInstance { ...@@ -111,6 +111,13 @@ export class NextDeployInstance extends NextInstance {
) )
} }
if (process.env.IS_TURBOPACK_TEST) {
additionalEnv.push(`IS_TURBOPACK_TEST=1`)
}
if (process.env.IS_WEBPACK_TEST) {
additionalEnv.push(`IS_WEBPACK_TEST=1`)
}
const deployRes = await execa( const deployRes = await execa(
'vercel', 'vercel',
[ [
......
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