From c5ce97902c1b84dc354f270a4f596a6f5f634611 Mon Sep 17 00:00:00 2001 From: Ahn Date: Sun, 30 May 2021 23:02:46 +0200 Subject: [PATCH] fix(config): `exclude` should only exclude files which match glob values (#2637) Closes #2634 --- src/config/config-set.spec.ts | 38 +++++++++++++++++------------------ src/config/config-set.ts | 10 ++++----- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 995cb34d9e..25cff81ccd 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -498,26 +498,27 @@ describe('raiseDiagnostics', () => { category = ts.DiagnosticCategory.Warning, }: // eslint-disable-next-line @typescript-eslint/no-explicit-any Partial = {}): ts.Diagnostic => ({ messageText, code, category } as any) - it('should throw when diagnostics contains file path and exclude config matches file path', () => { + test('should not throw when diagnostics contains file path and exclude config matches file path', () => { const cs = createConfigSet({ logger, tsJestConfig: { diagnostics: { exclude: ['src/__mocks__/index.ts'], pretty: false } }, }) logger.target.clear() - expect(() => - cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger), - ).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`) + expect(() => cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger)).not.toThrow() }) - it("should not throw when diagnostics contains file path and exclude config doesn't match file path", () => { + test("should throw when diagnostics contains file path and exclude config doesn't match file path", () => { const cs = createConfigSet({ logger, - tsJestConfig: { diagnostics: { warnOnly: true, exclude: ['/bar/'], pretty: false } }, + tsJestConfig: { diagnostics: { exclude: ['/bar/'], pretty: false } }, }) + cs.compilerModule.formatDiagnostics = jest.fn().mockReturnValueOnce('warning TS9999: foo') logger.target.clear() - expect(() => cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger)).not.toThrow() + expect(() => + cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger), + ).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`) }) }) @@ -536,41 +537,40 @@ describe('raiseDiagnostics', () => { }: // eslint-disable-next-line @typescript-eslint/no-explicit-any Partial = {}): ts.Diagnostic => ({ messageText, code, category, file } as any) - it("should not throw when exclude config doesn't match source file path", () => { + test(`should throw when exclude config doesn't match source file path`, () => { const cs = createConfigSet({ logger, tsJestConfig: { diagnostics: { exclude: ['/foo/'], pretty: false, ignoreCodes: [1111] } }, }) + cs.compilerModule.formatDiagnostics = jest.fn().mockReturnValueOnce('warning TS9999: foo') logger.target.clear() - expect(() => cs.raiseDiagnostics([makeDiagnostic()])).not.toThrow() + expect(() => cs.raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`) }) - it("should throw when exclude config doesn't match source file path", () => { + it(`should not throw when exclude config doesn't match source file path`, () => { const cs = createConfigSet({ logger, tsJestConfig: { diagnostics: { exclude: ['src/__mocks__/index.ts'], pretty: false } }, }) logger.target.clear() - expect(() => cs.raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot( - `"Debug Failure. False expression: position cannot precede the beginning of the file"`, - ) + expect(() => cs.raiseDiagnostics([makeDiagnostic()])).not.toThrow() }) }) }) // raiseDiagnostics describe('shouldReportDiagnostics', () => { - it('should return correct value for ts/tsx files', () => { + test('should return correct value for ts/tsx files', () => { let cs = createConfigSet({ tsJestConfig: { tsconfig: false, - diagnostics: { exclude: ['**/foo/*.ts', '**/foo/*.tsx'] }, + diagnostics: { exclude: ['**/foo/*.ts', 'NOTHING'] }, } as any, // eslint-disable-line @typescript-eslint/no-explicit-any }) - expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true) - expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false) + expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(false) + expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(true) // eslint-disable-next-line @typescript-eslint/no-explicit-any cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any }) @@ -597,8 +597,8 @@ describe('shouldReportDiagnostics', () => { }, }) - expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true) - expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true) + expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false) + expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false) }) }) // shouldReportDiagnostics diff --git a/src/config/config-set.ts b/src/config/config-set.ts index a429cd65f9..b4ebd6d642 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -141,7 +141,7 @@ export class ConfigSet { /** * @internal */ - private _shouldGetDiagnosticsForFile!: (filePath: string) => boolean + private _shouldIgnoreDiagnosticsForFile!: (filePath: string) => boolean /** * @internal */ @@ -277,9 +277,9 @@ export class ConfigSet { throws: diagnosticsOpt, } } - this._shouldGetDiagnosticsForFile = this._diagnostics.exclude.length + this._shouldIgnoreDiagnosticsForFile = this._diagnostics.exclude.length ? globsToMatcher(this._diagnostics.exclude) - : () => true + : () => false this.logger.debug({ diagnostics: this._diagnostics }, 'normalized diagnostics config via ts-jest option') @@ -549,8 +549,8 @@ export class ConfigSet { const fileExtension = extname(filePath) return JS_JSX_EXTENSIONS.includes(fileExtension) - ? this.parsedTsConfig.options.checkJs && this._shouldGetDiagnosticsForFile(filePath) - : this._shouldGetDiagnosticsForFile(filePath) + ? this.parsedTsConfig.options.checkJs && !this._shouldIgnoreDiagnosticsForFile(filePath) + : !this._shouldIgnoreDiagnosticsForFile(filePath) } /**