|
| 1 | +/* eslint-env jest */ |
| 2 | +import os from 'os' |
| 3 | +import fs from 'fs-extra' |
| 4 | +import { join } from 'path' |
| 5 | +import { writeConfigurationDefaults } from 'next/dist/lib/typescript/writeConfigurationDefaults' |
| 6 | +import * as ts from 'typescript' |
| 7 | + |
| 8 | +const fixtureDir = join(__dirname, 'fixtures/config-ts') |
| 9 | +const tsconfigFile = join(fixtureDir, 'tsconfig.json') |
| 10 | +const tsconfigBaseFile = join(fixtureDir, 'tsconfig.base.json') |
| 11 | + |
| 12 | +describe('tsconfig.base.json', () => { |
| 13 | + beforeEach(async () => { |
| 14 | + await fs.ensureDir(fixtureDir) |
| 15 | + }) |
| 16 | + afterEach(async () => { |
| 17 | + await fs.remove(tsconfigFile) |
| 18 | + await fs.remove(tsconfigBaseFile) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should support empty includes when base provides it', async () => { |
| 22 | + const content = { |
| 23 | + extends: './tsconfig.base.json', |
| 24 | + } |
| 25 | + |
| 26 | + const baseContent = { |
| 27 | + include: ['**/*.ts', '**/*.tsx'], |
| 28 | + } |
| 29 | + |
| 30 | + await fs.writeFile(tsconfigFile, JSON.stringify(content, null, 2)) |
| 31 | + await fs.writeFile(tsconfigBaseFile, JSON.stringify(baseContent, null, 2)) |
| 32 | + |
| 33 | + await expect( |
| 34 | + writeConfigurationDefaults(ts, tsconfigFile, false, true, '.next', true) |
| 35 | + ).resolves.not.toThrow() |
| 36 | + }) |
| 37 | + |
| 38 | + it('should not add strictNullChecks if base provides it', async () => { |
| 39 | + const content = { |
| 40 | + extends: './tsconfig.base.json', |
| 41 | + compilerOptions: { |
| 42 | + plugins: [ |
| 43 | + { |
| 44 | + name: 'next', |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + const baseContent = { |
| 51 | + compilerOptions: { |
| 52 | + strictNullChecks: true, |
| 53 | + strict: true, |
| 54 | + }, |
| 55 | + include: ['next-env.d.ts', '**/*.ts', '**/*.tsx', '.next/types/**/*.ts'], |
| 56 | + exclude: ['node_modules'], |
| 57 | + } |
| 58 | + |
| 59 | + await fs.writeFile(tsconfigFile, JSON.stringify(content, null, 2)) |
| 60 | + await fs.writeFile(tsconfigBaseFile, JSON.stringify(baseContent, null, 2)) |
| 61 | + |
| 62 | + await writeConfigurationDefaults( |
| 63 | + ts, |
| 64 | + tsconfigFile, |
| 65 | + false, |
| 66 | + true, |
| 67 | + '.next', |
| 68 | + true |
| 69 | + ) |
| 70 | + const output = await fs.readFile(tsconfigFile, 'utf8') |
| 71 | + const parsed = JSON.parse(output) |
| 72 | + |
| 73 | + expect(parsed.compilerOptions.strictNullChecks).toBeUndefined() |
| 74 | + }) |
| 75 | +}) |
0 commit comments