Unverified Commit 55b472b5 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix app-dir/next-font for Turbopack (#61678)

## What?

Updates the test checks for Turbopack.

<!-- 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 #

-->


Closes NEXT-2364
parent 933c9865
......@@ -8,13 +8,6 @@ import { join } from 'path'
// used by that server module).
// This is a known limitation of flight-client-entry-plugin and we will improve
// this in the future.
// TODO-APP: After the above issue is fixed, we should change this function to
// be strictly equal instead: `expect(arr).toEqual(expected)`.
function containObjects(arr: any[], expected: any[]) {
for (const o of expected) {
expect(arr).toContainEqual(o)
}
}
const getAttrs = (elems: Cheerio) =>
Array.from(elems)
......@@ -52,8 +45,12 @@ describe('app dir - next/font', () => {
// layout
expect(JSON.parse($('#root-layout').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
variable: expect.stringMatching(
process.env.TURBOPACK ? /^variable_.*/ : /^__variable_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font1_.{6}', '__font1_Fallback_.{6}'$/
......@@ -62,8 +59,12 @@ describe('app dir - next/font', () => {
})
// page
expect(JSON.parse($('#root-page').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
variable: expect.stringMatching(
process.env.TURBOPACK ? /^variable_.*/ : /^__variable_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font2_.{6}', '__font2_Fallback_.{6}'$/
......@@ -72,7 +73,9 @@ describe('app dir - next/font', () => {
})
// Comp
expect(JSON.parse($('#root-comp').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font3_.{6}', '__font3_Fallback_.{6}'$/
......@@ -88,8 +91,12 @@ describe('app dir - next/font', () => {
// root layout
expect(JSON.parse($('#root-layout').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
variable: expect.stringMatching(
process.env.TURBOPACK ? /^variable_.*/ : /^__variable_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font1_.{6}', '__font1_Fallback_.{6}'$/
......@@ -99,7 +106,9 @@ describe('app dir - next/font', () => {
// layout
expect(JSON.parse($('#client-layout').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font4_.{6}', '__font4_Fallback_.{6}'$/
......@@ -109,7 +118,9 @@ describe('app dir - next/font', () => {
})
// page
expect(JSON.parse($('#client-page').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font5_.{6}', '__font5_Fallback_.{6}'$/
......@@ -119,7 +130,9 @@ describe('app dir - next/font', () => {
})
// Comp
expect(JSON.parse($('#client-comp').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font6_.{6}', '__font6_Fallback_.{6}'$/
......@@ -131,13 +144,17 @@ describe('app dir - next/font', () => {
it('should transform code in node_modules', async () => {
const $ = await next.render$('/third-party')
expect(JSON.parse($('#third-party-page').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
className: expect.stringMatching(
process.env.TURBOPACK ? /^className_.*/ : /^__className_.*/
),
style: {
fontFamily: expect.stringMatching(
/^'__font1_.{6}', '__font1_Fallback_.{6}'$/
),
},
variable: expect.stringMatching(/^__variable_.{6}$/),
variable: expect.stringMatching(
process.env.TURBOPACK ? /^variable_.*/ : /^__variable_.*/
),
})
})
})
......@@ -279,30 +296,23 @@ describe('app dir - next/font', () => {
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(0)
// From root layout
containObjects(getAttrs($('link[as="font"]')), [
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/f1d1a01b6d3e0953-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/92f44bb82993d879-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/934c4b7cb736f2a3-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
])
const items = getAttrs($('link[as="font"]'))
for (const item of items) {
expect(item.as).toBe('font')
expect(item.crossorigin).toBe('')
if (process.env.TURBOPACK) {
expect(item.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.(.*)\.woff2/
)
} else {
expect(item.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.woff2/
)
}
expect(item.rel).toBe('preload')
expect(item.type).toBe('font/woff2')
}
})
it('should preload correctly with client components', async () => {
......@@ -311,30 +321,23 @@ describe('app dir - next/font', () => {
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(0)
// From root layout
containObjects(getAttrs($('link[as="font"]')), [
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/1ad1613b8b1aead5-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/934c4b7cb736f2a3-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/6376730d43ea1694-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
])
const links = getAttrs($('link[as="font"]'))
for (const link of links) {
expect(link.as).toBe('font')
expect(link.crossorigin).toBe('')
if (process.env.TURBOPACK) {
expect(link.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.(.*)\.woff2/
)
} else {
expect(link.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.woff2/
)
}
expect(link.rel).toBe('preload')
expect(link.type).toBe('font/woff2')
}
})
it('should preload correctly with layout using fonts', async () => {
......@@ -343,23 +346,23 @@ describe('app dir - next/font', () => {
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(0)
// From root layout
containObjects(getAttrs($('link[as="font"]')), [
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/7881252a527aaa0c-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/934c4b7cb736f2a3-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
])
const links = getAttrs($('link[as="font"]'))
for (const link of links) {
expect(link.as).toBe('font')
expect(link.crossorigin).toBe('')
if (process.env.TURBOPACK) {
expect(link.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.(.*)\.woff2/
)
} else {
expect(link.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.woff2/
)
}
expect(link.rel).toBe('preload')
expect(link.type).toBe('font/woff2')
}
})
it('should preload correctly with page using fonts', async () => {
......@@ -368,23 +371,23 @@ describe('app dir - next/font', () => {
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(0)
// From root layout
containObjects(getAttrs($('link[as="font"]')), [
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/beb742c171df2176-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
{
as: 'font',
crossorigin: '',
href: '/_next/static/media/934c4b7cb736f2a3-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
},
])
const links = getAttrs($('link[as="font"]'))
for (const link of links) {
expect(link.as).toBe('font')
expect(link.crossorigin).toBe('')
if (process.env.TURBOPACK) {
expect(link.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.(.*)\.woff2/
)
} else {
expect(link.href).toMatch(
/\/_next\/static\/media\/(.*)-s.p.woff2/
)
}
expect(link.rel).toBe('preload')
expect(link.type).toBe('font/woff2')
}
})
})
......@@ -426,9 +429,14 @@ describe('app dir - next/font', () => {
'link[as="font"]'
)
expect(preloadBeforeNavigation.length).toBe(1)
expect(
await preloadBeforeNavigation[0].getAttribute('href')
).toBe('/_next/static/media/0ae584388df2bd05-s.p.woff2')
const href = await preloadBeforeNavigation[0].getAttribute('href')
if (process.env.TURBOPACK) {
expect(href).toMatch(
/\/_next\/static\/media\/(.*)-s\.p\.(.*)\.woff2/
)
} else {
expect(href).toMatch(/\/_next\/static\/media\/(.*)-s\.p\.woff2/)
}
// Navigate to a page that also imports that font
await browser.elementByCss('a').click()
......@@ -439,9 +447,17 @@ describe('app dir - next/font', () => {
'link[as="font"]'
)
expect(preloadAfterNavigation.length).toBe(1)
expect(await preloadAfterNavigation[0].getAttribute('href')).toBe(
'/_next/static/media/0ae584388df2bd05-s.p.woff2'
)
const href2 = await preloadAfterNavigation[0].getAttribute('href')
if (process.env.TURBOPACK) {
expect(href2).toMatch(
/\/_next\/static\/media\/(.*)-s\.p\.(.*)\.woff2/
)
} else {
expect(href2).toMatch(
/\/_next\/static\/media\/(.*)-s\.p\.woff2/
)
}
})
})
......
......@@ -3821,15 +3821,15 @@
"test/e2e/app-dir/next-font/next-font.test.ts": {
"passed": [
"app dir - next/font app app dir - next-font computed styles should have correct styles at /",
"app dir - next/font app app dir - next-font computed styles should have correct styles at /client"
],
"failed": [
"app dir - next/font app app dir - next-font Dev errors should recover on font loader error",
"app dir - next/font app app dir - next-font computed styles should have correct styles at /client",
"app dir - next/font app app dir - next-font import values should have correct values at /",
"app dir - next/font app app dir - next-font import values should have correct values at /client",
"app dir - next/font app app dir - next-font import values should transform code in node_modules",
"app dir - next/font app app dir - next-font navigation should not have duplicate preload tags on navigation"
],
"failed": [
"app dir - next/font app app dir - next-font Dev errors should recover on font loader error"
],
"pending": [],
"flakey": [],
"runtimeError": false
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