Unverified Commit 252f5f8d authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack build: Fix client-404 tests (#67846)

The test was written in a way that asserts internals but that is not
needed for this test. Instead we can use interception of requests of the
browser to make the actual requests fail, which more closely mirrors
reality.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent 6dff8853
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { check, waitFor } from 'next-test-utils'
export default (context, isProd = false) => {
describe('Client Navigation 404', () => {
describe('should show 404 upon client replacestate', () => {
it('should navigate the page', async () => {
const browser = await webdriver(context.appPort, '/asd')
const serverCode = await browser
.waitForElementByCss('#errorStatusCode')
.text()
await browser.waitForElementByCss('#errorGoHome').click()
await browser.waitForElementByCss('#hellom8').back()
const clientCode = await browser
.waitForElementByCss('#errorStatusCode')
.text()
expect({ serverCode, clientCode }).toMatchObject({
serverCode: '404',
clientCode: '404',
})
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')
})
if (isProd) {
it('should hard navigate to URL on failing to load missing bundle', async () => {
const browser = await webdriver(context.appPort, '/to-missing-link')
await browser.eval(() => (window.beforeNav = 'hi'))
expect(
await browser.eval(() =>
document.querySelector('script[src*="pages/missing"]')
)
).toBeFalsy()
await browser.elementByCss('#to-missing').moveTo()
await waitFor(2000)
expect(
await browser.eval(() =>
document.querySelector('script[src*="pages/missing"]')
)
).toBeTruthy()
await browser.elementByCss('#to-missing').click()
await waitFor(2000)
expect(await browser.eval(() => window.beforeNav)).not.toBe('hi')
await check(() => browser.elementByCss('#missing').text(), /poof/)
})
}
})
}
......@@ -8,12 +8,62 @@ import {
killApp,
nextBuild,
nextStart,
getBuildManifest,
retry,
} from 'next-test-utils'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import { check } from 'next-test-utils'
// test suite
import clientNavigation from './client-navigation'
const clientNavigation = (context, isProd = false) => {
describe('Client Navigation 404', () => {
describe('should show 404 upon client replacestate', () => {
it('should navigate the page', async () => {
const browser = await webdriver(context.appPort, '/asd')
const serverCode = await browser
.waitForElementByCss('#errorStatusCode')
.text()
await browser.waitForElementByCss('#errorGoHome').click()
await browser.waitForElementByCss('#hellom8').back()
const clientCode = await browser
.waitForElementByCss('#errorStatusCode')
.text()
expect({ serverCode, clientCode }).toMatchObject({
serverCode: '404',
clientCode: '404',
})
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')
})
if (isProd) {
it('should hard navigate to URL on failing to load missing bundle', async () => {
const browser = await webdriver(context.appPort, '/to-missing-link', {
beforePageLoad(page) {
page.route('**/pages/missing**', (route) => {
route.abort('internetdisconnected')
})
},
})
await browser.eval(() => (window.beforeNav = 'hi'))
await browser.elementByCss('#to-missing').click()
await retry(async () => {
expect(await browser.url()).toContain('/missing')
})
expect(await browser.elementByCss('#missing').text()).toBe('poof')
expect(await browser.eval(() => window.beforeNav)).not.toBe('hi')
})
}
})
}
const context = {}
const appDir = join(__dirname, '../')
......@@ -45,15 +95,6 @@ describe('Client 404', () => {
await nextBuild(appDir)
context.appPort = await findPort()
context.server = await nextStart(appDir, context.appPort)
const manifest = await getBuildManifest(appDir)
const files = manifest.pages['/missing'].filter((d) =>
/static[\\/]chunks[\\/]pages/.test(d)
)
if (files.length < 1) {
throw new Error('oops!')
}
await Promise.all(files.map((f) => fs.remove(join(appDir, '.next', f))))
})
afterAll(() => killApp(context.server))
......
......@@ -7176,12 +7176,12 @@
"runtimeError": false
},
"test/integration/client-404/test/index.test.js": {
"passed": [],
"failed": [
"passed": [
"Client 404 production mode Client Navigation 404 should hard navigate to URL on failing to load bundle",
"Client 404 production mode Client Navigation 404 should hard navigate to URL on failing to load missing bundle",
"Client 404 production mode Client Navigation 404 should show 404 upon client replacestate should navigate the page"
],
"failed": [],
"pending": [
"Client 404 development mode Client Navigation 404 should hard navigate to URL on failing to load bundle",
"Client 404 development mode Client Navigation 404 should show 404 upon client replacestate should navigate the page"
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