From e5305df848542a6d5e4359053a744735a8bc920c Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 11 Jun 2020 11:59:25 -0500 Subject: [PATCH] Add test case for loading 404 on invalid bundle (#14082) This adds a test case to ensure hard navigating when a client bundle fails to load is occurring correctly x-ref: https://github.com/vercel/next.js/issues/13516 --- .../client-404/pages/invalid-link.js | 7 ++++ .../client-404/test/client-navigation.js | 9 ++++ .../integration/client-404/test/index.test.js | 41 +++++++++++++++---- 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 test/integration/client-404/pages/invalid-link.js diff --git a/test/integration/client-404/pages/invalid-link.js b/test/integration/client-404/pages/invalid-link.js new file mode 100644 index 00000000000000..841840464a5284 --- /dev/null +++ b/test/integration/client-404/pages/invalid-link.js @@ -0,0 +1,7 @@ +import Link from 'next/link' + +export default () => ( + + to 404 + +) diff --git a/test/integration/client-404/test/client-navigation.js b/test/integration/client-404/test/client-navigation.js index 84abab330b5bec..060a8a030074f7 100644 --- a/test/integration/client-404/test/client-navigation.js +++ b/test/integration/client-404/test/client-navigation.js @@ -1,5 +1,6 @@ /* eslint-env jest */ import webdriver from 'next-webdriver' +import { check } from 'next-test-utils' export default (context) => { describe('Client Navigation 404', () => { @@ -22,5 +23,13 @@ export default (context) => { await browser.close() }) }) + + it('should hard navigate to URL on failing to load bundle', async () => { + const browser = await webdriver(context.appPort, '/invalid-link') + await browser.eval(() => (window.beforeNav = 'hi')) + await browser.elementByCss('#to-nonexistent').click() + await check(() => browser.elementByCss('#errorStatusCode').text(), /404/) + expect(await browser.eval(() => window.beforeNav)).not.toBe('hi') + }) }) } diff --git a/test/integration/client-404/test/index.test.js b/test/integration/client-404/test/index.test.js index d9f4b4e7696d31..79f41050d679ee 100644 --- a/test/integration/client-404/test/index.test.js +++ b/test/integration/client-404/test/index.test.js @@ -1,23 +1,48 @@ /* eslint-env jest */ import { join } from 'path' -import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils' +import { + renderViaHTTP, + findPort, + launchApp, + killApp, + nextBuild, + nextStart, +} from 'next-test-utils' // test suite import clientNavigation from './client-navigation' const context = {} +const appDir = join(__dirname, '../') jest.setTimeout(1000 * 60 * 5) +const runTests = () => { + clientNavigation(context, (p, q) => renderViaHTTP(context.appPort, p, q)) +} + describe('Client 404', () => { - beforeAll(async () => { - context.appPort = await findPort() - context.server = await launchApp(join(__dirname, '../'), context.appPort) + describe('dev mode', () => { + beforeAll(async () => { + context.appPort = await findPort() + context.server = await launchApp(appDir, context.appPort) + + // pre-build page at the start + await renderViaHTTP(context.appPort, '/') + }) + afterAll(() => killApp(context.server)) - // pre-build page at the start - await renderViaHTTP(context.appPort, '/') + runTests() }) - afterAll(() => killApp(context.server)) - clientNavigation(context, (p, q) => renderViaHTTP(context.appPort, p, q)) + describe('production mode', () => { + beforeAll(async () => { + await nextBuild(appDir) + context.appPort = await findPort() + context.server = await nextStart(appDir, context.appPort) + }) + afterAll(() => killApp(context.server)) + + runTests() + }) })