Skip to content

Commit c5ce979

Browse files
authoredMay 30, 2021
fix(config): exclude should only exclude files which match glob values (#2637)
Closes #2634
1 parent eaa4e13 commit c5ce979

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed
 

‎src/config/config-set.spec.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -498,26 +498,27 @@ describe('raiseDiagnostics', () => {
498498
category = ts.DiagnosticCategory.Warning,
499499
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
500500
Partial<ts.Diagnostic> = {}): ts.Diagnostic => ({ messageText, code, category } as any)
501-
it('should throw when diagnostics contains file path and exclude config matches file path', () => {
501+
test('should not throw when diagnostics contains file path and exclude config matches file path', () => {
502502
const cs = createConfigSet({
503503
logger,
504504
tsJestConfig: { diagnostics: { exclude: ['src/__mocks__/index.ts'], pretty: false } },
505505
})
506506
logger.target.clear()
507507

508-
expect(() =>
509-
cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger),
510-
).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`)
508+
expect(() => cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger)).not.toThrow()
511509
})
512510

513-
it("should not throw when diagnostics contains file path and exclude config doesn't match file path", () => {
511+
test("should throw when diagnostics contains file path and exclude config doesn't match file path", () => {
514512
const cs = createConfigSet({
515513
logger,
516-
tsJestConfig: { diagnostics: { warnOnly: true, exclude: ['/bar/'], pretty: false } },
514+
tsJestConfig: { diagnostics: { exclude: ['/bar/'], pretty: false } },
517515
})
516+
cs.compilerModule.formatDiagnostics = jest.fn().mockReturnValueOnce('warning TS9999: foo')
518517
logger.target.clear()
519518

520-
expect(() => cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger)).not.toThrow()
519+
expect(() =>
520+
cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger),
521+
).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`)
521522
})
522523
})
523524

@@ -536,41 +537,40 @@ describe('raiseDiagnostics', () => {
536537
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
537538
Partial<ts.Diagnostic> = {}): ts.Diagnostic => ({ messageText, code, category, file } as any)
538539

539-
it("should not throw when exclude config doesn't match source file path", () => {
540+
test(`should throw when exclude config doesn't match source file path`, () => {
540541
const cs = createConfigSet({
541542
logger,
542543
tsJestConfig: { diagnostics: { exclude: ['/foo/'], pretty: false, ignoreCodes: [1111] } },
543544
})
545+
cs.compilerModule.formatDiagnostics = jest.fn().mockReturnValueOnce('warning TS9999: foo')
544546
logger.target.clear()
545547

546-
expect(() => cs.raiseDiagnostics([makeDiagnostic()])).not.toThrow()
548+
expect(() => cs.raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`)
547549
})
548550

549-
it("should throw when exclude config doesn't match source file path", () => {
551+
it(`should not throw when exclude config doesn't match source file path`, () => {
550552
const cs = createConfigSet({
551553
logger,
552554
tsJestConfig: { diagnostics: { exclude: ['src/__mocks__/index.ts'], pretty: false } },
553555
})
554556
logger.target.clear()
555557

556-
expect(() => cs.raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(
557-
`"Debug Failure. False expression: position cannot precede the beginning of the file"`,
558-
)
558+
expect(() => cs.raiseDiagnostics([makeDiagnostic()])).not.toThrow()
559559
})
560560
})
561561
}) // raiseDiagnostics
562562

563563
describe('shouldReportDiagnostics', () => {
564-
it('should return correct value for ts/tsx files', () => {
564+
test('should return correct value for ts/tsx files', () => {
565565
let cs = createConfigSet({
566566
tsJestConfig: {
567567
tsconfig: false,
568-
diagnostics: { exclude: ['**/foo/*.ts', '**/foo/*.tsx'] },
568+
diagnostics: { exclude: ['**/foo/*.ts', 'NOTHING'] },
569569
} as any, // eslint-disable-line @typescript-eslint/no-explicit-any
570570
})
571571

572-
expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
573-
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false)
572+
expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(false)
573+
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(true)
574574

575575
// eslint-disable-next-line @typescript-eslint/no-explicit-any
576576
cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any })
@@ -597,8 +597,8 @@ describe('shouldReportDiagnostics', () => {
597597
},
598598
})
599599

600-
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true)
601-
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true)
600+
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false)
601+
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false)
602602
})
603603
}) // shouldReportDiagnostics
604604

‎src/config/config-set.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class ConfigSet {
141141
/**
142142
* @internal
143143
*/
144-
private _shouldGetDiagnosticsForFile!: (filePath: string) => boolean
144+
private _shouldIgnoreDiagnosticsForFile!: (filePath: string) => boolean
145145
/**
146146
* @internal
147147
*/
@@ -277,9 +277,9 @@ export class ConfigSet {
277277
throws: diagnosticsOpt,
278278
}
279279
}
280-
this._shouldGetDiagnosticsForFile = this._diagnostics.exclude.length
280+
this._shouldIgnoreDiagnosticsForFile = this._diagnostics.exclude.length
281281
? globsToMatcher(this._diagnostics.exclude)
282-
: () => true
282+
: () => false
283283

284284
this.logger.debug({ diagnostics: this._diagnostics }, 'normalized diagnostics config via ts-jest option')
285285

@@ -549,8 +549,8 @@ export class ConfigSet {
549549
const fileExtension = extname(filePath)
550550

551551
return JS_JSX_EXTENSIONS.includes(fileExtension)
552-
? this.parsedTsConfig.options.checkJs && this._shouldGetDiagnosticsForFile(filePath)
553-
: this._shouldGetDiagnosticsForFile(filePath)
552+
? this.parsedTsConfig.options.checkJs && !this._shouldIgnoreDiagnosticsForFile(filePath)
553+
: !this._shouldIgnoreDiagnosticsForFile(filePath)
554554
}
555555

556556
/**

0 commit comments

Comments
 (0)
Please sign in to comment.