Unverified Commit 8e0700c7 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Fix the legacy image preload order assertion for React 18 (#99572)

PR #99528 removed the automatic individual-test retries introduced in
#89929. This exposed a failure in the React 18.3.1 canary job:
https://github.com/vercel/next.js/actions/runs/36976099741/job/110749496538

The test checked preload order in the hydrated DOM. React 18's
client-side `next/head` updates can reorder those links, although the
SSR HTML already places CSS preloads before image preloads.

Jest deferred the failed test's retry until the end of the describe
block. A later test had moved the shared browser to `/loader-prop` by
then. That page has no preloads, so the retried assertion passed without
checking the intended page's preload order.

The assertion now checks the SSR HTML for `/` directly. It requires both
CSS and image preloads and verifies that all CSS preloads precede the
image preloads.

Verified by adding the `run-react-18-tests` label to the PR on creation.
parent 621015d4
...@@ -22,23 +22,6 @@ describe('Image Component Tests', () => { ...@@ -22,23 +22,6 @@ describe('Image Component Tests', () => {
return false return false
} }
async function hasImagePreloadBeforeCSSPreload(browser) {
const links = await browser.elementsByCss('link')
let foundImage = false
for (const link of links) {
const rel = await link.getAttribute('rel')
if (rel === 'preload') {
const linkAs = await link.getAttribute('as')
if (linkAs === 'image') {
foundImage = true
} else if (linkAs === 'style' && foundImage) {
return true
}
}
}
return false
}
function runTests(browser: () => Browser) { function runTests(browser: () => Browser) {
it('should render an image tag', async () => { it('should render an image tag', async () => {
expect(await browser().hasElementByCssSelector('img')).toBeTruthy() expect(await browser().hasElementByCssSelector('img')).toBeTruthy()
...@@ -333,8 +316,19 @@ describe('Image Component Tests', () => { ...@@ -333,8 +316,19 @@ describe('Image Component Tests', () => {
) )
).toBe(true) ).toBe(true)
}) })
it('should not create any preload tags higher up the page than CSS preload tags', async () => { it('should render image preload tags after CSS preload tags in SSR HTML', async () => {
expect(await hasImagePreloadBeforeCSSPreload(browser)).toBe(false) // This assertion checks the SSR order before React 18 client-side head
// updates can reorder the preload links.
const $ = await next.render$('/')
const preloadTypes = $('link[rel="preload"]')
.toArray()
.map((link) => $(link).attr('as'))
const firstImagePreloadIndex = preloadTypes.indexOf('image')
const lastStylePreloadIndex = preloadTypes.lastIndexOf('style')
expect(firstImagePreloadIndex).toBeGreaterThanOrEqual(0)
expect(lastStylePreloadIndex).toBeGreaterThanOrEqual(0)
expect(firstImagePreloadIndex).toBeGreaterThan(lastStylePreloadIndex)
}) })
it('should add data-nimg data attribute based on layout', async () => { it('should add data-nimg data attribute based on layout', async () => {
expect( expect(
......
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