Unverified Commit cc0cdc8b authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

test: remove some more dev/prod gating (#100717)

**View diff without whitespace**

Removes unnecessary dev/production restrictions to expand test coverage,
consolidates duplicate suites, and replaces remaining conditional skips
with explicit gate directives.
parent 5988244c
...@@ -13,14 +13,13 @@ describe.each([ ...@@ -13,14 +13,13 @@ describe.each([
}, },
}, },
])('unused scss', ({ dependencies, nextConfig }) => { ])('unused scss', ({ dependencies, nextConfig }) => {
describe('Body is not hidden when unused in Development ($dependencies)', () => { describe('Body is not hidden when unused ($dependencies)', () => {
const { next, isNextDev } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
dependencies, dependencies,
nextConfig, nextConfig,
}) })
;(isNextDev ? describe : describe.skip)('development only', () => {
it('should have body visible', async () => { it('should have body visible', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
const currentDisplay = await browser.eval( const currentDisplay = await browser.eval(
...@@ -29,16 +28,16 @@ describe.each([ ...@@ -29,16 +28,16 @@ describe.each([
expect(currentDisplay).toBe('block') expect(currentDisplay).toBe('block')
}) })
}) })
})
// This test introduces a syntax error after startup and requires HMR.
// @force-gate dev
describe('Body is not hidden when broken in Development', () => { describe('Body is not hidden when broken in Development', () => {
const { next, isNextDev } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
dependencies, dependencies,
nextConfig, nextConfig,
}) })
;(isNextDev ? describe : describe.skip)('development only', () => {
it('should have body visible', async () => { it('should have body visible', async () => {
await next.patchFile('pages/index.js', (contents) => { await next.patchFile('pages/index.js', (contents) => {
return contents.replace('<div />', '<div>') return contents.replace('<div />', '<div>')
...@@ -51,5 +50,4 @@ describe.each([ ...@@ -51,5 +50,4 @@ describe.each([
expect(currentDisplay).toBe('block') expect(currentDisplay).toBe('block')
}) })
}) })
})
}) })
import { join } from 'path' import { join } from 'path'
import { nextTestSetup, isNextDev, isNextStart } from 'e2e-utils' import { nextTestSetup, isNextStart } from 'e2e-utils'
import { findPort, initNextServerScript, killApp } from 'next-test-utils' import { findPort, initNextServerScript, killApp } from 'next-test-utils'
// Deploy mode exclusion: This suite starts an HTTP server on a local port and
// patches that port into next.config.js, which a deployment can't reach.
// @force-gate !deploy
describe('Fetch polyfill with ky-universal', () => { describe('Fetch polyfill with ky-universal', () => {
;(isNextDev ? describe : describe.skip)('development mode', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
dependencies: { 'ky-universal': '0.6.0', ky: '0.19.1' },
})
let apiServerPort: number
let apiServer: any
beforeAll(async () => {
const scriptPath = join(__dirname, 'api-server.js')
apiServerPort = await findPort()
apiServer = await initNextServerScript(
scriptPath,
/ready on/i,
{ ...process.env, PORT: String(apiServerPort) },
/ReferenceError: options is not defined/
)
next.env.NEXT_PUBLIC_API_PORT = String(apiServerPort)
await next.start()
})
afterAll(async () => {
await killApp(apiServer)
})
it('includes polyfilled fetch when using getStaticProps (dev)', async () => {
const html = await next.render('/static')
expect(html).toMatch(/bar/)
})
it('includes polyfilled fetch when using getServerSideProps (dev)', async () => {
const html = await next.render('/ssr')
expect(html).toMatch(/bar/)
})
it('includes polyfilled fetch when using getInitialProps (dev)', async () => {
const html = await next.render('/getinitialprops')
expect(html).toMatch(/bar/)
})
})
;(isNextStart ? describe : describe.skip)('production mode', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
skipStart: true, skipStart: true,
...@@ -67,7 +26,9 @@ describe('Fetch polyfill with ky-universal', () => { ...@@ -67,7 +26,9 @@ describe('Fetch polyfill with ky-universal', () => {
) )
next.env.NEXT_PUBLIC_API_PORT = String(apiServerPort) next.env.NEXT_PUBLIC_API_PORT = String(apiServerPort)
if (isNextStart) {
await next.build() await next.build()
}
await next.start() await next.start()
}) })
...@@ -75,19 +36,18 @@ describe('Fetch polyfill with ky-universal', () => { ...@@ -75,19 +36,18 @@ describe('Fetch polyfill with ky-universal', () => {
await killApp(apiServer) await killApp(apiServer)
}) })
it('includes polyfilled fetch when using getStaticProps (prod)', async () => { it('includes polyfilled fetch when using getStaticProps', async () => {
const html = await next.render('/static') const html = await next.render('/static')
expect(html).toMatch(/bar/) expect(html).toMatch(/bar/)
}) })
it('includes polyfilled fetch when using getServerSideProps (prod)', async () => { it('includes polyfilled fetch when using getServerSideProps', async () => {
const html = await next.render('/ssr') const html = await next.render('/ssr')
expect(html).toMatch(/bar/) expect(html).toMatch(/bar/)
}) })
it('includes polyfilled fetch when using getInitialProps (prod)', async () => { it('includes polyfilled fetch when using getInitialProps', async () => {
const html = await next.render('/getinitialprops') const html = await next.render('/getinitialprops')
expect(html).toMatch(/bar/) expect(html).toMatch(/bar/)
}) })
})
}) })
...@@ -60,15 +60,12 @@ describe('i18n Support basePath', () => { ...@@ -60,15 +60,12 @@ describe('i18n Support basePath', () => {
afterAll(() => { afterAll(() => {
externalServer?.close() externalServer?.close()
}) })
;(isNextDev ? describe : describe.skip)('development mode', () => {
runTests(ctx)
})
;(!isNextDev ? describe : describe.skip)('production mode', () => {
runTests(ctx) runTests(ctx)
})
// These checks rebuild the fixture and inspect the production routes manifest.
// @force-gate !dev
describe('with localeDetection disabled', () => { describe('with localeDetection disabled', () => {
if (!isNextDev) {
beforeAll(async () => { beforeAll(async () => {
await next.stop() await next.stop()
await next.patchFile('next.config.js', (content) => await next.patchFile('next.config.js', (content) =>
...@@ -161,6 +158,5 @@ describe('i18n Support basePath', () => { ...@@ -161,6 +158,5 @@ describe('i18n Support basePath', () => {
expect($('#router-as-path').text()).toBe('/') expect($('#router-as-path').text()).toBe('/')
} }
}) })
}
}) })
}) })
import { join } from 'path' import { join } from 'path'
import { nextTestSetup, isNextDev, isNextStart } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils' import { check } from 'next-test-utils'
import { cleanImagesDir, expectWidth, fsToJson } from './util' import { cleanImagesDir, expectWidth, fsToJson } from './util'
...@@ -281,19 +281,12 @@ describe('Image Optimizer', () => { ...@@ -281,19 +281,12 @@ describe('Image Optimizer', () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
}) })
}) })
;(isNextStart ? describe : describe.skip)(
'Server support for headers in next.config.js', describe('Server support for headers in next.config.js', () => {
() => {
const size = 96 const size = 96
const { next, skipped } = nextTestSetup({ const { next, skipped, isNextDev } = nextTestSetup({
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
}) nextConfig: {
if (skipped) return
beforeAll(async () => {
await next.patchFile(
'next.config.js',
`module.exports = {
async headers() { async headers() {
return [ return [
{ {
...@@ -307,41 +300,30 @@ describe('Image Optimizer', () => { ...@@ -307,41 +300,30 @@ describe('Image Optimizer', () => {
}, },
] ]
}, },
}` },
)
})
afterAll(async () => {
await next.patchFile(
'next.config.js',
'// prettier-ignore\nmodule.exports = { /* replaceme */ }'
)
}) })
if (skipped) return
it('should set max-age header', async () => { it('should set max-age header', async () => {
const query = { url: '/test.png', w: size, q: 75 } const query = { url: '/test.png', w: size, q: 75 }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await next.fetch( const res = await next.fetch(`/_next/image?${toQueryString(query)}`, opts)
`/_next/image?${toQueryString(query)}`,
opts
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
'public, max-age=14400, must-revalidate' `public, max-age=${isNextDev ? 0 : 14400}, must-revalidate`
) )
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
'attachment; filename="test.webp"' 'attachment; filename="test.webp"'
) )
const imagesDir = join(next.testDir, '.next', 'cache', 'images') const imagesDir = join(next.testDir, next.distDir, 'cache', 'images')
await check(async () => { await check(async () => {
const files = await fsToJson(imagesDir) const files = await fsToJson(imagesDir)
let found = false let found = false
const maxAge = '14400' const maxAge = '14400'
Object.keys(files).forEach((dir) => { Object.keys(files).forEach((dir) => {
if ( if (
Object.keys(files[dir]).some((file) => Object.keys(files[dir]).some((file) => file.includes(`${maxAge}.`))
file.includes(`${maxAge}.`)
)
) { ) {
found = true found = true
} }
...@@ -353,23 +335,18 @@ describe('Image Optimizer', () => { ...@@ -353,23 +335,18 @@ describe('Image Optimizer', () => {
it('should not set max-age header when not matching next.config.js', async () => { it('should not set max-age header when not matching next.config.js', async () => {
const query = { url: '/test.jpg', w: size, q: 75 } const query = { url: '/test.jpg', w: size, q: 75 }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await next.fetch( const res = await next.fetch(`/_next/image?${toQueryString(query)}`, opts)
`/_next/image?${toQueryString(query)}`,
opts
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
'public, max-age=14400, must-revalidate' `public, max-age=${isNextDev ? 0 : 14400}, must-revalidate`
) )
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
'attachment; filename="test.webp"' 'attachment; filename="test.webp"'
) )
}) })
} })
)
;(isNextDev ? describe : describe.skip)( describe('next.config.js cloudinary loader', () => {
'dev support next.config.js cloudinary loader',
() => {
const { next, skipped } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
nextConfig: { nextConfig: {
...@@ -385,17 +362,11 @@ describe('Image Optimizer', () => { ...@@ -385,17 +362,11 @@ describe('Image Optimizer', () => {
const size = 384 const size = 384
const query = { w: size, q: 90, url: '/test.svg' } const query = { w: size, q: 90, url: '/test.svg' }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await next.fetch( const res = await next.fetch(`/_next/image?${toQueryString(query)}`, opts)
`/_next/image?${toQueryString(query)}`,
opts
)
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
} })
) describe('images.unoptimized in next.config.js', () => {
;(isNextDev ? describe : describe.skip)(
'images.unoptimized in next.config.js',
() => {
const { next, skipped } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
nextConfig: { nextConfig: {
...@@ -408,17 +379,11 @@ describe('Image Optimizer', () => { ...@@ -408,17 +379,11 @@ describe('Image Optimizer', () => {
const size = 384 const size = 384
const query = { w: size, q: 75, url: '/test.jpg' } const query = { w: size, q: 75, url: '/test.jpg' }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await next.fetch( const res = await next.fetch(`/_next/image?${toQueryString(query)}`, opts)
`/_next/image?${toQueryString(query)}`,
opts
)
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
} })
) describe('experimental.imgOptMaxInputPixels in next.config.js', () => {
;(isNextDev ? describe : describe.skip)(
'experimental.imgOptMaxInputPixels in next.config.js',
() => {
const { next, skipped } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
nextConfig: { nextConfig: {
...@@ -431,19 +396,14 @@ describe('Image Optimizer', () => { ...@@ -431,19 +396,14 @@ describe('Image Optimizer', () => {
const size = 256 const size = 256
const query = { w: size, q: 75, url: '/test.jpg' } const query = { w: size, q: 75, url: '/test.jpg' }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await next.fetch( const res = await next.fetch(`/_next/image?${toQueryString(query)}`, opts)
`/_next/image?${toQueryString(query)}`,
opts
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('image/jpeg') expect(res.headers.get('Content-Type')).toBe('image/jpeg')
}) })
} })
)
;(isNextStart ? describe : describe.skip)( describe('External rewrite support with for serving static content in images', () => {
'External rewrite support with for serving static content in images', const { next, skipped, isNextDev } = nextTestSetup({
() => {
const { next, skipped } = nextTestSetup({
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
nextConfig: { nextConfig: {
async rewrites() { async rewrites() {
...@@ -460,19 +420,16 @@ describe('Image Optimizer', () => { ...@@ -460,19 +420,16 @@ describe('Image Optimizer', () => {
if (skipped) return if (skipped) return
it('should return response when image is served from an external rewrite', async () => { it('should return response when image is served from an external rewrite', async () => {
const imagesDir = join(next.testDir, '.next', 'cache', 'images') const imagesDir = join(next.testDir, next.distDir, 'cache', 'images')
await cleanImagesDir(imagesDir) await cleanImagesDir(imagesDir)
const query = { url: '/next-js/next-js-bg.png', w: 64, q: 75 } const query = { url: '/next-js/next-js-bg.png', w: 64, q: 75 }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await next.fetch( const res = await next.fetch(`/_next/image?${toQueryString(query)}`, opts)
`/_next/image?${toQueryString(query)}`,
opts
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('image/webp') expect(res.headers.get('Content-Type')).toBe('image/webp')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
'public, max-age=31536000, must-revalidate' `public, max-age=${isNextDev ? 0 : 31536000}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept') expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
...@@ -485,9 +442,7 @@ describe('Image Optimizer', () => { ...@@ -485,9 +442,7 @@ describe('Image Optimizer', () => {
const maxAge = '31536000' const maxAge = '31536000'
Object.keys(files).forEach((dir) => { Object.keys(files).forEach((dir) => {
if ( if (
Object.keys(files[dir]).some((file) => Object.keys(files[dir]).some((file) => file.includes(`${maxAge}.`))
file.includes(`${maxAge}.`)
)
) { ) {
found = true found = true
} }
...@@ -496,11 +451,11 @@ describe('Image Optimizer', () => { ...@@ -496,11 +451,11 @@ describe('Image Optimizer', () => {
}, 'success') }, 'success')
await expectWidth(res, 64) await expectWidth(res, 64)
}) })
} })
)
;(isNextDev ? describe : describe.skip)( // Only development accepts the implicit blur-placeholder width and quality.
'dev support for dynamic blur placeholder', // @gate dev
() => { describe('dev support for dynamic blur placeholder', () => {
const { next, skipped } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
nextConfig: { nextConfig: {
...@@ -515,13 +470,9 @@ describe('Image Optimizer', () => { ...@@ -515,13 +470,9 @@ describe('Image Optimizer', () => {
it('should support width 8 per BLUR_IMG_SIZE with next dev', async () => { it('should support width 8 per BLUR_IMG_SIZE with next dev', async () => {
const query = { url: '/test.png', w: 8, q: 70 } const query = { url: '/test.png', w: 8, q: 70 }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await next.fetch( const res = await next.fetch(`/_next/image?${toQueryString(query)}`, opts)
`/_next/image?${toQueryString(query)}`,
opts
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
await expectWidth(res, 320) await expectWidth(res, 320)
}) })
} })
)
}) })
import { nextTestSetup, isNextDev, isNextStart } from 'e2e-utils' import { nextTestSetup, isNextDev } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
// TODO(deploy-test-completion): Re-enable this suite in deploy mode. // TODO(deploy-test-completion): Re-enable this suite in deploy mode.
...@@ -32,9 +32,8 @@ describe('Errors on invalid custom middleware matchers', () => { ...@@ -32,9 +32,8 @@ describe('Errors on invalid custom middleware matchers', () => {
) )
} }
/** Same branching as integration `runTests(getStderr, isDev)`. */ function assertInvalidMatchersStderr(stderr: string) {
function assertInvalidMatchersStderr(stderr: string, isDev: boolean) { if (isTurbopack && !isNextDev) {
if (isTurbopack && !isDev) {
expect(stderr).toContain('Turbopack build failed with 10 errors') expect(stderr).toContain('Turbopack build failed with 10 errors')
let matches = 0 let matches = 0
...@@ -109,12 +108,9 @@ describe('Errors on invalid custom middleware matchers', () => { ...@@ -109,12 +108,9 @@ describe('Errors on invalid custom middleware matchers', () => {
} }
} }
function runTests(mode: 'dev' | 'start') {
const isDevMode = mode === 'dev'
it('should error when source length is exceeded', async () => { it('should error when source length is exceeded', async () => {
await writeMiddleware([{ source: `/${Array(4096).join('a')}` }]) await writeMiddleware([{ source: `/${Array(4096).join('a')}` }])
if (isDevMode) { if (isNextDev) {
await next.start() await next.start()
try { try {
await next.fetch('/').catch(() => {}) await next.fetch('/').catch(() => {})
...@@ -184,27 +180,19 @@ describe('Errors on invalid custom middleware matchers', () => { ...@@ -184,27 +180,19 @@ describe('Errors on invalid custom middleware matchers', () => {
}, },
]) ])
if (isDevMode) { if (isNextDev) {
await next.start() await next.start()
try { try {
await next.fetch('/').catch(() => {}) await next.fetch('/').catch(() => {})
await retry(async () => { await retry(async () => {
assertInvalidMatchersStderr(next.cliOutput, isDevMode) assertInvalidMatchersStderr(next.cliOutput)
}) })
} finally { } finally {
await next.stop() await next.stop()
} }
} else { } else {
await next.build() await next.build()
assertInvalidMatchersStderr(next.cliOutput, isDevMode) assertInvalidMatchersStderr(next.cliOutput)
}
})
} }
;(isNextDev ? describe : describe.skip)('development mode', () => {
runTests('dev')
})
;(isNextStart ? describe : describe.skip)('production mode', () => {
runTests('start')
}) })
}) })
import { nextTestSetup, isNextDev, isNextStart } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { import {
getClientBuildManifestLoaderChunkUrlPath, getClientBuildManifestLoaderChunkUrlPath,
retry, retry,
...@@ -45,17 +45,13 @@ describe('Link ref forwarding', () => { ...@@ -45,17 +45,13 @@ describe('Link ref forwarding', () => {
await browser.close() await browser.close()
} }
function runCommonTests() {
it('should not have a race condition with a click handler', async () => { it('should not have a race condition with a click handler', async () => {
const browser = await next.browser('/click-away-race-condition') const browser = await next.browser('/click-away-race-condition')
await browser.elementByCss('#click-me').click() await browser.elementByCss('#click-me').click()
await browser.waitForElementByCss('#the-menu') await browser.waitForElementByCss('#the-menu')
}) })
}
;(isNextDev ? describe : describe.skip)('development mode', () => {
runCommonTests()
describe('ref forwarding', () => {
it('should not show error for function component with forwardRef', async () => { it('should not show error for function component with forwardRef', async () => {
await noError('/function') await noError('/function')
}) })
...@@ -76,9 +72,10 @@ describe('Link ref forwarding', () => { ...@@ -76,9 +72,10 @@ describe('Link ref forwarding', () => {
await noError('/child-ref-func-cleanup') await noError('/child-ref-func-cleanup')
}) })
}) })
;(isNextStart ? describe : describe.skip)('production mode', () => {
runCommonTests()
// Prefetching is disabled in dev, and these checks read the local build manifest.
// @force-gate start
describe('production mode', () => {
it('should preload with forwardRef', async () => { it('should preload with forwardRef', async () => {
await didPrefetch('/function') await didPrefetch('/function')
}) })
......
...@@ -4,13 +4,11 @@ import { ...@@ -4,13 +4,11 @@ import {
testShouldRedirect, testShouldRedirect,
testLinkShouldRewriteTo, testLinkShouldRewriteTo,
} from './shared-tests.util' } from './shared-tests.util'
import { nextTestSetup, isNextDev } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
// we don't need to exhaustively test this, just do some basic sanity checks // we don't need to exhaustively test this, just do some basic sanity checks
// for use in combiantion with basePath // for use in combination with basePath
;(isNextDev ? describe : describe.skip)( describe('Trailing slashes with basePath, trailingSlash: true', () => {
'Trailing slashes in development mode, with basepath, trailingSlash: true',
() => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
nextConfig: { nextConfig: {
...@@ -30,5 +28,4 @@ import { nextTestSetup, isNextDev } from 'e2e-utils' ...@@ -30,5 +28,4 @@ import { nextTestSetup, isNextDev } from 'e2e-utils'
['/docs/linker?href=/about', '/docs/about/'], ['/docs/linker?href=/about', '/docs/about/'],
['/docs/linker?href=/', '/docs/'], ['/docs/linker?href=/', '/docs/'],
]) ])
} })
)
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