Skip to content

Commit

Permalink
fix(config): exclude should only exclude files which match glob val…
Browse files Browse the repository at this point in the history
…ues (#2637)

Closes #2634
  • Loading branch information
ahnpnl authored May 30, 2021
1 parent eaa4e13 commit c5ce979
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
38 changes: 19 additions & 19 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,26 +498,27 @@ describe('raiseDiagnostics', () => {
category = ts.DiagnosticCategory.Warning,
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
Partial<ts.Diagnostic> = {}): 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"`)
})
})

Expand All @@ -536,41 +537,40 @@ describe('raiseDiagnostics', () => {
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
Partial<ts.Diagnostic> = {}): 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 })
Expand All @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class ConfigSet {
/**
* @internal
*/
private _shouldGetDiagnosticsForFile!: (filePath: string) => boolean
private _shouldIgnoreDiagnosticsForFile!: (filePath: string) => boolean
/**
* @internal
*/
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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)
}

/**
Expand Down

0 comments on commit c5ce979

Please sign in to comment.