Commit 30b9c8f9 authored by Jiachi Liu's avatar Jiachi Liu

test: add feature of overriding files (#74716)

parent 79bdd868
...@@ -8,9 +8,7 @@ import { ...@@ -8,9 +8,7 @@ import {
} from 'next-test-utils' } from 'next-test-utils'
// TODO: When owner stack is enabled by default, remove the condition and only keep one test // TODO: When owner stack is enabled by default, remove the condition and only keep one test
const isOwnerStackEnabled = const isOwnerStackEnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
process.env.TEST_OWNER_STACK !== 'false' ||
process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
;(isOwnerStackEnabled ? describe.skip : describe)( ;(isOwnerStackEnabled ? describe.skip : describe)(
'app-dir - invalid-element-type', 'app-dir - invalid-element-type',
......
...@@ -6,9 +6,7 @@ import { ...@@ -6,9 +6,7 @@ import {
} from 'next-test-utils' } from 'next-test-utils'
// TODO: When owner stack is enabled by default, remove the condition and only keep one test // TODO: When owner stack is enabled by default, remove the condition and only keep one test
const isOwnerStackEnabled = const isOwnerStackEnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
process.env.TEST_OWNER_STACK !== 'false' ||
process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
;(isOwnerStackEnabled ? describe : describe.skip)( ;(isOwnerStackEnabled ? describe : describe.skip)(
'app-dir - owner-stack-react-missing-key-prop', 'app-dir - owner-stack-react-missing-key-prop',
......
...@@ -2,9 +2,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -2,9 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
import { getRedboxSource, openRedbox } from 'next-test-utils' import { getRedboxSource, openRedbox } from 'next-test-utils'
// TODO: When owner stack is enabled by default, remove the condition and only keep one test // TODO: When owner stack is enabled by default, remove the condition and only keep one test
const isOwnerStackEnabled = const isOwnerStackEnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
process.env.TEST_OWNER_STACK !== 'false' ||
process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
async function getStackFramesContent(browser) { async function getStackFramesContent(browser) {
const stackFrameElements = await browser.elementsByCss( const stackFrameElements = await browser.elementsByCss(
...@@ -33,29 +31,26 @@ async function getStackFramesContent(browser) { ...@@ -33,29 +31,26 @@ async function getStackFramesContent(browser) {
return stackFramesContent return stackFramesContent
} }
// Without owner stack, the source is not available
;(isOwnerStackEnabled ? describe.skip : describe)( ;(isOwnerStackEnabled ? describe.skip : describe)(
'app-dir - react-missing-key-prop', 'app-dir - react-missing-key-prop',
() => { () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
}) overrideFiles: {
'next.config.js': `
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
reactOwnerStack: false,
},
}
let nextConfig: string = '' module.exports = nextConfig
beforeAll(async () => { `,
await next.stop() },
await next.patchFile('next.config.js', (content: string) => {
nextConfig = content
return content.replace(
`reactOwnerStack: true`,
`reactOwnerStack: false`
)
})
await next.start()
})
afterAll(async () => {
await next.stop()
// Restore original next.config.js
await next.patchFile('next.config.js', nextConfig)
}) })
it('should catch invalid element from on rsc component', async () => { it('should catch invalid element from on rsc component', async () => {
...@@ -68,30 +63,29 @@ async function getStackFramesContent(browser) { ...@@ -68,30 +63,29 @@ async function getStackFramesContent(browser) {
if (process.env.TURBOPACK) { if (process.env.TURBOPACK) {
expect(stackFramesContent).toMatchInlineSnapshot(`""`) expect(stackFramesContent).toMatchInlineSnapshot(`""`)
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"app/rsc/page.tsx (5:5) @ Page "app/rsc/page.tsx (5:6) @ Page
3 | export default function Page() { 3 | export default function Page() {
4 | return ( 4 | return (
> 5 | <div> > 5 | <div>
| ^ | ^
6 | {list.map((item, index) => ( 6 | {list.map((item, index) => (
7 | <span>{item}</span> 7 | <span>{item}</span>
8 | ))}" 8 | ))}"
`) `)
} else { } else {
expect(stackFramesContent).toMatchInlineSnapshot(`""`) expect(stackFramesContent).toMatchInlineSnapshot(`""`)
// FIXME: the methodName should be `@ Page` instead of `@ div`
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"app/rsc/page.tsx (5:6) @ div "app/rsc/page.tsx (5:6) @ Page
3 | export default function Page() { 3 | export default function Page() {
4 | return ( 4 | return (
> 5 | <div> > 5 | <div>
| ^ | ^
6 | {list.map((item, index) => ( 6 | {list.map((item, index) => (
7 | <span>{item}</span> 7 | <span>{item}</span>
8 | ))}" 8 | ))}"
`) `)
} }
}) })
...@@ -104,30 +98,29 @@ async function getStackFramesContent(browser) { ...@@ -104,30 +98,29 @@ async function getStackFramesContent(browser) {
if (process.env.TURBOPACK) { if (process.env.TURBOPACK) {
expect(stackFramesContent).toMatchInlineSnapshot(`""`) expect(stackFramesContent).toMatchInlineSnapshot(`""`)
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"app/ssr/page.tsx (7:5) @ Page "app/ssr/page.tsx (7:5) @ Page
5 | export default function Page() { 5 | export default function Page() {
6 | return ( 6 | return (
> 7 | <div> > 7 | <div>
| ^ | ^
8 | {list.map((item, index) => ( 8 | {list.map((item, index) => (
9 | <p>{item}</p> 9 | <p>{item}</p>
10 | ))}" 10 | ))}"
`) `)
} else { } else {
expect(stackFramesContent).toMatchInlineSnapshot(`""`) expect(stackFramesContent).toMatchInlineSnapshot(`""`)
// FIXME: the methodName should be `@ Page` instead of `@ div`
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"app/ssr/page.tsx (7:6) @ div "app/ssr/page.tsx (7:6) @ Page
5 | export default function Page() { 5 | export default function Page() {
6 | return ( 6 | return (
> 7 | <div> > 7 | <div>
| ^ | ^
8 | {list.map((item, index) => ( 8 | {list.map((item, index) => (
9 | <p>{item}</p> 9 | <p>{item}</p>
10 | ))}" 10 | ))}"
`) `)
} }
}) })
} }
......
...@@ -23,8 +23,12 @@ export type PackageJson = { ...@@ -23,8 +23,12 @@ export type PackageJson = {
dependencies?: { [key: string]: string } dependencies?: { [key: string]: string }
[key: string]: unknown [key: string]: unknown
} }
type ResolvedFileConfig = FileRef | { [filename: string]: string | FileRef }
type FilesConfig = ResolvedFileConfig | string
export interface NextInstanceOpts { export interface NextInstanceOpts {
files: FileRef | string | { [filename: string]: string | FileRef } files: FilesConfig
overrideFiles?: FilesConfig
dependencies?: { [name: string]: string } dependencies?: { [name: string]: string }
resolutions?: { [name: string]: string } resolutions?: { [name: string]: string }
packageJson?: PackageJson packageJson?: PackageJson
...@@ -55,7 +59,8 @@ type OmitFirstArgument<F> = F extends ( ...@@ -55,7 +59,8 @@ type OmitFirstArgument<F> = F extends (
const nextjsReactPeerVersion = "^19.0.0"; const nextjsReactPeerVersion = "^19.0.0";
export class NextInstance { export class NextInstance {
protected files: FileRef | { [filename: string]: string | FileRef } protected files: ResolvedFileConfig
protected overrideFiles: ResolvedFileConfig
protected nextConfig?: NextConfig protected nextConfig?: NextConfig
protected installCommand?: InstallCommand protected installCommand?: InstallCommand
protected buildCommand?: string protected buildCommand?: string
...@@ -95,10 +100,10 @@ export class NextInstance { ...@@ -95,10 +100,10 @@ export class NextInstance {
} }
} }
protected async writeInitialFiles() { private async writeFiles(filesConfig: FilesConfig, testDir: string) {
// Handle case where files is a directory string // Handle case where files is a directory string
const files = const files =
typeof this.files === 'string' ? new FileRef(this.files) : this.files typeof filesConfig === 'string' ? new FileRef(filesConfig) : filesConfig
if (files instanceof FileRef) { if (files instanceof FileRef) {
// if a FileRef is passed directly to `files` we copy the // if a FileRef is passed directly to `files` we copy the
// entire folder to the test directory // entire folder to the test directory
...@@ -110,7 +115,7 @@ export class NextInstance { ...@@ -110,7 +115,7 @@ export class NextInstance {
) )
} }
await fs.cp(files.fsPath, this.testDir, { await fs.cp(files.fsPath, testDir, {
recursive: true, recursive: true,
filter(source) { filter(source) {
// we don't copy a package.json as it's manually written // we don't copy a package.json as it's manually written
...@@ -124,7 +129,7 @@ export class NextInstance { ...@@ -124,7 +129,7 @@ export class NextInstance {
} else { } else {
for (const filename of Object.keys(files)) { for (const filename of Object.keys(files)) {
const item = files[filename] const item = files[filename]
const outputFilename = path.join(this.testDir, filename) const outputFilename = path.join(testDir, filename)
if (typeof item === 'string') { if (typeof item === 'string') {
await fs.mkdir(path.dirname(outputFilename), { recursive: true }) await fs.mkdir(path.dirname(outputFilename), { recursive: true })
...@@ -136,6 +141,16 @@ export class NextInstance { ...@@ -136,6 +141,16 @@ export class NextInstance {
} }
} }
protected async writeInitialFiles() {
return this.writeFiles(this.files, this.testDir)
}
protected async writeOverrideFiles() {
if (this.overrideFiles) {
return this.writeFiles(this.overrideFiles, this.testDir)
}
}
protected async createTestDir({ protected async createTestDir({
skipInstall = false, skipInstall = false,
parentSpan, parentSpan,
...@@ -240,6 +255,12 @@ export class NextInstance { ...@@ -240,6 +255,12 @@ export class NextInstance {
await this.writeInitialFiles() await this.writeInitialFiles()
}) })
await rootSpan
.traceChild('writeOverrideFiles')
.traceAsyncFn(async () => {
await this.writeOverrideFiles()
})
const testDirFiles = await fs.readdir(this.testDir) const testDirFiles = await fs.readdir(this.testDir)
let nextConfigFile = testDirFiles.find((file) => let nextConfigFile = testDirFiles.find((file) =>
......
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