Skip to content

Commit

Permalink
patchFile awaits compilation (vercel#72267)
Browse files Browse the repository at this point in the history
We will wait the server to respond with a compile success message after
a file is patched before proceeding to the next steps in the test to
reduce flakiness.

By doing so, we uncovered a few tests that were passing accidentally due
to flakiness of `patchFile`, and fixed them in the PR.
  • Loading branch information
gaojude authored and stipsan committed Nov 6, 2024
1 parent 02e18f3 commit 073e553
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
8 changes: 4 additions & 4 deletions test/development/middleware-errors/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('middleware - development errors', () => {
const browser = await next.browser('/')
await assertHasRedbox(browser)
await next.patchFile('middleware.js', `export default function () {}`)
await assertHasRedbox(browser)
await assertNoRedbox(browser)
})
})

Expand Down Expand Up @@ -160,7 +160,7 @@ describe('middleware - development errors', () => {
await assertHasRedbox(browser)
expect(await getRedboxSource(browser)).toContain(`eval('test')`)
await next.patchFile('middleware.js', `export default function () {}`)
await assertHasRedbox(browser)
await assertNoRedbox(browser)
})
})

Expand Down Expand Up @@ -207,7 +207,7 @@ describe('middleware - development errors', () => {
expect(source).toContain('middleware.js')
expect(source).not.toContain('//middleware.js')
await next.patchFile('middleware.js', `export default function () {}`)
await assertHasRedbox(browser)
await assertNoRedbox(browser)
})
})

Expand Down Expand Up @@ -311,7 +311,7 @@ describe('middleware - development errors', () => {
await browser.elementByCss('#nextjs__container_errors_desc').text()
).toEqual('Failed to compile')
await next.patchFile('middleware.js', `export default function () {}`)
await assertHasRedbox(browser)
await assertNoRedbox(browser)
expect(await browser.elementByCss('#page-title')).toBeTruthy()
})
})
Expand Down
7 changes: 1 addition & 6 deletions test/e2e/prerender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,13 +1133,8 @@ describe('Prerender', () => {

await next.patchFile(
'pages/index.js',
(content) =>
content
.replace('// throw new', 'throw new')
.replace('{/* <div', '<div')
.replace('</div> */}', '</div>'),
(content) => content.replace('// throw new', 'throw new'),
async () => {
await browser.waitForElementByCss('#after-change')
// we need to reload the page to trigger getStaticProps
await browser.refresh()

Expand Down
1 change: 0 additions & 1 deletion test/e2e/prerender/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export async function getStaticProps() {
const Page = ({ world, time }) => {
return (
<>
{/* <div id='after-change'>idk</div> */}
<p>hello {world}</p>
<span>time: {time}</span>
<Link href="/non-json/[p]" as="/non-json/1" id="non-json">
Expand Down
1 change: 1 addition & 0 deletions test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class NextInstance {
public forcedPort?: string
public dirSuffix: string = ''
public serverReadyPattern?: RegExp = / ✓ Ready in /
public serverCompiledPattern?: RegExp = / ✓ Compiled /

constructor(opts: NextInstanceOpts) {
this.env = {}
Expand Down
12 changes: 12 additions & 0 deletions test/lib/next-modes/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ export class NextDevInstance extends NextInstance {
throw new Error('Server has not finished restarting.')
}
})
} else {
try {
await retry(async () => {
const cliOutput = this.cliOutput.slice(cliOutputLength)

if (!this.serverCompiledPattern.test(cliOutput)) {
throw new Error('Server has not finished restarting.')
}
}, 5000)
} catch (e) {
/** Fail silently because not all change will be reflected in the server output */
}
}
}
}
Expand Down

0 comments on commit 073e553

Please sign in to comment.