mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
e860cec656
### What? Migrate remaining direct `next-webdriver` test callers that have a `NextInstance` to `next.browser()`, and expose the shared `Playwright` browser type from `e2e-utils`. ### Why? `NextInstance.browser` should be the supported browser-opening interface for test fixtures, with `next-webdriver` kept as the private implementation detail. ### How? Updated affected development, e2e, and production tests to call `next.browser()` directly, passing `baseUrl` where tests intentionally target a manually spawned or proxied server. Shared helpers now receive browser callbacks from the test context, and browser types import `Playwright` from `e2e-utils` instead of deriving from `next.browser` or importing from private paths. <!-- NEXT_JS_LLM_PR -->
495 lines
13 KiB
TypeScript
495 lines
13 KiB
TypeScript
import { nextTestSetup, type Playwright } from 'e2e-utils'
|
|
import { check } from 'next-test-utils'
|
|
|
|
describe('beforeInteractive in document Head', () => {
|
|
const { next } = nextTestSetup({
|
|
files: {
|
|
'pages/_document.js': `
|
|
import { Html, Head, Main, NextScript } from 'next/document'
|
|
import Script from 'next/script'
|
|
|
|
export default function Document() {
|
|
return (
|
|
<Html>
|
|
<Head>
|
|
<Script
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
|
|
strategy="beforeInteractive"
|
|
></Script>
|
|
</Head>
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
)
|
|
}
|
|
`,
|
|
'pages/index.js': `
|
|
export default function Home() {
|
|
return (
|
|
<>
|
|
<p>Home page</p>
|
|
</>
|
|
)
|
|
}
|
|
`,
|
|
},
|
|
})
|
|
|
|
it('Script is injected server-side', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
const script = await browser.eval(
|
|
`document.querySelector('script[data-nscript="beforeInteractive"]')`
|
|
)
|
|
expect(script).not.toBeNull()
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('beforeInteractive in document body', () => {
|
|
const { next } = nextTestSetup({
|
|
files: {
|
|
'pages/_document.js': `
|
|
import { Html, Head, Main, NextScript } from 'next/document'
|
|
import Script from 'next/script'
|
|
|
|
export default function Document() {
|
|
return (
|
|
<Html>
|
|
<Head />
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
<Script
|
|
src="https://www.google-analytics.com/analytics.js"
|
|
strategy="beforeInteractive"
|
|
/>
|
|
</body>
|
|
</Html>
|
|
)
|
|
}
|
|
`,
|
|
'pages/index.js': `
|
|
export default function Home() {
|
|
return (
|
|
<>
|
|
<p>Home page</p>
|
|
</>
|
|
)
|
|
}
|
|
`,
|
|
},
|
|
})
|
|
|
|
it('Script is injected server-side', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
const script = await browser.eval(
|
|
`document.querySelector('script[data-nscript="beforeInteractive"]')`
|
|
)
|
|
|
|
expect(script).not.toBeNull()
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('empty strategy in document Head', () => {
|
|
const { next } = nextTestSetup({
|
|
files: {
|
|
'pages/_document.js': `
|
|
import { Html, Head, Main, NextScript } from 'next/document'
|
|
import Script from 'next/script'
|
|
|
|
export default function Document() {
|
|
return (
|
|
<Html>
|
|
<Head>
|
|
<Script
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
|
|
></Script>
|
|
</Head>
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
)
|
|
}
|
|
`,
|
|
'pages/index.js': `
|
|
export default function Home() {
|
|
return (
|
|
<>
|
|
<p>Home page</p>
|
|
</>
|
|
)
|
|
}
|
|
`,
|
|
},
|
|
})
|
|
|
|
it('Script is injected server-side', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
const script = await browser.eval(
|
|
`document.querySelector('script[data-nscript="afterInteractive"]')`
|
|
)
|
|
expect(script).not.toBeNull()
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('empty strategy in document body', () => {
|
|
const { next } = nextTestSetup({
|
|
files: {
|
|
'pages/_document.js': `
|
|
import { Html, Head, Main, NextScript } from 'next/document'
|
|
import Script from 'next/script'
|
|
|
|
export default function Document() {
|
|
return (
|
|
<Html>
|
|
<Head/>
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
<Script
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
|
|
/>
|
|
</body>
|
|
</Html>
|
|
)
|
|
}
|
|
`,
|
|
'pages/index.js': `
|
|
export default function Home() {
|
|
return (
|
|
<>
|
|
<p>Home page</p>
|
|
</>
|
|
)
|
|
}
|
|
`,
|
|
},
|
|
})
|
|
|
|
it('Script is injected server-side', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
const script = await browser.eval(
|
|
`document.querySelector('script[data-nscript="afterInteractive"]')`
|
|
)
|
|
expect(script).not.toBeNull()
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
;(process.env.IS_TURBOPACK_TEST ? describe.skip : describe)(
|
|
'experimental.nextScriptWorkers',
|
|
() => {
|
|
describe('experimental.nextScriptWorkers: false with no Partytown dependency', () => {
|
|
const { next } = nextTestSetup({
|
|
files: {
|
|
'pages/index.js': `
|
|
import Script from 'next/script'
|
|
|
|
export default function Page() {
|
|
return (
|
|
<>
|
|
<Script
|
|
src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"
|
|
strategy="worker"
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
`,
|
|
},
|
|
})
|
|
|
|
it('Partytown snippet is not injected to head if not enabled in configuration', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
const snippetScript = await browser.eval(
|
|
`document.querySelector('script[data-partytown]')`
|
|
)
|
|
|
|
expect(snippetScript).toEqual(null)
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('experimental.nextScriptWorkers: true with required Partytown dependency for external script', () => {
|
|
const { next } = nextTestSetup({
|
|
nextConfig: {
|
|
experimental: {
|
|
nextScriptWorkers: true,
|
|
},
|
|
},
|
|
files: {
|
|
'pages/index.js': `
|
|
import Script from 'next/script'
|
|
|
|
export default function Page() {
|
|
return (
|
|
<>
|
|
<Script
|
|
src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"
|
|
strategy="worker"
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
`,
|
|
},
|
|
dependencies: {
|
|
'@builder.io/partytown': '0.4.2',
|
|
},
|
|
})
|
|
|
|
it('Partytown snippets are injected to head if enabled in configuration', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
const snippetScript = await browser.eval(
|
|
`document.querySelector('script[data-partytown]').innerHTML`
|
|
)
|
|
const configScript = await browser.eval(
|
|
`document.querySelector('script[data-partytown-config]').innerHTML`
|
|
)
|
|
|
|
expect(snippetScript).not.toEqual(null)
|
|
|
|
// A default config is included that points to the correct folder that hosts partytown's static files
|
|
expect(configScript).not.toEqual(null)
|
|
expect(configScript.replace(/(?: *[\n\r])+ */g, '')).toEqual(
|
|
'partytown = {lib: "/_next/static/~partytown/"};'
|
|
)
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
|
|
it('Worker scripts are modified by Partytown to execute on a worker thread', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
// Partytown modifies type to "text/partytown-x" after it has been executed in the web worker
|
|
await check(async () => {
|
|
const processedWorkerScripts = await browser.eval(
|
|
`document.querySelectorAll('script[type="text/partytown-x"]').length`
|
|
)
|
|
return processedWorkerScripts > 0
|
|
? 'success'
|
|
: processedWorkerScripts
|
|
}, 'success')
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
function buildInlineScriptPage(script: string) {
|
|
return `
|
|
import Script from 'next/script'
|
|
|
|
export default function Page() {
|
|
return (
|
|
<>
|
|
${script}
|
|
<div id="text" />
|
|
</>
|
|
)
|
|
}
|
|
`
|
|
}
|
|
|
|
describe('experimental.nextScriptWorkers: true with required Partytown dependency for inline script (children)', () => {
|
|
const { next } = nextTestSetup({
|
|
nextConfig: {
|
|
experimental: {
|
|
nextScriptWorkers: true,
|
|
},
|
|
},
|
|
files: {
|
|
'pages/index.js': buildInlineScriptPage(
|
|
`<Script id="inline-script" strategy="worker">{"document.getElementById('text').textContent += 'abc'"}</Script>`
|
|
),
|
|
},
|
|
dependencies: {
|
|
'@builder.io/partytown': '0.4.2',
|
|
},
|
|
})
|
|
|
|
it('Inline worker script through children is modified by Partytown to execute on a worker thread', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
// Partytown modifies type to "text/partytown-x" after it has been executed in the web worker
|
|
await check(async () => {
|
|
const processedWorkerScripts = await browser.eval(
|
|
`document.querySelectorAll('script[type="text/partytown-x"]').length`
|
|
)
|
|
return processedWorkerScripts + ''
|
|
}, '1')
|
|
|
|
const text = await browser.elementById('text').text()
|
|
expect(text).toBe('abc')
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('experimental.nextScriptWorkers: true with required Partytown dependency for inline script (dangerouslySetInnerHTML)', () => {
|
|
const { next } = nextTestSetup({
|
|
nextConfig: {
|
|
experimental: {
|
|
nextScriptWorkers: true,
|
|
},
|
|
},
|
|
files: {
|
|
'pages/index.js': buildInlineScriptPage(
|
|
`<Script id="inline-script" strategy="worker" dangerouslySetInnerHTML={{__html: "document.getElementById('text').textContent += 'abcd'"}}/>`
|
|
),
|
|
},
|
|
dependencies: {
|
|
'@builder.io/partytown': '0.4.2',
|
|
},
|
|
})
|
|
|
|
it('Inline worker script through dangerouslySetInnerHtml is modified by Partytown to execute on a worker thread', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
// Partytown modifies type to "text/partytown-x" after it has been executed in the web worker
|
|
await check(async () => {
|
|
const processedWorkerScripts = await browser.eval(
|
|
`document.querySelectorAll('script[type="text/partytown-x"]').length`
|
|
)
|
|
return processedWorkerScripts + ''
|
|
}, '1')
|
|
|
|
const text = await browser.elementById('text').text()
|
|
expect(text).toBe('abcd')
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('experimental.nextScriptWorkers: true with config override', () => {
|
|
const { next } = nextTestSetup({
|
|
nextConfig: {
|
|
experimental: {
|
|
nextScriptWorkers: true,
|
|
},
|
|
},
|
|
files: {
|
|
'pages/_document.js': `
|
|
import Document, { Html, Head, Main, NextScript } from "next/document";
|
|
|
|
class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
<Head>
|
|
<script
|
|
data-partytown-config
|
|
dangerouslySetInnerHTML={{
|
|
__html: \`
|
|
partytown = {
|
|
lib: "/_next/static/~partytown/",
|
|
debug: true
|
|
};
|
|
\`,
|
|
}}
|
|
/>
|
|
</Head>
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default MyDocument;
|
|
`,
|
|
'pages/index.js': `
|
|
import Script from 'next/script'
|
|
|
|
export default function Page() {
|
|
return (
|
|
<>
|
|
<Script
|
|
src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"
|
|
strategy="worker"
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
`,
|
|
},
|
|
dependencies: {
|
|
'@builder.io/partytown': '0.4.2',
|
|
},
|
|
})
|
|
|
|
it('Partytown config script is overwritten', async () => {
|
|
let browser: Playwright
|
|
|
|
try {
|
|
browser = await next.browser('/')
|
|
|
|
const configScript = await browser.eval(
|
|
`document.querySelector('script[data-partytown-config]').innerHTML`
|
|
)
|
|
|
|
expect(configScript).not.toEqual(null)
|
|
expect(configScript.replace(/(?: *[\n\r])+ */g, '')).toEqual(
|
|
'partytown = {lib: "/_next/static/~partytown/",debug: true};'
|
|
)
|
|
} finally {
|
|
if (browser) await browser.close()
|
|
}
|
|
})
|
|
})
|
|
}
|
|
)
|