Files
vercel__next.js/test/unit/write-app-declarations.test.ts
Josh Story 079df0f6f4 Enable rootParams by default (#93863)
rootParams is now available by default. the flag is removed.

There are still intentional limitations. For instance rootParams cannot
be used in route handlers and Server Actions. The feature will be
expanded with some support for this in the future.
2026-05-19 09:45:55 -07:00

141 lines
3.9 KiB
TypeScript

/* eslint-env jest */
import os from 'os'
import fs from 'fs-extra'
import { join } from 'path'
import { writeAppTypeDeclarations } from 'next/dist/lib/typescript/writeAppTypeDeclarations'
const fixtureDir = join(__dirname, 'fixtures/app-declarations')
const declarationFile = join(fixtureDir, 'next-env.d.ts')
const imageImportsEnabled = false
describe('find config', () => {
beforeEach(async () => {
await fs.ensureDir(fixtureDir)
})
afterEach(() => fs.remove(declarationFile))
it('should preserve CRLF EOL', async () => {
const eol = '\r\n'
const content =
'/// <reference types="next" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
`import "./.next/types/routes.d.ts";` +
eol +
`import "./.next/types/root-params.d.ts";` +
eol +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.' +
eol
await fs.writeFile(declarationFile, content)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
distDir: '.next',
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: false,
strictRouteTypes: false,
typedRoutes: true,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should preserve LF EOL', async () => {
const eol = '\n'
const content =
'/// <reference types="next" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
`import "./.next/types/routes.d.ts";` +
eol +
`import "./.next/types/root-params.d.ts";` +
eol +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.' +
eol
await fs.writeFile(declarationFile, content)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
distDir: '.next',
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: false,
strictRouteTypes: false,
typedRoutes: true,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should use OS EOL by default', async () => {
const eol = os.EOL
const content =
'/// <reference types="next" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
`import "./.next/types/routes.d.ts";` +
eol +
`import "./.next/types/root-params.d.ts";` +
eol +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.' +
eol
await writeAppTypeDeclarations({
baseDir: fixtureDir,
distDir: '.next',
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: false,
strictRouteTypes: false,
typedRoutes: true,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should include navigation types if app directory is enabled', async () => {
await writeAppTypeDeclarations({
baseDir: fixtureDir,
distDir: '.next',
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: true,
strictRouteTypes: false,
typedRoutes: true,
})
await expect(fs.readFile(declarationFile, 'utf8')).resolves.not.toContain(
'next/navigation-types/compat/navigation'
)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
distDir: '.next',
imageImportsEnabled,
hasPagesDir: true,
hasAppDir: true,
strictRouteTypes: false,
typedRoutes: true,
})
await expect(fs.readFile(declarationFile, 'utf8')).resolves.toContain(
'next/navigation-types/compat/navigation'
)
})
})