Unverified Commit c7005486 authored by Steven's avatar Steven Committed by GitHub

chore: refactor image optimization tests (#46269)

This is a small refactor to image optimization tests. Previously, the
`ctx` object was used for input options as well as next.config.js
options but this PR separates the latter into `nextConfigImages`.
parent 19240383
...@@ -11,7 +11,7 @@ import { ...@@ -11,7 +11,7 @@ import {
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import { join } from 'path' import { join } from 'path'
import { cleanImagesDir, expectWidth, fsToJson, runTests } from './util' import { cleanImagesDir, expectWidth, fsToJson } from './util'
const appDir = join(__dirname, '../app') const appDir = join(__dirname, '../app')
const imagesDir = join(appDir, '.next', 'cache', 'images') const imagesDir = join(appDir, '.next', 'cache', 'images')
...@@ -375,57 +375,6 @@ describe('Image Optimizer', () => { ...@@ -375,57 +375,6 @@ describe('Image Optimizer', () => {
}) })
}) })
// domains for testing
const domains = [
'localhost',
'example.com',
'assets.vercel.com',
'image-optimization-test.vercel.app',
]
// Reduce to 5 seconds so tests dont dont need to
// wait too long before testing stale responses.
const minimumCacheTTL = 5
describe('Server support for minimumCacheTTL in next.config.js', () => {
const size = 96 // defaults defined in server/config.ts
const dangerouslyAllowSVG = true
const ctx: any = {
w: size,
isDev: false,
domains,
minimumCacheTTL,
dangerouslyAllowSVG,
imagesDir,
appDir,
}
beforeAll(async () => {
const json = JSON.stringify({
images: {
domains,
minimumCacheTTL,
dangerouslyAllowSVG,
},
})
ctx.nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json)
await nextBuild(appDir)
await cleanImagesDir({ imagesDir })
ctx.appPort = await findPort()
ctx.app = await nextStart(appDir, ctx.appPort, {
onStderr(msg) {
ctx.nextOutput += msg
},
})
})
afterAll(async () => {
await killApp(ctx.app)
nextConfig.restore()
})
runTests(ctx)
})
describe('Server support for trailingSlash in next.config.js', () => { describe('Server support for trailingSlash in next.config.js', () => {
let app let app
let appPort let appPort
......
import { join } from 'path'
import { setupTests } from './util'
const appDir = join(__dirname, '../app')
const imagesDir = join(appDir, '.next', 'cache', 'images')
describe('with minimumCacheTTL of 5 sec', () => {
setupTests({
nextConfigImages: {
// Configure external domains so we can try out
// variations of the upstream Cache-Control header.
domains: [
'localhost',
'example.com',
'assets.vercel.com',
'image-optimization-test.vercel.app',
],
// Reduce to 5 seconds so tests dont dont need to
// wait too long before testing stale responses.
minimumCacheTTL: 5,
},
appDir,
imagesDir,
})
})
...@@ -131,8 +131,14 @@ async function fetchWithDuration( ...@@ -131,8 +131,14 @@ async function fetchWithDuration(
} }
export function runTests(ctx) { export function runTests(ctx) {
const { isDev, minimumCacheTTL = 60, nextConfigImages } = ctx const { isDev, nextConfigImages } = ctx
const { contentDispositionType = 'inline' } = nextConfigImages || {} const {
contentDispositionType = 'inline',
domains = [],
formats = [],
minimumCacheTTL = 60,
} = nextConfigImages || {}
const avifEnabled = formats[0] === 'image/avif'
let slowImageServer: Awaited<ReturnType<typeof serveSlowImage>> let slowImageServer: Awaited<ReturnType<typeof serveSlowImage>>
beforeAll(async () => { beforeAll(async () => {
slowImageServer = await serveSlowImage() slowImageServer = await serveSlowImage()
...@@ -141,7 +147,7 @@ export function runTests(ctx) { ...@@ -141,7 +147,7 @@ export function runTests(ctx) {
slowImageServer.stop() slowImageServer.stop()
}) })
if (ctx.domains?.length > 0) { if (domains.length > 0) {
it('should normalize invalid status codes', async () => { it('should normalize invalid status codes', async () => {
const url = `http://localhost:${ const url = `http://localhost:${
slowImageServer.port slowImageServer.port
...@@ -625,7 +631,7 @@ export function runTests(ctx) { ...@@ -625,7 +631,7 @@ export function runTests(ctx) {
await expectWidth(res, ctx.w) await expectWidth(res, ctx.w)
}) })
if (ctx.avifEnabled) { if (avifEnabled) {
it('should resize relative url and new Chrome accept header as avif', async () => { it('should resize relative url and new Chrome accept header as avif', async () => {
const query = { url: '/test.png', w: ctx.w, q: 80 } const query = { url: '/test.png', w: ctx.w, q: 80 }
const opts = { const opts = {
...@@ -662,7 +668,7 @@ export function runTests(ctx) { ...@@ -662,7 +668,7 @@ export function runTests(ctx) {
}) })
} }
if (ctx.domains?.length > 0) { if (domains.length > 0) {
it('should resize absolute url from localhost', async () => { it('should resize absolute url from localhost', async () => {
const url = `http://localhost:${ctx.appPort}/test.png` const url = `http://localhost:${ctx.appPort}/test.png`
const query = { url, w: ctx.w, q: 80 } const query = { url, w: ctx.w, q: 80 }
...@@ -841,7 +847,7 @@ export function runTests(ctx) { ...@@ -841,7 +847,7 @@ export function runTests(ctx) {
) )
}) })
if (ctx.domains?.length > 0) { if (domains.length > 0) {
it('should fail when url fails to load an image', async () => { it('should fail when url fails to load an image', async () => {
const url = `http://localhost:${ctx.appPort}/not-an-image` const url = `http://localhost:${ctx.appPort}/not-an-image`
const query = { w: ctx.w, url, q: 100 } const query = { w: ctx.w, url, q: 100 }
...@@ -1198,7 +1204,7 @@ export function runTests(ctx) { ...@@ -1198,7 +1204,7 @@ export function runTests(ctx) {
expect(await res.text()).toBe("The requested resource isn't a valid image.") expect(await res.text()).toBe("The requested resource isn't a valid image.")
}) })
if (ctx.domains?.length > 0) { if (domains.length > 0) {
it('should handle concurrent requests', async () => { it('should handle concurrent requests', async () => {
await cleanImagesDir(ctx) await cleanImagesDir(ctx)
const delay = 500 const delay = 500
...@@ -1265,7 +1271,7 @@ export function runTests(ctx) { ...@@ -1265,7 +1271,7 @@ export function runTests(ctx) {
}) })
} }
if (ctx.isSharp && ctx.isOutdatedSharp && ctx.avifEnabled) { if (ctx.isSharp && ctx.isOutdatedSharp && avifEnabled) {
it('should have sharp outdated warning', () => { it('should have sharp outdated warning', () => {
expect(ctx.nextOutput).toContain(sharpOutdatedText) expect(ctx.nextOutput).toContain(sharpOutdatedText)
}) })
...@@ -1279,15 +1285,6 @@ export function runTests(ctx) { ...@@ -1279,15 +1285,6 @@ export function runTests(ctx) {
export const setupTests = (ctx) => { export const setupTests = (ctx) => {
const nextConfig = new File(join(ctx.appDir, 'next.config.js')) const nextConfig = new File(join(ctx.appDir, 'next.config.js'))
if (!ctx.domains) {
ctx.domains = [
'localhost',
'example.com',
'assets.vercel.com',
'image-optimization-test.vercel.app',
]
}
// only run one server config with outdated sharp // only run one server config with outdated sharp
if (!ctx.isOutdatedSharp) { if (!ctx.isOutdatedSharp) {
describe('dev support w/o next.config.js', () => { describe('dev support w/o next.config.js', () => {
...@@ -1300,8 +1297,6 @@ export const setupTests = (ctx) => { ...@@ -1300,8 +1297,6 @@ export const setupTests = (ctx) => {
...ctx, ...ctx,
w: size, w: size,
isDev: true, isDev: true,
domains: [],
avifEnabled: false,
} }
beforeAll(async () => { beforeAll(async () => {
...@@ -1333,17 +1328,22 @@ export const setupTests = (ctx) => { ...@@ -1333,17 +1328,22 @@ export const setupTests = (ctx) => {
...ctx, ...ctx,
w: size, w: size,
isDev: true, isDev: true,
avifEnabled: true, nextConfigImages: {
} domains: [
beforeAll(async () => { 'localhost',
const json = JSON.stringify({ 'example.com',
images: { 'assets.vercel.com',
'image-optimization-test.vercel.app',
],
formats: ['image/avif', 'image/webp'],
deviceSizes: [largeSize], deviceSizes: [largeSize],
imageSizes: [size], imageSizes: [size],
domains: curCtx.domains,
formats: ['image/avif', 'image/webp'],
...ctx.nextConfigImages, ...ctx.nextConfigImages,
}, },
}
beforeAll(async () => {
const json = JSON.stringify({
images: curCtx.nextConfigImages,
}) })
curCtx.nextOutput = '' curCtx.nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json) nextConfig.replace('{ /* replaceme */ }', json)
...@@ -1379,7 +1379,6 @@ export const setupTests = (ctx) => { ...@@ -1379,7 +1379,6 @@ export const setupTests = (ctx) => {
...ctx, ...ctx,
w: size, w: size,
isDev: false, isDev: false,
domains: [],
} }
beforeAll(async () => { beforeAll(async () => {
curCtx.nextOutput = '' curCtx.nextOutput = ''
...@@ -1412,16 +1411,21 @@ export const setupTests = (ctx) => { ...@@ -1412,16 +1411,21 @@ export const setupTests = (ctx) => {
...ctx, ...ctx,
w: size, w: size,
isDev: false, isDev: false,
avifEnabled: true, nextConfigImages: {
} domains: [
beforeAll(async () => { 'localhost',
const json = JSON.stringify({ 'example.com',
images: { 'assets.vercel.com',
'image-optimization-test.vercel.app',
],
formats: ['image/avif', 'image/webp'], formats: ['image/avif', 'image/webp'],
deviceSizes: [size, largeSize], deviceSizes: [size, largeSize],
domains: ctx.domains,
...ctx.nextConfigImages, ...ctx.nextConfigImages,
}, },
}
beforeAll(async () => {
const json = JSON.stringify({
images: curCtx.nextConfigImages,
}) })
curCtx.nextOutput = '' curCtx.nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json) nextConfig.replace('{ /* replaceme */ }', json)
......
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