Files
4588a73542 Convert tests using createNext -> nextTestSetup (#93767)
## What?

Converts existing `createNext()` usage into `nextTestSetup()`. 

`createNext()` was the setup step we had before `nextTestSetup()` was
added.

This PR focused on the simple conversion cases. There will be a
follow-up to complete the last few.

---------

Co-authored-by: Cursor <[email protected]>
2026-05-12 13:16:31 +02:00

104 lines
2.9 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
import { statSync } from 'fs'
import { join } from 'path'
// TODO: Implement experimental.fallbackNodePolyfills
;(process.env.IS_TURBOPACK_TEST ? describe.skip : describe)(
'Disable fallback polyfills',
() => {
async function getIndexPageSize() {
// Read build manifest to get chunk files for the index page
// this only works reliably for pages router and simple examples.
const buildManifest = await next.readJSON('.next/build-manifest.json')
// Get chunks for the '/' page
const indexPageChunks = buildManifest.pages['/'] || []
// Calculate total size of all chunks for the index page
let totalSize = 0
for (const chunkPath of indexPageChunks) {
const fullChunkPath = join(next.testDir, '.next', chunkPath)
try {
const stats = statSync(fullChunkPath)
totalSize += stats.size
} catch (error) {
console.warn(`Could not read chunk: ${chunkPath}`, error.message)
}
}
// Convert to kB for easier comparison
return totalSize / 1024
}
const { next } = nextTestSetup({
files: {
'pages/index.js': `
import { useEffect } from 'react'
import crypto from 'crypto'
export default function Page() {
useEffect(() => {
crypto;
}, [])
return <p>hello world</p>
}
`,
},
dependencies: {
axios: '0.27.2',
},
})
beforeAll(async () => {
await next.stop()
})
it('Fallback polyfills added by default', async () => {
const indexPageSizeKB = await getIndexPageSize()
console.log(
`Index page size (with polyfills): ${indexPageSizeKB.toFixed(2)} kB`
)
expect(indexPageSizeKB).not.toBeLessThan(400)
})
it('Reduced bundle size when polyfills are disabled', async () => {
await next.patchFile(
'next.config.js',
`module.exports = {
experimental: {
fallbackNodePolyfills: false
}
}`
)
await next.start()
await next.stop()
const indexPageSizeKB = await getIndexPageSize()
console.log(
`Index page size (without polyfills): ${indexPageSizeKB.toFixed(2)} kB`
)
expect(indexPageSizeKB).toBeLessThan(400)
})
it('Pass build without error if non-polyfilled module is unreachable', async () => {
// `axios` uses `Buffer`, but it should be unreachable in the browser.
// https://github.com/axios/axios/blob/649d739288c8e2c55829ac60e2345a0f3439c730/lib/helpers/toFormData.js#L138
await next.patchFile(
'pages/index.js',
`import axios from 'axios'
import { useEffect } from 'react'
export default function Home() {
useEffect(() => {
axios.get('/api')
}, [])
return "hello world"
}`
)
await expect(next.start()).not.toReject()
})
}
)