rsnext/test/production/build-spinners/index.test.ts
Tim Neutkens 5a49b88fb9
Use consistent name for App Router tests (#56352)
Ensures all App Router tests have a unique name and similar reference
(with `-`).

<!-- 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 #

-->

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-10-06 11:06:06 +02:00

193 lines
4.4 KiB
TypeScript

import path from 'path'
import fs from 'fs-extra'
import stripAnsi from 'strip-ansi'
import resolveFrom from 'resolve-from'
import { NextInstance, createNext } from 'e2e-utils'
type File = {
filename: string
content: string
}
const appDirFiles: File[] = [
{
filename: 'app/page.js',
content: `
export default function Page() {
return <p>hello world</p>
}
`,
},
{
filename: 'app/layout.js',
content: `
export default function Layout({ children }) {
return (
<html>
<head />
<body>{children}</body>
</html>
)
}
`,
},
]
const pagesFiles: File[] = [
{
filename: 'pages/another.js',
content: `
export default function Page() {
return (
<p>another page</p>
)
}
`,
},
]
let next: NextInstance
describe('build-spinners', () => {
beforeAll(async () => {
next = await createNext({
skipStart: true,
files: {},
dependencies: {
'node-pty': '0.10.1',
},
})
})
afterAll(() => next.destroy())
beforeEach(async () => {
await fs.remove(path.join(next.testDir, 'pages'))
await fs.remove(path.join(next.testDir, 'app'))
})
it.each([
{ name: 'app dir - basic', files: appDirFiles },
{
name: 'app dir - (compile workers)',
files: [
...appDirFiles,
{
filename: 'next.config.js',
content: `
module.exports = {
experimental: {
webpackBuildWorker: true,
}
}
`,
},
],
},
{
name: 'page dir',
files: [
...pagesFiles,
{
filename: 'next.config.js',
content: `
module.exports = {
experimental: {
webpackBuildWorker: true,
}
}
`,
},
],
},
{ name: 'page dir (compile workers)', files: pagesFiles },
{ name: 'app and pages', files: [...appDirFiles, ...pagesFiles] },
])('should handle build spinners correctly $name', async ({ files }) => {
for (const { filename, content } of files) {
await next.patchFile(filename, content)
}
const appDir = next.testDir
const nextBin = resolveFrom(appDir, 'next/dist/bin/next')
const ptyPath = resolveFrom(appDir, 'node-pty')
const pty = require(ptyPath)
const output = []
const ptyProcess = pty.spawn(process.execPath, [nextBin, 'build'], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: appDir,
env: process.env,
})
ptyProcess.onData(function (data) {
stripAnsi(data)
.split('\n')
.forEach((line) => output.push(line))
process.stdout.write(data)
})
await new Promise<void>((resolve, reject) => {
ptyProcess.onExit(({ exitCode, signal }) => {
if (exitCode) {
return reject(`failed with code ${exitCode}`)
}
resolve()
})
})
let compiledIdx = -1
let optimizedBuildIdx = -1
let collectingPageDataIdx = -1
let generatingStaticIdx = -1
let finalizingOptimization = -1
// order matters so we check output from end to start
for (let i = output.length - 1; i--; i >= 0) {
const line = output[i]
if (compiledIdx === -1 && line.includes('Compiled successfully')) {
compiledIdx = i
}
if (
optimizedBuildIdx === -1 &&
line.includes('Creating an optimized production build')
) {
optimizedBuildIdx = i
}
if (
collectingPageDataIdx === -1 &&
line.includes('Collecting page data')
) {
collectingPageDataIdx = i
}
if (
generatingStaticIdx === -1 &&
line.includes('Generating static pages')
) {
generatingStaticIdx = i
}
if (
finalizingOptimization === -1 &&
line.includes('Finalizing page optimization')
) {
finalizingOptimization = i
}
}
expect(compiledIdx).not.toBe(-1)
expect(optimizedBuildIdx).not.toBe(-1)
expect(collectingPageDataIdx).not.toBe(-1)
expect(generatingStaticIdx).not.toBe(-1)
expect(finalizingOptimization).not.toBe(-1)
expect(optimizedBuildIdx).toBeLessThan(compiledIdx)
expect(compiledIdx).toBeLessThan(collectingPageDataIdx)
expect(collectingPageDataIdx).toBeLessThan(generatingStaticIdx)
expect(generatingStaticIdx).toBeLessThan(finalizingOptimization)
})
})