Skip to content

Commit

Permalink
Turbopack build: Fix client-404 tests (#67846)
Browse files Browse the repository at this point in the history
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 #

-->
  • Loading branch information
timneutkens authored Jul 17, 2024
1 parent 6dff885 commit 252f5f8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 73 deletions.
58 changes: 0 additions & 58 deletions test/integration/client-404/test/client-navigation.js

This file was deleted.

67 changes: 54 additions & 13 deletions test/integration/client-404/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '../')
Expand Down Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 252f5f8

Please sign in to comment.