Skip to content

Commit

Permalink
fix: updated tests, reduced impact of tsconfig updates by limiting it…
Browse files Browse the repository at this point in the history
… to migration users
  • Loading branch information
wyattjoh committed Feb 14, 2023
1 parent 764969d commit 624f433
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export async function writeAppTypeDeclarations({

// Push the notice in.
directives.push(
'',
'// NOTE: This file should not be edited',
'// see https://nextjs.org/docs/basic-features/typescript for more information.'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ export async function writeConfigurationDefaults(
tsConfigPath: string,
isFirstTimeSetup: boolean,
isAppDirEnabled: boolean,
distDir: string
distDir: string,
hasPagesDir: boolean
): Promise<void> {
if (isFirstTimeSetup) {
await fs.writeFile(tsConfigPath, '{}' + os.EOL)
Expand Down Expand Up @@ -230,6 +231,7 @@ export async function writeConfigurationDefaults(
// If `strict` is set to `false` or `strictNullChecks` is set to `false`,
// then set `strictNullChecks` to `true`.
if (
hasPagesDir &&
isAppDirEnabled &&
!userTsConfig.compilerOptions.strict &&
!('strictNullChecks' in userTsConfig.compilerOptions)
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/lib/verifyTypeScriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export async function verifyTypeScriptSetup({
resolvedTsConfigPath,
intent.firstTimeSetup,
isAppDirEnabled,
distDir
distDir,
hasPagesDir
)
// Write out the necessary `next-env.d.ts` file to correctly register
// Next.js' types:
Expand Down
51 changes: 45 additions & 6 deletions test/unit/write-app-declarations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ 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 () => {
Expand All @@ -25,10 +28,14 @@ describe('find config', () => {
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
eol

await fs.ensureDir(fixtureDir)
await fs.writeFile(declarationFile, content)

await writeAppTypeDeclarations(fixtureDir, imageImportsEnabled)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
isAppDirEnabled: false,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})

Expand All @@ -46,10 +53,14 @@ describe('find config', () => {
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
eol

await fs.ensureDir(fixtureDir)
await fs.writeFile(declarationFile, content)

await writeAppTypeDeclarations(fixtureDir, imageImportsEnabled)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
isAppDirEnabled: false,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})

Expand All @@ -67,8 +78,36 @@ describe('find config', () => {
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
eol

await fs.ensureDir(fixtureDir)
await writeAppTypeDeclarations(fixtureDir, imageImportsEnabled)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
isAppDirEnabled: false,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})

it('should include navigation types if app directory is enabled', async () => {
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
isAppDirEnabled: true,
})

await expect(fs.readFile(declarationFile, 'utf8')).resolves.toContain(
'next/navigation-types/navigation'
)

await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: true,
isAppDirEnabled: true,
})

await expect(fs.readFile(declarationFile, 'utf8')).resolves.toContain(
'next/navigation-types/compat/navigation'
)
})
})

0 comments on commit 624f433

Please sign in to comment.