Skip to content

Commit 2f6421c

Browse files
committed
Do not warn about disabled strict mode if strict mode was enabled
This is the case after starting the `dev` command for the first time, after having created a new project with `create-next-app`.
1 parent ebde21b commit 2f6421c

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

packages/next/src/lib/typescript/writeConfigurationDefaults.test.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ import ts from 'typescript'
66
import { writeConfigurationDefaults } from './writeConfigurationDefaults'
77

88
describe('writeConfigurationDefaults()', () => {
9+
let consoleLogSpy: jest.SpyInstance
10+
11+
beforeEach(() => {
12+
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation()
13+
})
14+
15+
afterEach(() => {
16+
consoleLogSpy.mockRestore()
17+
})
18+
919
it('applies suggested and mandatory defaults to existing tsconfig.json and logs them', async () => {
1020
const tmpDir = await mkdtemp(join(tmpdir(), 'nextjs-test-'))
1121
const tsConfigPath = join(tmpDir, 'tsconfig.json')
1222
const isFirstTimeSetup = false
1323
const hasAppDir = true
1424
const distDir = '.next'
1525
const hasPagesDir = false
16-
const logSpy = jest.spyOn(console, 'log')
1726

1827
await writeFile(tsConfigPath, JSON.stringify({ compilerOptions: {} }), {
1928
encoding: 'utf8',
@@ -69,7 +78,7 @@ describe('writeConfigurationDefaults()', () => {
6978
`)
7079

7180
expect(
72-
logSpy.mock.calls
81+
consoleLogSpy.mock.calls
7382
.flat()
7483
.join('\n')
7584
// eslint-disable-next-line no-control-regex
@@ -120,4 +129,36 @@ describe('writeConfigurationDefaults()', () => {
120129
"
121130
`)
122131
})
132+
133+
it('does not warn about disabled strict mode if strict mode was already enabled', async () => {
134+
const tmpDir = await mkdtemp(join(tmpdir(), 'nextjs-test-'))
135+
const tsConfigPath = join(tmpDir, 'tsconfig.json')
136+
const isFirstTimeSetup = false
137+
const hasAppDir = true
138+
const distDir = '.next'
139+
const hasPagesDir = false
140+
141+
await writeFile(
142+
tsConfigPath,
143+
JSON.stringify({ compilerOptions: { strict: true } }),
144+
{ encoding: 'utf8' }
145+
)
146+
147+
await writeConfigurationDefaults(
148+
ts,
149+
tsConfigPath,
150+
isFirstTimeSetup,
151+
hasAppDir,
152+
distDir,
153+
hasPagesDir
154+
)
155+
156+
expect(
157+
consoleLogSpy.mock.calls
158+
.flat()
159+
.join('\n')
160+
// eslint-disable-next-line no-control-regex
161+
.replace(/\x1B\[\d+m/g, '') // remove color control characters
162+
).not.toMatch('Strict-mode is set to false by default.')
163+
})
123164
})

packages/next/src/lib/typescript/writeConfigurationDefaults.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,13 @@ export async function writeConfigurationDefaults(
313313
Log.info(
314314
`We detected TypeScript in your project and reconfigured your ${cyan(
315315
'tsconfig.json'
316-
)} file for you. Strict-mode is set to ${cyan('false')} by default.`
316+
)} file for you.${
317+
userTsConfig.compilerOptions?.strict
318+
? ''
319+
: ` Strict-mode is set to ${cyan('false')} by default.`
320+
}`
317321
)
322+
318323
if (suggestedActions.length) {
319324
Log.info(
320325
`The following suggested values were added to your ${cyan(

0 commit comments

Comments
 (0)