Skip to content

Commit 89e6837

Browse files
authored
Update next/font tests for Turbopack (#61665)
## What? Updates the tests to correctly assert 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-2359
1 parent 8a11319 commit 89e6837

File tree

2 files changed

+98
-101
lines changed

2 files changed

+98
-101
lines changed

test/e2e/next-font/google-font-mocked-responses.js

+8-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const path = require('path')
2-
31
module.exports = {
42
'https://fonts.googleapis.com/css2?family=Open+Sans:[email protected]&display=swap': `
53
/* cyrillic-ext */
@@ -501,47 +499,35 @@ module.exports = {
501499
font-style: normal;
502500
font-weight: 400;
503501
font-display: swap;
504-
src: url(${path.join(
505-
__dirname,
506-
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTBv7Tp05GNyXkb24.woff2'
507-
)}) format('woff2');
508-
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB;
502+
src: url(https://fonts.gstatic.com/s/fraunces/v31/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTBv7Tp05GNyXkb24.woff2) format('woff2');
503+
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB;
509504
}
510505
/* latin-ext */
511506
@font-face {
512507
font-family: 'Fraunces';
513508
font-style: normal;
514509
font-weight: 400;
515510
font-display: swap;
516-
src: url(${path.join(
517-
__dirname,
518-
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTB_7Tp05GNyXkb24.woff2'
519-
)}) format('woff2');
520-
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
511+
src: url(https://fonts.gstatic.com/s/fraunces/v31/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTB_7Tp05GNyXkb24.woff2) format('woff2');
512+
unicode-range: U+0100-02AF, U+0304, U+0308, U+0329, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
521513
}
522514
/* latin */
523515
@font-face {
524516
font-family: 'Fraunces';
525517
font-style: normal;
526518
font-weight: 400;
527519
font-display: swap;
528-
src: url(${path.join(
529-
__dirname,
530-
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTCf7Tp05GNyXk.woff2'
531-
)}) format('woff2');
532-
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
520+
src: url(https://fonts.gstatic.com/s/fraunces/v31/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTCf7Tp05GNyXk.woff2) format('woff2');
521+
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
533522
}`,
534523
'https://fonts.googleapis.com/css2?family=Indie+Flower:wght@400&display=swap': `/* latin */
535524
@font-face {
536525
font-family: 'Indie Flower';
537526
font-style: normal;
538527
font-weight: 400;
539528
font-display: swap;
540-
src: url(${path.join(
541-
__dirname,
542-
'./fonts/m8JVjfNVeKWVnh3QMuKkFcZVaUuH99GUDg.woff2'
543-
)}) format('woff2');
544-
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
529+
src: url(https://fonts.gstatic.com/s/indieflower/v21/m8JVjfNVeKWVnh3QMuKkFcZVaUuH99GUDg.woff2) format('woff2');
530+
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
545531
}`,
546532
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght,SOFT,WONK@0,9..144,100..900,0..100,0..1;1,9..144,100..900,0..100,0..1&display=swap': `
547533
/* vietnamese */

test/e2e/next-font/index.test.ts

+90-79
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ function getClassNameRegex(className: string): RegExp {
1616
: new RegExp(`^__${className}_.{6}$`) // e.g. `__className_a8cc56`
1717
}
1818

19+
function hrefMatchesFontWithSizeAdjust(href: string) {
20+
if (process.env.TURBOPACK) {
21+
expect(href).toMatch(
22+
// Turbopack includes the file hash
23+
/\/_next\/static\/media\/(.*)-s\.p\.(.*)\.woff2/
24+
)
25+
} else {
26+
expect(href).toMatch(/\/_next\/static\/media\/(.*)-s\.p\.woff2/)
27+
}
28+
}
29+
30+
function hrefMatchesFontWithoutSizeAdjust(href: string) {
31+
if (process.env.TURBOPACK) {
32+
expect(href).toMatch(
33+
// Turbopack includes the file hash
34+
/\/_next\/static\/media\/(.*)\.p\.(.*)\.woff2/
35+
)
36+
} else {
37+
expect(href).toMatch(/\/_next\/static\/media\/(.*)\.p\.woff2/)
38+
}
39+
}
40+
1941
describe('next/font', () => {
2042
for (const fixture of ['app', 'app-old']) {
2143
describe(fixture, () => {
@@ -49,8 +71,6 @@ describe('next/font', () => {
4971

5072
if ((global as any).isNextDev) {
5173
it('should use production cache control for fonts', async () => {
52-
const html = await next.render('/')
53-
console.log({ html })
5474
const $ = await next.render$('/')
5575
const link = $('[rel="preload"][as="font"]').attr('href')
5676
expect(link).toBeDefined()
@@ -334,22 +354,21 @@ describe('next/font', () => {
334354
return a.attribs.href.localeCompare(b.attribs.href)
335355
})
336356
// From /_app
337-
expect(links[0].attribs).toEqual({
338-
as: 'font',
339-
crossorigin: 'anonymous',
340-
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
341-
rel: 'preload',
342-
type: 'font/woff2',
343-
'data-next-font': 'size-adjust',
344-
})
345-
expect(links[1].attribs).toEqual({
346-
as: 'font',
347-
crossorigin: 'anonymous',
348-
href: '/_next/static/media/675c25f648fd6a30-s.p.woff2',
349-
rel: 'preload',
350-
type: 'font/woff2',
351-
'data-next-font': 'size-adjust',
352-
})
357+
const attributes = links[0].attribs
358+
expect(attributes.as).toBe('font')
359+
expect(attributes.crossorigin).toBe('anonymous')
360+
hrefMatchesFontWithSizeAdjust(attributes.href)
361+
expect(attributes.rel).toBe('preload')
362+
expect(attributes.type).toBe('font/woff2')
363+
expect(attributes['data-next-font']).toBe('size-adjust')
364+
365+
const attributes2 = links[1].attribs
366+
expect(attributes2.as).toBe('font')
367+
expect(attributes2.crossorigin).toBe('anonymous')
368+
hrefMatchesFontWithSizeAdjust(attributes2.href)
369+
expect(attributes2.rel).toBe('preload')
370+
expect(attributes2.type).toBe('font/woff2')
371+
expect(attributes2['data-next-font']).toBe('size-adjust')
353372
})
354373

355374
test('page without fonts', async () => {
@@ -361,14 +380,14 @@ describe('next/font', () => {
361380

362381
// From _app
363382
expect($('link[as="font"]').length).toBe(1)
364-
expect($('link[as="font"]').get(0).attribs).toEqual({
365-
as: 'font',
366-
crossorigin: 'anonymous',
367-
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
368-
rel: 'preload',
369-
type: 'font/woff2',
370-
'data-next-font': 'size-adjust',
371-
})
383+
384+
const attributes = $('link[as="font"]').get(0).attribs
385+
expect(attributes.as).toBe('font')
386+
expect(attributes.crossorigin).toBe('anonymous')
387+
hrefMatchesFontWithSizeAdjust(attributes.href)
388+
expect(attributes.rel).toBe('preload')
389+
expect(attributes.type).toBe('font/woff2')
390+
expect(attributes['data-next-font']).toBe('size-adjust')
372391
})
373392

374393
test('page with local fonts', async () => {
@@ -380,17 +399,13 @@ describe('next/font', () => {
380399

381400
// Preload
382401
expect($('link[as="font"]').length).toBe(5)
383-
expect(
384-
Array.from($('link[as="font"]'))
385-
.map((el) => el.attribs.href)
386-
.sort()
387-
).toEqual([
388-
'/_next/static/media/02205c9944024f15-s.p.woff2',
389-
'/_next/static/media/0812efcfaefec5ea-s.p.woff2',
390-
'/_next/static/media/1deec1af325840fd-s.p.woff2',
391-
'/_next/static/media/ab6fdae82d1a8d92-s.p.woff2',
392-
'/_next/static/media/d55edb6f37902ebf-s.p.woff2',
393-
])
402+
const hrefs = Array.from($('link[as="font"]'))
403+
.map((el) => el.attribs.href)
404+
.sort()
405+
for (const href of hrefs) {
406+
hrefMatchesFontWithSizeAdjust(href)
407+
}
408+
expect(hrefs.length).toBe(5)
394409
})
395410

396411
test('google fonts with multiple weights/styles', async () => {
@@ -403,20 +418,15 @@ describe('next/font', () => {
403418
// Preload
404419
expect($('link[as="font"]').length).toBe(8)
405420

406-
expect(
407-
Array.from($('link[as="font"]'))
408-
.map((el) => el.attribs.href)
409-
.sort()
410-
).toEqual([
411-
'/_next/static/media/0812efcfaefec5ea-s.p.woff2',
412-
'/_next/static/media/4f3dcdf40b3ca86d-s.p.woff2',
413-
'/_next/static/media/560a6db6ac485cb1-s.p.woff2',
414-
'/_next/static/media/686d1702f12625fe-s.p.woff2',
415-
'/_next/static/media/86d92167ff02c708-s.p.woff2',
416-
'/_next/static/media/9ac01b894b856187-s.p.woff2',
417-
'/_next/static/media/c9baea324111137d-s.p.woff2',
418-
'/_next/static/media/fb68b4558e2a718e-s.p.woff2',
419-
])
421+
const hrefs = Array.from($('link[as="font"]'))
422+
.map((el) => el.attribs.href)
423+
.sort()
424+
425+
for (const href of hrefs) {
426+
hrefMatchesFontWithSizeAdjust(href)
427+
}
428+
429+
expect(hrefs.length).toBe(8)
420430
})
421431

422432
test('font without preloadable subsets', async () => {
@@ -431,39 +441,40 @@ describe('next/font', () => {
431441

432442
// From _app
433443
expect($('link[as="font"]').length).toBe(1)
434-
expect($('link[as="font"]').get(0).attribs).toEqual({
435-
as: 'font',
436-
crossorigin: 'anonymous',
437-
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
438-
rel: 'preload',
439-
type: 'font/woff2',
440-
'data-next-font': 'size-adjust',
441-
})
444+
const attributes = $('link[as="font"]').get(0).attribs
445+
446+
expect(attributes.as).toBe('font')
447+
expect(attributes.crossorigin).toBe('anonymous')
448+
hrefMatchesFontWithSizeAdjust(attributes.href)
449+
expect(attributes.rel).toBe('preload')
450+
expect(attributes.type).toBe('font/woff2')
451+
expect(attributes['data-next-font']).toBe('size-adjust')
442452
})
443453

444454
test('font without size adjust', async () => {
445455
const html = await renderViaHTTP(next.url, '/with-fallback')
446456
const $ = cheerio.load(html)
447-
const links = Array.from($('link[as="font"]')).sort((a, b) => {
448-
return a.attribs.href.localeCompare(b.attribs.href)
449-
})
450-
expect(links[1].attribs).toEqual({
451-
as: 'font',
452-
crossorigin: 'anonymous',
453-
href: '/_next/static/media/0812efcfaefec5ea.p.woff2',
454-
rel: 'preload',
455-
type: 'font/woff2',
456-
'data-next-font': '',
457-
})
458-
459-
expect(links[2].attribs).toEqual({
460-
as: 'font',
461-
crossorigin: 'anonymous',
462-
href: '/_next/static/media/ab6fdae82d1a8d92.p.woff2',
463-
rel: 'preload',
464-
type: 'font/woff2',
465-
'data-next-font': '',
466-
})
457+
const links = Array.from($('link[as="font"]'))
458+
.map((node) => node.attribs)
459+
.sort((a, b) => {
460+
return a.href.localeCompare(b.href)
461+
})
462+
const attributes = links[1]
463+
expect(attributes.as).toBe('font')
464+
expect(attributes.crossorigin).toBe('anonymous')
465+
hrefMatchesFontWithoutSizeAdjust(attributes.href)
466+
expect(attributes.rel).toBe('preload')
467+
expect(attributes.type).toBe('font/woff2')
468+
expect(attributes['data-next-font']).toBe('')
469+
470+
const attributes2 = links[2]
471+
472+
expect(attributes2.as).toBe('font')
473+
expect(attributes2.crossorigin).toBe('anonymous')
474+
hrefMatchesFontWithoutSizeAdjust(attributes2.href)
475+
expect(attributes2.rel).toBe('preload')
476+
expect(attributes2.type).toBe('font/woff2')
477+
expect(attributes2['data-next-font']).toBe('')
467478
})
468479
})
469480

0 commit comments

Comments
 (0)