Unverified Commit ebc1b0e5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix missing trace items and migrate required files test (#29649)

parent dffcfe82
......@@ -307,12 +307,6 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
continue
}
const filepath = nodePath.join(root, file)
// don't include transpiled files as they are included
// in the webpack output (e.g. chunks or the entry itself)
if (depModMap.get(filepath)?.originalSource?.()) {
continue
}
tracedDeps.push(filepath)
}
......
......@@ -118,6 +118,8 @@ describe('Production Usage', () => {
/node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/,
/next\/link\.js/,
/next\/dist\/client\/link\.js/,
],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/],
},
......@@ -129,6 +131,8 @@ describe('Production Usage', () => {
/node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/,
/next\/router\.js/,
/next\/dist\/client\/router\.js/,
],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/],
},
......
module.exports = {
// ensure incorrect target is overridden by env
target: 'serverless',
experimental: {
outputFileTracing: true,
},
eslint: {
ignoreDuringBuilds: true,
},
rewrites() {
return [
{
source: '/some-catch-all/:path*',
destination: '/',
},
]
},
}
......@@ -16,7 +16,7 @@ export class NextInstance {
protected nextConfig?: NextConfig
protected dependencies?: { [name: string]: string }
protected events: { [eventName: string]: Set<any> }
protected testDir: string
public testDir: string
protected isStopping: boolean
protected isDestroyed: boolean
protected childProcess: ChildProcess
......@@ -156,7 +156,10 @@ export class NextInstance {
this.isDestroyed = true
this.emit('destroy', [])
await this.stop()
await fs.remove(this.testDir)
if (!process.env.NEXT_TEST_SKIP_CLEANUP) {
await fs.remove(this.testDir)
}
}
public get url() {
......
/* eslint-env jest */
import os from 'os'
import glob from 'glob'
import fs from 'fs-extra'
import execa from 'execa'
import _fs from 'fs-extra'
import cheerio from 'cheerio'
import { join, dirname, relative } from 'path'
import { version } from 'next/package'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { join, dirname } from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import {
fetchViaHTTP,
findPort,
......@@ -16,131 +12,95 @@ import {
renderViaHTTP,
} from 'next-test-utils'
const appDir = join(__dirname, '../app')
const workDir = join(os.tmpdir(), `required-server-files-${Date.now()}`)
let server
let nextApp
let appPort
let buildId
let requiredFilesManifest
let errors = []
describe('should set-up next', () => {
let next: NextInstance
let server
let appPort
let errors = []
let requiredFilesManifest
describe('Required Server Files', () => {
beforeAll(async () => {
const nextServerTrace = await fs.readJSON(
require.resolve('next/dist/server/next-server') + '.nft.json'
)
const packageDir = dirname(require.resolve('next/package.json'))
await execa('yarn', ['pack'], {
cwd: packageDir,
})
const packagePath = join(packageDir, `next-v${version}.tgz`)
await fs.ensureDir(workDir)
await fs.writeFile(
join(workDir, 'package.json'),
JSON.stringify({
dependencies: {
next: packagePath,
react: 'latest',
'react-dom': 'latest',
},
})
)
await fs.copy(appDir, workDir)
await execa('yarn', ['install'], {
cwd: workDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: {
...process.env,
YARN_CACHE_FOLDER: join(workDir, '.yarn-cache'),
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'required-server-files/pages')),
lib: new FileRef(join(__dirname, 'required-server-files/lib')),
'data.txt': new FileRef(
join(__dirname, 'required-server-files/data.txt')
),
},
})
await execa('yarn', ['next', 'build'], {
cwd: workDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: {
...process.env,
NODE_ENV: 'production',
NOW_BUILDER: '1',
nextConfig: {
experimental: {
outputFileTracing: true,
},
eslint: {
ignoreDuringBuilds: true,
},
async rewrites() {
return [
{
source: '/some-catch-all/:path*',
destination: '/',
},
]
},
},
})
await next.stop()
const keptFiles = new Set<string>()
const nextServerTrace = require('next/dist/server/next-server.js.nft.json')
buildId = await fs.readFile(join(workDir, '.next/BUILD_ID'), 'utf8')
requiredFilesManifest = await fs.readJSON(
join(workDir, '.next/required-server-files.json')
requiredFilesManifest = JSON.parse(
await next.readFile('.next/required-server-files.json')
)
requiredFilesManifest.files.forEach((file) => keptFiles.add(file))
// react and react-dom need to be traced specific to version
// so isn't pre-traced
await fs.ensureDir(`${workDir}-react`)
await fs.writeFile(
join(`${workDir}-react/package.json`),
JSON.stringify({
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
})
)
await execa('yarn', ['install'], {
cwd: `${workDir}-react`,
stdio: ['ignore', 'inherit', 'inherit'],
env: {
...process.env,
YARN_CACHE_FOLDER: join(workDir, '.yarn'),
},
const pageTraceFiles = glob.sync('**/*.nft.json', {
cwd: join(next.testDir, '.next/server/pages'),
})
await fs.remove(packagePath)
for (const traceFile of pageTraceFiles) {
const pageDir = dirname(join('.next/server/pages', traceFile))
const trace = await _fs.readJSON(
join(next.testDir, '.next/server/pages', traceFile)
)
keptFiles.add(
join('.next/server/pages', traceFile.replace('.nft.json', ''))
)
const files = await recursiveReadDir(workDir, /.*/)
for (const file of trace.files) {
keptFiles.add(join(pageDir, file))
}
}
const pageTraceFiles = await glob.sync('**/*.nft.json', {
cwd: join(workDir, '.next/server/pages'),
const allFiles = glob.sync('**/*', {
cwd: next.testDir,
dot: true,
})
const combinedTraces = new Set()
for (const file of pageTraceFiles) {
const filePath = join(workDir, '.next/server/pages', file)
const trace = await fs.readJSON(filePath)
trace.files.forEach((f) =>
combinedTraces.add(relative(workDir, join(dirname(filePath), f)))
)
}
for (const file of files) {
const cleanFile = join('./', file)
for (const file of allFiles) {
const filePath = join(next.testDir, file)
if (
!nextServerTrace.files.includes(cleanFile) &&
file !== '/node_modules/next/dist/server/next-server.js' &&
!combinedTraces.has(cleanFile) &&
!requiredFilesManifest.files.includes(cleanFile) &&
!cleanFile.startsWith('.next/server') &&
cleanFile !== '.next/required-server-files.json'
!keptFiles.has(file) &&
!(await _fs.stat(filePath).catch(() => null))?.isDirectory() &&
!nextServerTrace.files.includes(file) &&
!file.match(/node_modules\/(react|react-dom)\//) &&
file !== 'node_modules/next/dist/server/next-server.js'
) {
await fs.remove(join(workDir, file))
await _fs.remove(filePath)
}
}
appPort = await findPort()
for (const file of await fs.readdir(`${workDir}-react/node_modules`)) {
await fs.copy(
join(`${workDir}-react/node_modules`, file),
join(workDir, 'node_modules', file)
)
}
await fs.remove(`${workDir}-react`)
async function startServer() {
const testServer = join(next.testDir, 'server.js')
await _fs.writeFile(
testServer,
`
const http = require('http')
const NextServer = require('next/dist/server/next-server').default
const appPort = ${appPort}
const appPort = process.env.PORT
nextApp = new NextServer({
conf: global.nextConfig,
dir: process.env.APP_DIR,
const nextApp = new NextServer({
conf: ${JSON.stringify(requiredFilesManifest.config)},
dir: "${next.testDir}",
quiet: false,
minimalMode: true,
})
......@@ -154,37 +114,23 @@ describe('Required Server Files', () => {
res.end('error')
}
})
await new Promise((res, rej) => {
server.listen(appPort, (err) => (err ? rej(err) : res()))
server.listen(appPort, (err) => {
if (err) throw err
console.log(\`Listening at ::${appPort}\`)
})
console.log(`Listening at ::${appPort}`)
}
const serverPath = join(workDir, 'server.js')
await fs.writeFile(
serverPath,
'global.nextConfig = ' +
JSON.stringify(requiredFilesManifest.config) +
';\n' +
startServer.toString() +
';\n' +
`startServer().catch(console.error)`
`
)
appPort = await findPort()
server = await initNextServerScript(
serverPath,
testServer,
/Listening at/,
{
...process.env,
NODE_ENV: 'production',
PORT: appPort,
APP_DIR: workDir,
},
undefined,
{
cwd: workDir,
cwd: next.testDir,
onStderr(msg) {
if (msg.includes('top-level')) {
errors.push(msg)
......@@ -194,8 +140,8 @@ describe('Required Server Files', () => {
)
})
afterAll(async () => {
if (server) killApp(server)
await fs.remove(workDir)
await next.destroy()
if (server) await killApp(server)
})
it('should output required-server-files manifest correctly', async () => {
......@@ -207,15 +153,10 @@ describe('Required Server Files', () => {
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) {
expect(await fs.exists(join(workDir, file))).toBe(true)
}
expect(await fs.exists(join(workDir, '.next/server'))).toBe(true)
})
it('should set correct SWR headers with notFound gsp', async () => {
await fs.writeFile(join(workDir, 'data.txt'), 'show')
await next.patchFile('data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ',
......@@ -225,7 +166,7 @@ describe('Required Server Files', () => {
's-maxage=1, stale-while-revalidate'
)
await fs.writeFile(join(workDir, 'data.txt'), 'hide')
await next.patchFile('data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ',
......@@ -237,7 +178,7 @@ describe('Required Server Files', () => {
})
it('should set correct SWR headers with notFound gssp', async () => {
await fs.writeFile(join(workDir, 'data.txt'), 'show')
await next.patchFile('data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ',
......@@ -247,7 +188,7 @@ describe('Required Server Files', () => {
's-maxage=1, stale-while-revalidate'
)
await fs.writeFile(join(workDir, 'data.txt'), 'hide')
await next.patchFile('data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ',
......@@ -321,7 +262,10 @@ describe('Required Server Files', () => {
expect(isNaN(data3.random)).toBe(false)
const { pageProps: data4 } = JSON.parse(
await renderViaHTTP(appPort, `/_next/data/${buildId}/fallback/third.json`)
await renderViaHTTP(
appPort,
`/_next/data/${next.buildId}/fallback/third.json`
)
)
expect(data4.hello).toBe('world')
expect(data4.slug).toBe('third')
......@@ -425,7 +369,7 @@ describe('Required Server Files', () => {
it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/dynamic/first.json`,
`/_next/data/${next.buildId}/dynamic/first.json`,
undefined,
{
headers: {
......@@ -441,11 +385,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/fallback/[slug].json`,
`/_next/data/${next.buildId}/fallback/[slug].json`,
undefined,
{
headers: {
'x-matched-path': `/_next/data/${buildId}/fallback/[slug].json`,
'x-matched-path': `/_next/data/${next.buildId}/fallback/[slug].json`,
'x-now-route-matches': '1=second',
},
}
......@@ -518,7 +462,7 @@ describe('Required Server Files', () => {
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`,
`/_next/data/${next.buildId}/catch-all.json`,
undefined,
{
headers: {
......@@ -534,11 +478,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined,
{
headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`,
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': '1=hello&rest=hello',
},
}
......@@ -551,11 +495,11 @@ describe('Required Server Files', () => {
const res3 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined,
{
headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`,
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': '1=hello/world&rest=hello/world',
},
}
......
import { useRouter } from 'next/router'
import Link from 'next/link'
import getConfig from '../lib/config'
const localConfig = getConfig()
......@@ -23,6 +24,9 @@ export default function Page(props) {
<p id="index">index page</p>
<p id="router">{JSON.stringify(router)}</p>
<p id="props">{JSON.stringify(props)}</p>
<Link href="gsp">
<a>to /gsp</a>
</Link>
</>
)
}
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