Unverified Commit b925a1bf authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub
parent 01e7c373
...@@ -235,12 +235,13 @@ describe('app-dir - logging', () => { ...@@ -235,12 +235,13 @@ describe('app-dir - logging', () => {
await next.patchFile( await next.patchFile(
'app/default-cache/page.js', 'app/default-cache/page.js',
(content) => content.replace('Default Cache', 'Hello!'), (content) => content.replace('Default Cache', 'Hello!'),
async () => { async () =>
retry(async () => {
headline = await browser.waitForElementByCss('h1').text() headline = await browser.waitForElementByCss('h1').text()
expect(headline).toBe('Hello!') expect(headline).toBe('Hello!')
const logs = stripAnsi(next.cliOutput.slice(outputIndex)) const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).not.toInclude(` │ GET `) expect(logs).not.toInclude(` │ GET `)
} })
) )
}) })
} }
......
...@@ -1005,9 +1005,8 @@ describe('Prerender', () => { ...@@ -1005,9 +1005,8 @@ describe('Prerender', () => {
if ((global as any).isNextDev) { if ((global as any).isNextDev) {
it('should not show warning from url prop being returned', async () => { it('should not show warning from url prop being returned', async () => {
const urlPropPage = 'pages/url-prop.js'
await next.patchFile( await next.patchFile(
urlPropPage, 'pages/url-prop.js',
` `
export async function getStaticProps() { export async function getStaticProps() {
return { return {
...@@ -1018,16 +1017,17 @@ describe('Prerender', () => { ...@@ -1018,16 +1017,17 @@ describe('Prerender', () => {
} }
export default ({ url }) => <p>url: {url}</p> export default ({ url }) => <p>url: {url}</p>
` `,
) async () =>
retry(async () => {
const html = await renderViaHTTP(next.url, '/url-prop') const html = await renderViaHTTP(next.url, '/url-prop')
await next.deleteFile(urlPropPage)
expect(next.cliOutput).not.toMatch( expect(next.cliOutput).not.toMatch(
/The prop `url` is a reserved prop in Next.js for legacy reasons and will be overridden on page \/url-prop/ /The prop `url` is a reserved prop in Next.js for legacy reasons and will be overridden on page \/url-prop/
) )
expect(html).toMatch(/url:.*?something/) expect(html).toMatch(/url:.*?something/)
}) })
)
})
it('should always show fallback for page not in getStaticPaths', async () => { it('should always show fallback for page not in getStaticPaths', async () => {
const html = await renderViaHTTP(next.url, '/blog/post-321') const html = await renderViaHTTP(next.url, '/blog/post-321')
...@@ -1080,33 +1080,30 @@ describe('Prerender', () => { ...@@ -1080,33 +1080,30 @@ describe('Prerender', () => {
}) })
it('should log error in console and browser in development mode', async () => { it('should log error in console and browser in development mode', async () => {
const indexPage = 'pages/index.js'
const origContent = await next.readFile(indexPage)
const browser = await webdriver(next.url, '/') const browser = await webdriver(next.url, '/')
expect(await browser.elementByCss('p').text()).toMatch(/hello.*?world/) expect(await browser.elementByCss('p').text()).toMatch(/hello.*?world/)
await next.patchFile( await next.patchFile(
indexPage, 'pages/index.js',
origContent (content) =>
content
.replace('// throw new', 'throw new') .replace('// throw new', 'throw new')
.replace('{/* <div', '<div') .replace('{/* <div', '<div')
.replace('</div> */}', '</div>') .replace('</div> */}', '</div>'),
) async () => {
try {
await browser.waitForElementByCss('#after-change') await browser.waitForElementByCss('#after-change')
// we need to reload the page to trigger getStaticProps // we need to reload the page to trigger getStaticProps
await browser.refresh() await browser.refresh()
return retry(async () => {
await assertHasRedbox(browser) await assertHasRedbox(browser)
const errOverlayContent = await getRedboxHeader(browser) const errOverlayContent = await getRedboxHeader(browser)
const errorMsg = /oops from getStaticProps/ const errorMsg = /oops from getStaticProps/
expect(next.cliOutput).toMatch(errorMsg) expect(next.cliOutput).toMatch(errorMsg)
expect(errOverlayContent).toMatch(errorMsg) expect(errOverlayContent).toMatch(errorMsg)
} finally { })
await next.patchFile(indexPage, origContent)
} }
)
}) })
it('should always call getStaticProps without caching in dev', async () => { it('should always call getStaticProps without caching in dev', async () => {
...@@ -1133,25 +1130,20 @@ describe('Prerender', () => { ...@@ -1133,25 +1130,20 @@ describe('Prerender', () => {
}) })
it('should error on bad object from getStaticProps', async () => { it('should error on bad object from getStaticProps', async () => {
const indexPage = 'pages/index.js'
const origContent = await next.readFile(indexPage)
await next.patchFile( await next.patchFile(
indexPage, 'pages/index.js',
origContent.replace(/\/\/ bad-prop/, 'another: true,') (content) => content.replace(/\/\/ bad-prop/, 'another: true,'),
) async () =>
await waitFor(1000) retry(async () => {
try {
const html = await renderViaHTTP(next.url, '/') const html = await renderViaHTTP(next.url, '/')
expect(html).toMatch(/Additional keys were returned/) expect(html).toMatch(/Additional keys were returned/)
} finally { })
await next.patchFile(indexPage, origContent) )
}
}) })
it('should error on dynamic page without getStaticPaths', async () => { it('should error on dynamic page without getStaticPaths', async () => {
const curPage = 'pages/temp/[slug].js'
await next.patchFile( await next.patchFile(
curPage, 'pages/temp/[slug].js',
` `
export async function getStaticProps() { export async function getStaticProps() {
return { return {
...@@ -1161,23 +1153,20 @@ describe('Prerender', () => { ...@@ -1161,23 +1153,20 @@ describe('Prerender', () => {
} }
} }
export default () => 'oops' export default () => 'oops'
` `,
) async () =>
await waitFor(1000) retry(async () => {
try {
const html = await renderViaHTTP(next.url, '/temp/hello') const html = await renderViaHTTP(next.url, '/temp/hello')
expect(html).toMatch( expect(html).toMatch(
/getStaticPaths is required for dynamic SSG pages and is missing for/ /getStaticPaths is required for dynamic SSG pages and is missing for/
) )
} finally { })
await next.deleteFile(curPage) )
}
}) })
it('should error on dynamic page without getStaticPaths returning fallback property', async () => { it('should error on dynamic page without getStaticPaths returning fallback property', async () => {
const curPage = 'pages/temp2/[slug].js'
await next.patchFile( await next.patchFile(
curPage, 'pages/temp2/[slug].js',
` `
export async function getStaticPaths() { export async function getStaticPaths() {
return { return {
...@@ -1192,15 +1181,13 @@ describe('Prerender', () => { ...@@ -1192,15 +1181,13 @@ describe('Prerender', () => {
} }
} }
export default () => 'oops' export default () => 'oops'
` `,
) async () =>
await waitFor(1000) retry(async () => {
try {
const html = await renderViaHTTP(next.url, '/temp2/hello') const html = await renderViaHTTP(next.url, '/temp2/hello')
expect(html).toMatch(/`fallback` key must be returned from/) expect(html).toMatch(/`fallback` key must be returned from/)
} finally { })
await next.deleteFile(curPage) )
}
}) })
it('should not re-call getStaticProps when updating query', async () => { it('should not re-call getStaticProps when updating query', async () => {
...@@ -2196,7 +2183,7 @@ describe('Prerender', () => { ...@@ -2196,7 +2183,7 @@ describe('Prerender', () => {
if (!isDev && !isDeploy) { if (!isDev && !isDeploy) {
it('should automatically reset cache TTL when an error occurs and build cache was available', async () => { it('should automatically reset cache TTL when an error occurs and build cache was available', async () => {
await next.patchFile('error.txt', 'yes') await next.patchFile('error.txt', 'yes', async () => {
await waitFor(2000) await waitFor(2000)
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
...@@ -2206,17 +2193,14 @@ describe('Prerender', () => { ...@@ -2206,17 +2193,14 @@ describe('Prerender', () => {
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
} }
await next.deleteFile('error.txt')
await check( return retry(async () => {
() => expect(next.cliOutput).toMatch(
next.cliOutput.match(
/throwing error for \/blocking-fallback\/test-errors-1/ /throwing error for \/blocking-fallback\/test-errors-1/
).length === 1
? 'success'
: next.cliOutput,
'success'
) )
}) })
})
})
it('should automatically reset cache TTL when an error occurs and runtime cache was available', async () => { it('should automatically reset cache TTL when an error occurs and runtime cache was available', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
...@@ -2226,8 +2210,8 @@ describe('Prerender', () => { ...@@ -2226,8 +2210,8 @@ describe('Prerender', () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
await waitFor(2000) await waitFor(2000)
await next.patchFile('error.txt', 'yes')
await next.patchFile('error.txt', 'yes', async () => {
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
next.url, next.url,
...@@ -2235,18 +2219,14 @@ describe('Prerender', () => { ...@@ -2235,18 +2219,14 @@ describe('Prerender', () => {
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
} }
await next.deleteFile('error.txt')
await check( return retry(async () => {
() => expect(next.cliOutput).toMatch(
next.cliOutput.match(
/throwing error for \/blocking-fallback\/test-errors-2/ /throwing error for \/blocking-fallback\/test-errors-2/
).length === 1
? 'success'
: next.cliOutput,
'success'
) )
}) })
})
})
it('should not on-demand revalidate for fallback: blocking with onlyGenerated if not generated', async () => { it('should not on-demand revalidate for fallback: blocking with onlyGenerated if not generated', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
...@@ -2291,6 +2271,7 @@ describe('Prerender', () => { ...@@ -2291,6 +2271,7 @@ describe('Prerender', () => {
expect($('p').text()).toMatch(/Post:.*?test-if-generated-2/) expect($('p').text()).toMatch(/Post:.*?test-if-generated-2/)
expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/) expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/)
await retry(async () => {
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
next.url, next.url,
'/blocking-fallback/test-if-generated-2' '/blocking-fallback/test-if-generated-2'
...@@ -2300,6 +2281,7 @@ describe('Prerender', () => { ...@@ -2300,6 +2281,7 @@ describe('Prerender', () => {
expect(initialTime).toBe($2('#time').text()) expect(initialTime).toBe($2('#time').text())
expect(res2.headers.get('x-nextjs-cache')).toMatch(/(HIT|STALE)/) expect(res2.headers.get('x-nextjs-cache')).toMatch(/(HIT|STALE)/)
})
const res3 = await fetchViaHTTP( const res3 = await fetchViaHTTP(
next.url, next.url,
...@@ -2315,6 +2297,7 @@ describe('Prerender', () => { ...@@ -2315,6 +2297,7 @@ describe('Prerender', () => {
const revalidateData = await res3.json() const revalidateData = await res3.json()
expect(revalidateData.revalidated).toBe(true) expect(revalidateData.revalidated).toBe(true)
await retry(async () => {
const res4 = await fetchViaHTTP( const res4 = await fetchViaHTTP(
next.url, next.url,
'/blocking-fallback/test-if-generated-2' '/blocking-fallback/test-if-generated-2'
...@@ -2324,6 +2307,7 @@ describe('Prerender', () => { ...@@ -2324,6 +2307,7 @@ describe('Prerender', () => {
expect($4('#time').text()).not.toBe(initialTime) expect($4('#time').text()).not.toBe(initialTime)
expect(res4.headers.get('x-nextjs-cache')).toMatch(/(HIT|STALE)/) expect(res4.headers.get('x-nextjs-cache')).toMatch(/(HIT|STALE)/)
}) })
})
it('should on-demand revalidate for revalidate: false', async () => { it('should on-demand revalidate for revalidate: false', async () => {
const html = await renderViaHTTP( const html = await renderViaHTTP(
......
...@@ -8,7 +8,7 @@ import { ChildProcess } from 'child_process' ...@@ -8,7 +8,7 @@ import { ChildProcess } from 'child_process'
import { createNextInstall } from '../create-next-install' import { createNextInstall } from '../create-next-install'
import { Span } from 'next/dist/trace' import { Span } from 'next/dist/trace'
import webdriver from '../next-webdriver' import webdriver from '../next-webdriver'
import { renderViaHTTP, fetchViaHTTP, findPort, retry } from 'next-test-utils' import { renderViaHTTP, fetchViaHTTP, findPort } from 'next-test-utils'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { once } from 'events' import { once } from 'events'
import { BrowserInterface } from '../browsers/base' import { BrowserInterface } from '../browsers/base'
...@@ -69,7 +69,7 @@ export class NextInstance { ...@@ -69,7 +69,7 @@ export class NextInstance {
public env: Record<string, string> public env: Record<string, string>
public forcedPort?: string public forcedPort?: string
public dirSuffix: string = '' public dirSuffix: string = ''
public serverReadyPattern?: RegExp = /^\s* ✓ Ready in / public serverReadyPattern?: RegExp = / ✓ Ready in /
constructor(opts: NextInstanceOpts) { constructor(opts: NextInstanceOpts) {
this.env = {} this.env = {}
...@@ -491,7 +491,7 @@ export class NextInstance { ...@@ -491,7 +491,7 @@ export class NextInstance {
public async patchFile( public async patchFile(
filename: string, filename: string,
content: string | ((content: string) => string), content: string | ((content: string) => string),
retryWithTempContent?: (context: { newFile: boolean }) => Promise<void> runWithTempContent?: (context: { newFile: boolean }) => Promise<void>
): Promise<{ newFile: boolean }> { ): Promise<{ newFile: boolean }> {
const outputPath = path.join(this.testDir, filename) const outputPath = path.join(this.testDir, filename)
const newFile = !existsSync(outputPath) const newFile = !existsSync(outputPath)
...@@ -503,9 +503,9 @@ export class NextInstance { ...@@ -503,9 +503,9 @@ export class NextInstance {
typeof content === 'function' ? content(previousContent) : content typeof content === 'function' ? content(previousContent) : content
) )
if (retryWithTempContent) { if (runWithTempContent) {
try { try {
await retry(() => retryWithTempContent({ newFile })) await runWithTempContent({ newFile })
} finally { } finally {
if (previousContent === undefined) { if (previousContent === undefined) {
await fs.rm(outputPath) await fs.rm(outputPath)
......
...@@ -153,7 +153,7 @@ export class NextDevInstance extends NextInstance { ...@@ -153,7 +153,7 @@ export class NextDevInstance extends NextInstance {
public override async patchFile( public override async patchFile(
filename: string, filename: string,
content: string | ((contents: string) => string), content: string | ((contents: string) => string),
retryWithTempContent?: (context: { newFile: boolean }) => Promise<void> runWithTempContent?: (context: { newFile: boolean }) => Promise<void>
) { ) {
const isServerRunning = this.childProcess && !this.isStopping const isServerRunning = this.childProcess && !this.isStopping
const cliOutputLength = this.cliOutput.length const cliOutputLength = this.cliOutput.length
...@@ -167,17 +167,21 @@ export class NextDevInstance extends NextInstance { ...@@ -167,17 +167,21 @@ export class NextDevInstance extends NextInstance {
if (newFile) { if (newFile) {
await this.handleDevWatchDelayAfterChange(filename) await this.handleDevWatchDelayAfterChange(filename)
} else if (filename.startsWith('next.config')) { } else if (filename.startsWith('next.config')) {
if (!this.cliOutput.slice(cliOutputLength).includes('Ready in')) { await retry(async () => {
const cliOutput = this.cliOutput.slice(cliOutputLength)
if (!this.serverReadyPattern.test(cliOutput)) {
throw new Error('Server has not finished restarting.') throw new Error('Server has not finished restarting.')
} }
})
} }
} }
} }
if (retryWithTempContent) { if (runWithTempContent) {
return super.patchFile(filename, content, async ({ newFile }) => { return super.patchFile(filename, content, async ({ newFile }) => {
await waitForChanges({ newFile }) await waitForChanges({ newFile })
await retryWithTempContent({ newFile }) await runWithTempContent({ newFile })
}) })
} }
......
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