Skip to content

Commit

Permalink
feat(config): introduce exclude to exclude files from diagnostics (#…
Browse files Browse the repository at this point in the history
…2308)

DEPRECATION
`pathRegex` is deprecated in favor of `exclude`
  • Loading branch information
ahnpnl committed Jan 29, 2021
1 parent 5ebab21 commit 0c555c2
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 105 deletions.
8 changes: 0 additions & 8 deletions e2e/__cases__/ts-jest-checks/index.spec.ts

This file was deleted.

13 changes: 0 additions & 13 deletions e2e/__tests__/ts-jest-checks.test.ts

This file was deleted.

30 changes: 13 additions & 17 deletions src/__helpers__/fakers.ts
Expand Up @@ -4,7 +4,7 @@ import { resolve } from 'path'

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

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

export const rootDir = filePath('')

export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
return {
isolatedModules: false,
compiler: 'typescript',
transformers: options?.transformers ?? Object.create(null),
babelConfig: undefined,
tsConfig: undefined,
stringifyContentPathRegex: undefined,
diagnostics: { ignoreCodes: [], pretty: false, throws: true },
...options,
}
}
const defaultTestRegex = ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$']
const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']

function getJestConfig<T extends Config.ProjectConfig>(
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
Expand Down Expand Up @@ -68,7 +58,15 @@ export function createConfigSet({
resolve?: ((path: string) => string) | null
[key: string]: any
} = {}): ConfigSet {
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), logger)
const jestCfg = getJestConfig(jestConfig, tsJestConfig)
const cs = new ConfigSet(
{
...jestCfg,
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...defaultTestMatch] : defaultTestMatch,
testRegex: jestConfig?.testRegex ? [...jestConfig.testRegex, ...defaultTestRegex] : defaultTestRegex,
},
logger,
)
if (resolve) {
cs.resolvePath = resolve
}
Expand All @@ -94,12 +92,10 @@ export function makeCompiler({
...(tsJestConfig.diagnostics as any),
pretty: false,
}
const defaultTestRegex = ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$']
const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']
jestConfig = {
...jestConfig,
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...defaultTestMatch] : defaultTestMatch,
testRegex: jestConfig?.testRegex ? [...defaultTestRegex, ...jestConfig.testRegex] : defaultTestRegex,
testRegex: jestConfig?.testRegex ? [...jestConfig.testRegex, ...defaultTestRegex] : defaultTestRegex,
}
const cs = createConfigSet({ jestConfig, tsJestConfig, parentConfig, resolve: null })

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/__snapshots__/transpiler.spec.ts.snap
@@ -1,8 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

exports[`Transpiler jsx option should compile tsx file for jsx preserve 1`] = `
===[ FILE: foo.tsx ]============================================================
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/language-service.spec.ts
Expand Up @@ -259,7 +259,7 @@ describe('Language service', () => {
const compiler = makeCompiler({
tsJestConfig: {
...baseTsJestConfig,
diagnostics: { pathRegex: 'foo.spec.ts' },
diagnostics: { exclude: ['foo.spec.ts'] },
},
})

Expand Down
8 changes: 4 additions & 4 deletions src/compiler/transpiler.spec.ts
Expand Up @@ -120,9 +120,9 @@ const t: string = f(5)
).toThrowErrorMatchingSnapshot()
})

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

expect(() =>
Expand All @@ -136,9 +136,9 @@ const t: string = f(5)
).toThrowErrorMatchingSnapshot()
})

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

expect(() =>
Expand Down
128 changes: 111 additions & 17 deletions src/config/config-set.spec.ts
Expand Up @@ -499,13 +499,20 @@ describe('isTestFile', () => {

describe('shouldStringifyContent', () => {
it('should return correct value is defined', () => {
const cs = createConfigSet({ tsJestConfig: { tsconfig: false, stringifyContentPathRegex: '\\.str$' } as any })
expect(cs.shouldStringifyContent('/foo/bar.ts')).toBe(false)
expect(cs.shouldStringifyContent('/foo/bar.str')).toBe(true)
const cs1 = createConfigSet({ tsJestConfig: { tsconfig: false, stringifyContentPathRegex: '\\.str$' } as any })

expect(cs1.shouldStringifyContent('/foo/bar.ts')).toBe(false)
expect(cs1.shouldStringifyContent('/foo/bar.str')).toBe(true)

const cs2 = createConfigSet({ tsJestConfig: { tsconfig: false, stringifyContentPathRegex: /\.str$/ } as any })

expect(cs2.shouldStringifyContent('/foo/bar.ts')).toBe(false)
expect(cs2.shouldStringifyContent('/foo/bar.str')).toBe(true)
})

it('should return correct value when stringifyContentPathRegex is undefined', () => {
const cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any })

expect(cs.shouldStringifyContent('/foo/bar.ts')).toBe(false)
})
}) // shouldStringifyContent
Expand Down Expand Up @@ -554,8 +561,19 @@ describe('raiseDiagnostics', () => {
code = 9999,
category = ts.DiagnosticCategory.Warning,
}: Partial<ts.Diagnostic> = {}): ts.Diagnostic => ({ messageText, code, category } as any)
it('should throw when diagnostics contains file path and pathRegex config matches file path', () => {
const cs = createConfigSet({

it('should throw when diagnostics contains file path and exclude config matches file path', () => {
let 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"`)

cs = createConfigSet({
logger,
tsJestConfig: { diagnostics: { pathRegex: 'src/__mocks__/index.ts', pretty: false } },
})
Expand All @@ -566,8 +584,16 @@ describe('raiseDiagnostics', () => {
).toThrowErrorMatchingInlineSnapshot(`"warning TS9999: foo"`)
})

it("should not throw when diagnostics contains file path and pathRegex config doesn't match file path", () => {
const cs = createConfigSet({
it("should not throw when diagnostics contains file path and exclude config doesn't match file path", () => {
let cs = createConfigSet({
logger,
tsJestConfig: { diagnostics: { warnOnly: true, exclude: ['/bar/'], pretty: false } },
})
logger.target.clear()

expect(() => cs.raiseDiagnostics([makeDiagnostic()], 'src/__mocks__/index.ts', logger)).not.toThrow()

cs = createConfigSet({
logger,
tsJestConfig: { diagnostics: { warnOnly: true, pathRegex: '/bar/', pretty: false } },
})
Expand All @@ -591,38 +617,106 @@ describe('raiseDiagnostics', () => {
file = program.getSourceFiles().find((sourceFile) => sourceFile.fileName === 'src/__mocks__/index.ts'),
}: Partial<ts.Diagnostic> = {}): ts.Diagnostic => ({ messageText, code, category, file } as any)

it("should not throw when pathRegex 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: { pathRegex: '/foo/', pretty: false, ignoreCodes: [1111] } },
tsJestConfig: { diagnostics: { exclude: ['/foo/'], pretty: false, ignoreCodes: [1111] } },
})
logger.target.clear()

expect(() => cs.raiseDiagnostics([makeDiagnostic()])).not.toThrow()

const cs1 = createConfigSet({
logger,
tsJestConfig: { diagnostics: { pathRegex: '/foo/', pretty: false, ignoreCodes: [1111] } },
})
logger.target.clear()

expect(() => cs1.raiseDiagnostics([makeDiagnostic()])).not.toThrow()
})

it("should throw when pathRegex config doesn't match source file path", () => {
it("should throw when exclude config doesn't match source file path", () => {
const cs = createConfigSet({
logger,
tsJestConfig: { diagnostics: { pathRegex: 'src/__mocks__/index.ts', pretty: false } },
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"`,
)

const cs1 = createConfigSet({
logger,
tsJestConfig: { diagnostics: { pathRegex: 'src/__mocks__/index.ts', pretty: false } },
})
logger.target.clear()

expect(() => cs1.raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(
`"Debug Failure. False expression: position cannot precede the beginning of the file"`,
)
})
})
}) // raiseDiagnostics

describe('shouldReportDiagnostics', () => {
it('should return correct value', () => {
let cs = createConfigSet({ tsJestConfig: { tsconfig: false, diagnostics: { pathRegex: '/foo/' } } as any })
it('should return correct value for ts/tsx files', () => {
let cs = createConfigSet({
tsJestConfig: {
tsconfig: false,
diagnostics: { exclude: ['**/foo/*.ts', '**/foo/*.tsx'] },
} as any,
})

expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false)

cs = createConfigSet({
tsJestConfig: {
tsconfig: false,
diagnostics: { pathRegex: '/foo/' },
} as any,
})

expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(false)
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false)

cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any })

expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(true)
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(true)
})

test('should return correct value for js/jsx files with checkJs compiler option', () => {
let cs = createConfigSet({
tsJestConfig: {
tsconfig: { checkJs: false },
diagnostics: { exclude: ['foo/*'] },
},
})

expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false)
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false)

cs = createConfigSet({
tsJestConfig: {
tsconfig: { checkJs: false },
diagnostics: { pathRegex: '/bar/' },
},
})

expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false)
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false)

cs = createConfigSet({
tsJestConfig: {
tsconfig: { checkJs: true },
diagnostics: { exclude: ['**/foo/*.js', '**/foo/*.jsx'] },
},
})

expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true)
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true)
})
}) // shouldReportDiagnostics

Expand Down Expand Up @@ -979,15 +1073,15 @@ describe('diagnostics', () => {
{
diagnostics: {
ignoreCodes: '10, 25',
pathRegex: '\\.test\\.ts',
exclude: ['\\.test\\.ts'],
pretty: false,
},
},
{
diagnostics: {
ignoreCodes: ['10', 25],
pretty: false,
pathRegex: RegExp('\\.test\\.ts'),
exclude: ['\\.test\\.ts'],
},
},
{ diagnostics: { warnOnly: true } },
Expand Down

0 comments on commit 0c555c2

Please sign in to comment.