Skip to content

Commit 0c555c2

Browse files
authored
feat(config): introduce exclude to exclude files from diagnostics (#2308)
DEPRECATION `pathRegex` is deprecated in favor of `exclude`
1 parent 5ebab21 commit 0c555c2

File tree

11 files changed

+211
-105
lines changed

11 files changed

+211
-105
lines changed

e2e/__cases__/ts-jest-checks/index.spec.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

e2e/__tests__/ts-jest-checks.test.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/__helpers__/fakers.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolve } from 'path'
44

55
import { createCompilerInstance } from '../compiler/instance'
66
import { ConfigSet } from '../config/config-set'
7-
import type { BabelConfig, TsCompiler, TsJestConfig, TsJestGlobalOptions } from '../types'
7+
import type { BabelConfig, TsCompiler, TsJestGlobalOptions } from '../types'
88
import type { ImportReasons } from '../utils/messages'
99

1010
export function filePath(relPath: string): string {
@@ -13,18 +13,8 @@ export function filePath(relPath: string): string {
1313

1414
export const rootDir = filePath('')
1515

16-
export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
17-
return {
18-
isolatedModules: false,
19-
compiler: 'typescript',
20-
transformers: options?.transformers ?? Object.create(null),
21-
babelConfig: undefined,
22-
tsConfig: undefined,
23-
stringifyContentPathRegex: undefined,
24-
diagnostics: { ignoreCodes: [], pretty: false, throws: true },
25-
...options,
26-
}
27-
}
16+
const defaultTestRegex = ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$']
17+
const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']
2818

2919
function getJestConfig<T extends Config.ProjectConfig>(
3020
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
@@ -68,7 +58,15 @@ export function createConfigSet({
6858
resolve?: ((path: string) => string) | null
6959
[key: string]: any
7060
} = {}): ConfigSet {
71-
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), logger)
61+
const jestCfg = getJestConfig(jestConfig, tsJestConfig)
62+
const cs = new ConfigSet(
63+
{
64+
...jestCfg,
65+
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...defaultTestMatch] : defaultTestMatch,
66+
testRegex: jestConfig?.testRegex ? [...jestConfig.testRegex, ...defaultTestRegex] : defaultTestRegex,
67+
},
68+
logger,
69+
)
7270
if (resolve) {
7371
cs.resolvePath = resolve
7472
}
@@ -94,12 +92,10 @@ export function makeCompiler({
9492
...(tsJestConfig.diagnostics as any),
9593
pretty: false,
9694
}
97-
const defaultTestRegex = ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$']
98-
const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']
9995
jestConfig = {
10096
...jestConfig,
10197
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...defaultTestMatch] : defaultTestMatch,
102-
testRegex: jestConfig?.testRegex ? [...defaultTestRegex, ...jestConfig.testRegex] : defaultTestRegex,
98+
testRegex: jestConfig?.testRegex ? [...jestConfig.testRegex, ...defaultTestRegex] : defaultTestRegex,
10399
}
104100
const cs = createConfigSet({ jestConfig, tsJestConfig, parentConfig, resolve: null })
105101

src/compiler/__snapshots__/transpiler.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Transpiler diagnostics should report diagnostics related to codes with pathRegex config is undefined 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
3+
exports[`Transpiler diagnostics should report diagnostics related to codes with exclude config matches file name 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
44

5-
exports[`Transpiler diagnostics should report diagnostics related to codes with pathRegex config matches file name 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
5+
exports[`Transpiler diagnostics should report diagnostics related to codes with pathRegex config is undefined 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
66

77
exports[`Transpiler jsx option should compile tsx file for jsx preserve 1`] = `
88
===[ FILE: foo.tsx ]============================================================

src/compiler/language-service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ describe('Language service', () => {
259259
const compiler = makeCompiler({
260260
tsJestConfig: {
261261
...baseTsJestConfig,
262-
diagnostics: { pathRegex: 'foo.spec.ts' },
262+
diagnostics: { exclude: ['foo.spec.ts'] },
263263
},
264264
})
265265

src/compiler/transpiler.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ const t: string = f(5)
120120
).toThrowErrorMatchingSnapshot()
121121
})
122122

123-
it('should report diagnostics related to codes with pathRegex config matches file name', () => {
123+
it('should report diagnostics related to codes with exclude config matches file name', () => {
124124
const compiler = makeCompiler({
125-
tsJestConfig: { ...baseTsJestConfig, tsconfig: false, diagnostics: { pathRegex: 'foo.ts' } },
125+
tsJestConfig: { ...baseTsJestConfig, tsconfig: false, diagnostics: { exclude: ['foo.ts'] } },
126126
})
127127

128128
expect(() =>
@@ -136,9 +136,9 @@ const t: string = f(5)
136136
).toThrowErrorMatchingSnapshot()
137137
})
138138

139-
it('should not report diagnostics related to codes with pathRegex config does not match file name', () => {
139+
it('should not report diagnostics related to codes with exclude config does not match file name', () => {
140140
const compiler = makeCompiler({
141-
tsJestConfig: { ...baseTsJestConfig, tsconfig: false, diagnostics: { pathRegex: 'bar.ts' } },
141+
tsJestConfig: { ...baseTsJestConfig, tsconfig: false, diagnostics: { exclude: ['bar.ts'] } },
142142
})
143143

144144
expect(() =>

src/config/config-set.spec.ts

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,20 @@ describe('isTestFile', () => {
499499

500500
describe('shouldStringifyContent', () => {
501501
it('should return correct value is defined', () => {
502-
const cs = createConfigSet({ tsJestConfig: { tsconfig: false, stringifyContentPathRegex: '\\.str$' } as any })
503-
expect(cs.shouldStringifyContent('/foo/bar.ts')).toBe(false)
504-
expect(cs.shouldStringifyContent('/foo/bar.str')).toBe(true)
502+
const cs1 = createConfigSet({ tsJestConfig: { tsconfig: false, stringifyContentPathRegex: '\\.str$' } as any })
503+
504+
expect(cs1.shouldStringifyContent('/foo/bar.ts')).toBe(false)
505+
expect(cs1.shouldStringifyContent('/foo/bar.str')).toBe(true)
506+
507+
const cs2 = createConfigSet({ tsJestConfig: { tsconfig: false, stringifyContentPathRegex: /\.str$/ } as any })
508+
509+
expect(cs2.shouldStringifyContent('/foo/bar.ts')).toBe(false)
510+
expect(cs2.shouldStringifyContent('/foo/bar.str')).toBe(true)
505511
})
506512

507513
it('should return correct value when stringifyContentPathRegex is undefined', () => {
508514
const cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any })
515+
509516
expect(cs.shouldStringifyContent('/foo/bar.ts')).toBe(false)
510517
})
511518
}) // shouldStringifyContent
@@ -554,8 +561,19 @@ describe('raiseDiagnostics', () => {
554561
code = 9999,
555562
category = ts.DiagnosticCategory.Warning,
556563
}: Partial<ts.Diagnostic> = {}): ts.Diagnostic => ({ messageText, code, category } as any)
557-
it('should throw when diagnostics contains file path and pathRegex config matches file path', () => {
558-
const cs = createConfigSet({
564+
565+
it('should throw when diagnostics contains file path and exclude config matches file path', () => {
566+
let cs = createConfigSet({
567+
logger,
568+
tsJestConfig: { diagnostics: { exclude: ['src/__mocks__/index.ts'], pretty: false } },
569+
})
570+
logger.target.clear()
571+
572+
expect(() =>
573+
cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger),
574+
).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`)
575+
576+
cs = createConfigSet({
559577
logger,
560578
tsJestConfig: { diagnostics: { pathRegex: 'src/__mocks__/index.ts', pretty: false } },
561579
})
@@ -566,8 +584,16 @@ describe('raiseDiagnostics', () => {
566584
).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`)
567585
})
568586

569-
it("should not throw when diagnostics contains file path and pathRegex config doesn't match file path", () => {
570-
const cs = createConfigSet({
587+
it("should not throw when diagnostics contains file path and exclude config doesn't match file path", () => {
588+
let cs = createConfigSet({
589+
logger,
590+
tsJestConfig: { diagnostics: { warnOnly: true, exclude: ['/bar/'], pretty: false } },
591+
})
592+
logger.target.clear()
593+
594+
expect(() => cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger)).not.toThrow()
595+
596+
cs = createConfigSet({
571597
logger,
572598
tsJestConfig: { diagnostics: { warnOnly: true, pathRegex: '/bar/', pretty: false } },
573599
})
@@ -591,38 +617,106 @@ describe('raiseDiagnostics', () => {
591617
file = program.getSourceFiles().find((sourceFile) => sourceFile.fileName === 'src/__mocks__/index.ts'),
592618
}: Partial<ts.Diagnostic> = {}): ts.Diagnostic => ({ messageText, code, category, file } as any)
593619

594-
it("should not throw when pathRegex config doesn't match source file path", () => {
620+
it("should not throw when exclude config doesn't match source file path", () => {
595621
const cs = createConfigSet({
596622
logger,
597-
tsJestConfig: { diagnostics: { pathRegex: '/foo/', pretty: false, ignoreCodes: [1111] } },
623+
tsJestConfig: { diagnostics: { exclude: ['/foo/'], pretty: false, ignoreCodes: [1111] } },
598624
})
599625
logger.target.clear()
600626

601627
expect(() => cs.raiseDiagnostics([makeDiagnostic()])).not.toThrow()
628+
629+
const cs1 = createConfigSet({
630+
logger,
631+
tsJestConfig: { diagnostics: { pathRegex: '/foo/', pretty: false, ignoreCodes: [1111] } },
632+
})
633+
logger.target.clear()
634+
635+
expect(() => cs1.raiseDiagnostics([makeDiagnostic()])).not.toThrow()
602636
})
603637

604-
it("should throw when pathRegex config doesn't match source file path", () => {
638+
it("should throw when exclude config doesn't match source file path", () => {
605639
const cs = createConfigSet({
606640
logger,
607-
tsJestConfig: { diagnostics: { pathRegex: 'src/__mocks__/index.ts', pretty: false } },
641+
tsJestConfig: { diagnostics: { exclude: ['src/__mocks__/index.ts'], pretty: false } },
608642
})
609643
logger.target.clear()
610644

611645
expect(() => cs.raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(
612646
`"Debug Failure. False expression: position cannot precede the beginning of the file"`,
613647
)
648+
649+
const cs1 = createConfigSet({
650+
logger,
651+
tsJestConfig: { diagnostics: { pathRegex: 'src/__mocks__/index.ts', pretty: false } },
652+
})
653+
logger.target.clear()
654+
655+
expect(() => cs1.raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(
656+
`"Debug Failure. False expression: position cannot precede the beginning of the file"`,
657+
)
614658
})
615659
})
616660
}) // raiseDiagnostics
617661

618662
describe('shouldReportDiagnostics', () => {
619-
it('should return correct value', () => {
620-
let cs = createConfigSet({ tsJestConfig: { tsconfig: false, diagnostics: { pathRegex: '/foo/' } } as any })
663+
it('should return correct value for ts/tsx files', () => {
664+
let cs = createConfigSet({
665+
tsJestConfig: {
666+
tsconfig: false,
667+
diagnostics: { exclude: ['**/foo/*.ts', '**/foo/*.tsx'] },
668+
} as any,
669+
})
670+
671+
expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
672+
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false)
673+
674+
cs = createConfigSet({
675+
tsJestConfig: {
676+
tsconfig: false,
677+
diagnostics: { pathRegex: '/foo/' },
678+
} as any,
679+
})
680+
621681
expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
622-
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(false)
682+
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false)
683+
623684
cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any })
685+
624686
expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
625-
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(true)
687+
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(true)
688+
})
689+
690+
test('should return correct value for js/jsx files with checkJs compiler option', () => {
691+
let cs = createConfigSet({
692+
tsJestConfig: {
693+
tsconfig: { checkJs: false },
694+
diagnostics: { exclude: ['foo/*'] },
695+
},
696+
})
697+
698+
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false)
699+
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false)
700+
701+
cs = createConfigSet({
702+
tsJestConfig: {
703+
tsconfig: { checkJs: false },
704+
diagnostics: { pathRegex: '/bar/' },
705+
},
706+
})
707+
708+
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false)
709+
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false)
710+
711+
cs = createConfigSet({
712+
tsJestConfig: {
713+
tsconfig: { checkJs: true },
714+
diagnostics: { exclude: ['**/foo/*.js', '**/foo/*.jsx'] },
715+
},
716+
})
717+
718+
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true)
719+
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true)
626720
})
627721
}) // shouldReportDiagnostics
628722

@@ -979,15 +1073,15 @@ describe('diagnostics', () => {
9791073
{
9801074
diagnostics: {
9811075
ignoreCodes: '10, 25',
982-
pathRegex: '\\.test\\.ts',
1076+
exclude: ['\\.test\\.ts'],
9831077
pretty: false,
9841078
},
9851079
},
9861080
{
9871081
diagnostics: {
9881082
ignoreCodes: ['10', 25],
9891083
pretty: false,
990-
pathRegex: RegExp('\\.test\\.ts'),
1084+
exclude: ['\\.test\\.ts'],
9911085
},
9921086
},
9931087
{ diagnostics: { warnOnly: true } },

0 commit comments

Comments
 (0)