Files
next.js/test/e2e/next-font/without-preloaded-fonts.test.ts
4588a73542 Convert tests using createNext -> nextTestSetup (#93767)
## What?

Converts existing `createNext()` usage into `nextTestSetup()`. 

`createNext()` was the setup step we had before `nextTestSetup()` was
added.

This PR focused on the simple conversion cases. There will be a
follow-up to complete the last few.

---------

Co-authored-by: Cursor <[email protected]>
2026-05-12 13:16:31 +02:00

113 lines
3.1 KiB
TypeScript

import cheerio from 'cheerio'
import { FileRef, nextTestSetup } from 'e2e-utils'
import { renderViaHTTP } from 'next-test-utils'
import { join } from 'path'
const mockedGoogleFontResponses = require.resolve(
'./google-font-mocked-responses.js'
)
describe('next/font/google without-preloaded-fonts without _app', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
const { next } = nextTestSetup({
files: {
'pages/no-preload.js': new FileRef(
join(__dirname, 'without-preloaded-fonts/pages/no-preload.js')
),
'pages/without-fonts.js': new FileRef(
join(__dirname, 'without-preloaded-fonts/pages/without-fonts.js')
),
},
env: {
NEXT_FONT_GOOGLE_MOCKED_RESPONSES: mockedGoogleFontResponses,
},
})
test('without preload', async () => {
const html = await renderViaHTTP(next.url, '/no-preload')
const $ = cheerio.load(html)
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(1)
expect($('link[rel="preconnect"]').get(0).attribs).toEqual({
crossorigin: 'anonymous',
href: '/',
rel: 'preconnect',
'data-next-font': 'size-adjust',
})
// Preload
expect($('link[as="font"]').length).toBe(0)
})
test('without fonts', async () => {
const html = await renderViaHTTP(next.url, '/without-fonts')
const $ = cheerio.load(html)
expect($('link[rel="preconnect"]').length).toBe(0)
expect($('link[as="font"]').length).toBe(0)
})
})
describe('next/font/google no preloads with _app', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
const { next } = nextTestSetup({
files: {
'pages/_app.js': new FileRef(
join(__dirname, 'without-preloaded-fonts/pages/_app.js')
),
'pages/no-preload.js': new FileRef(
join(__dirname, 'without-preloaded-fonts/pages/no-preload.js')
),
'pages/without-fonts.js': new FileRef(
join(__dirname, 'without-preloaded-fonts/pages/without-fonts.js')
),
},
env: {
NEXT_FONT_GOOGLE_MOCKED_RESPONSES: mockedGoogleFontResponses,
},
})
test('without preload', async () => {
const html = await renderViaHTTP(next.url, '/no-preload')
const $ = cheerio.load(html)
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(1)
expect($('link[rel="preconnect"]').get(0).attribs).toEqual({
crossorigin: 'anonymous',
href: '/',
rel: 'preconnect',
'data-next-font': 'size-adjust',
})
// Preload
expect($('link[as="font"]').length).toBe(0)
})
test('without fonts', async () => {
const html = await renderViaHTTP(next.url, '/without-fonts')
const $ = cheerio.load(html)
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(1)
expect($('link[rel="preconnect"]').get(0).attribs).toEqual({
crossorigin: 'anonymous',
href: '/',
rel: 'preconnect',
'data-next-font': 'size-adjust',
})
// Preload
expect($('link[as="font"]').length).toBe(0)
})
})