Skip to content

Commit

Permalink
Do not warn about disabled strict mode if strict mode was enabled
Browse files Browse the repository at this point in the history
This is the case after starting the `dev` command for the first time
after having created a new project with `create-next-app`.
  • Loading branch information
unstubbable committed Apr 20, 2024
1 parent ca51c64 commit da33856
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import ts from 'typescript'
import { writeConfigurationDefaults } from './writeConfigurationDefaults'

describe('writeConfigurationDefaults()', () => {
let consoleLogSpy: jest.SpyInstance

beforeEach(() => {
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation()
})

afterEach(() => {
consoleLogSpy.mockRestore()
})

it('applies suggested and mandatory defaults to existing tsconfig.json and logs them', async () => {
const tmpDir = await mkdtemp(join(tmpdir(), 'nextjs-test-'))
const tsConfigPath = join(tmpDir, 'tsconfig.json')
const isFirstTimeSetup = false
const hasAppDir = true
const distDir = '.next'
const hasPagesDir = false
const logSpy = jest.spyOn(console, 'log')

await writeFile(tsConfigPath, JSON.stringify({ compilerOptions: {} }), {
encoding: 'utf8',
Expand Down Expand Up @@ -69,7 +78,7 @@ describe('writeConfigurationDefaults()', () => {
`)

expect(
logSpy.mock.calls
consoleLogSpy.mock.calls
.flat()
.join('\n')
// eslint-disable-next-line no-control-regex
Expand Down Expand Up @@ -120,4 +129,36 @@ describe('writeConfigurationDefaults()', () => {
"
`)
})

it('does not warn about disabled strict mode if strict mode was already enabled', async () => {
const tmpDir = await mkdtemp(join(tmpdir(), 'nextjs-test-'))
const tsConfigPath = join(tmpDir, 'tsconfig.json')
const isFirstTimeSetup = false
const hasAppDir = true
const distDir = '.next'
const hasPagesDir = false

await writeFile(
tsConfigPath,
JSON.stringify({ compilerOptions: { strict: true } }),
{ encoding: 'utf8' }
)

await writeConfigurationDefaults(
ts,
tsConfigPath,
isFirstTimeSetup,
hasAppDir,
distDir,
hasPagesDir
)

expect(
consoleLogSpy.mock.calls
.flat()
.join('\n')
// eslint-disable-next-line no-control-regex
.replace(/\x1B\[\d+m/g, '') // remove color control characters
).not.toMatch('Strict-mode is set to false by default.')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,13 @@ export async function writeConfigurationDefaults(
Log.info(
`We detected TypeScript in your project and reconfigured your ${cyan(
'tsconfig.json'
)} file for you. Strict-mode is set to ${cyan('false')} by default.`
)} file for you.${
userTsConfig.compilerOptions?.strict
? ''
: ` Strict-mode is set to ${cyan('false')} by default.`
}`
)

if (suggestedActions.length) {
Log.info(
`The following suggested values were added to your ${cyan(
Expand Down

0 comments on commit da33856

Please sign in to comment.