Skip to content

Commit

Permalink
feat: allow env var to add diagnostic codes to ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 20, 2018
1 parent 83d7517 commit 777edf5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 29 deletions.
10 changes: 5 additions & 5 deletions e2e/__tests__/__snapshots__/type-checking.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jest exit code: 1
FAIL ./main.spec.ts
● Test suite failed to run
Unable to compile TypeScript:
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
2 return input
Expand All @@ -30,7 +30,7 @@ jest exit code: 1
FAIL ./main.spec.ts
● Test suite failed to run
Unable to compile TypeScript:
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
2 return input
Expand All @@ -52,7 +52,7 @@ jest exit code: 1
FAIL ./main.spec.ts
● Test suite failed to run
Unable to compile TypeScript:
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
2 return input
Expand All @@ -74,7 +74,7 @@ jest exit code: 1
FAIL ./main.spec.ts
● Test suite failed to run
Unable to compile TypeScript:
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
2 return input
Expand All @@ -96,7 +96,7 @@ jest exit code: 1
FAIL ./main.spec.ts
● Test suite failed to run
Unable to compile TypeScript:
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
2 return input
Expand Down
8 changes: 7 additions & 1 deletion scripts/test-external-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ cmdLine.push(...jestArgs)
logger.log('starting the tests using:', ...cmdLine)
logger.log()

spawnSync(cmdLine.shift(), cmdLine, { cwd: projectPath, stdio: 'inherit' })
spawnSync(cmdLine.shift(), cmdLine, {
cwd: projectPath,
stdio: 'inherit',
env: Object.assign({}, process.env, {
TS_JEST_IGNORE_DIAGNOSTICS: '5023,5024',
}),
})
8 changes: 4 additions & 4 deletions src/lib/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ const f = (v: number) => v
const t: string = f(5)
const v: boolean = t
`,
'[eval].ts',
'foo.ts',
),
).toThrowErrorMatchingInlineSnapshot(`
"Unable to compile TypeScript:
[eval].ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'.
[eval].ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'."
"Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
foo.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'.
foo.ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'."
`)
})
})
Expand Down
52 changes: 38 additions & 14 deletions src/lib/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ const normalizeRegex = (
: undefined
}

const toDiagnosticCode = (code: any): number | undefined => {
return code
? parseInt(('' + code).trim().replace(/^TS/, ''), 10) || undefined
: undefined
}

const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
if (!Array.isArray(items)) items = [items]
for (let item of items) {
if (!item) continue
if (Array.isArray(item)) {
toDiagnosticCodeList(item, into)
continue
} else if (typeof item === 'string') {
const children = item.trim().split(/\s*,\s*/g)
if (children.length > 1) {
toDiagnosticCodeList(children, into)
continue
}
item = children[0]
}
if (!item) continue
const code = toDiagnosticCode(item)
if (code && !into.includes(code)) into.push(code)
}
return into
}

export class ConfigSet {
constructor(
private _jestConfig: jest.ProjectConfig,
Expand Down Expand Up @@ -160,6 +188,12 @@ export class ConfigSet {
// diagnostics
let diagnostics: TsJestConfig['diagnostics']
const { diagnostics: diagnosticsOpt = true } = options
// messy list of stuff to ignore (will be casted later)
const ignoreList: any[] = [
IGNORE_DIAGNOSTIC_CODES,
process.env.TS_JEST_IGNORE_DIAGNOSTICS,
]

if (diagnosticsOpt === true || diagnosticsOpt == null) {
diagnostics = { ignoreCodes: [], pretty: true }
} else if (diagnosticsOpt === false) {
Expand All @@ -169,25 +203,15 @@ export class ConfigSet {
pathRegex: MATCH_NOTHING.source, // matches nothing
}
} else {
let ignoreCodes: any[] = []
if (diagnosticsOpt.ignoreCodes) {
ignoreCodes = Array.isArray(diagnosticsOpt.ignoreCodes)
? diagnosticsOpt.ignoreCodes
: `${diagnosticsOpt.ignoreCodes}`.trim().split(/\s*,\s*/g)
}

ignoreList.push(diagnosticsOpt.ignoreCodes)
diagnostics = {
pretty: diagnosticsOpt.pretty == null ? true : !!diagnosticsOpt.pretty,
ignoreCodes: ignoreCodes.map(Number),
ignoreCodes: [],
pathRegex: normalizeRegex(diagnosticsOpt.pathRegex),
}
}
diagnostics.ignoreCodes = IGNORE_DIAGNOSTIC_CODES.concat(
diagnostics.ignoreCodes,
).filter(
(code, index, list) =>
code && !isNaN(code) && list.indexOf(code) === index,
)
// now we clean and flaten the list
diagnostics.ignoreCodes = toDiagnosticCodeList(ignoreList)

// stringifyContentPathRegex option
const stringifyContentPathRegex = normalizeRegex(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export enum Errors {
FileNotFound = 'File not found: {{inputPath}} (resolved as: {{resolvedPath}})',
UntestedDependencyVersion = "Version {{actualVersion}} of {{module}} installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version ({{expectedVersion}}). Please do not report issues in ts-jest if you are using unsupported versions.",
MissingDependency = "Module {{module}} is not installed. If you're experiencing issues, consider installing a supported version ({{expectedVersion}}).",
UnableToCompileTypeScript = 'Unable to compile TypeScript ({{help}}):\n{{diagnostics}}',
}

export enum Helps {
FixMissingModule = '{{label}}: `npm i -D {{module}}` (or `yarn add --dev {{module}}`)',
IgnoreDiagnosticCode = 'add any of the code(s) to `globals.ts-jest.diagnostics.ignoreCodes` array in Jest configuration will silent this',
IgnoreDiagnosticCode = 'add code(s) in `[jest-config].globals.ts-jest.diagnostics.ignoreCodes` to ignore',
}

export enum Deprecateds {
Expand Down
9 changes: 5 additions & 4 deletions src/lib/ts-error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inspect } from 'util'
import { BaseError } from 'make-error'
import { Helps } from './messages'
import { Helps, Errors, interpolate } from './messages'

/**
* @internal
Expand All @@ -15,9 +15,10 @@ export class TSError extends BaseError {

constructor(public diagnosticText: string, public diagnosticCodes: number[]) {
super(
`⨯ Unable to compile TypeScript:\n${diagnosticText.trim()}\n ↳ ${
Helps.IgnoreDiagnosticCode
}`,
interpolate(Errors.UnableToCompileTypeScript, {
diagnostics: diagnosticText.trim(),
help: Helps.IgnoreDiagnosticCode,
}),
)
// ensure we blacklist any of our code
Object.defineProperty(this, 'stack', { value: '' })
Expand Down

0 comments on commit 777edf5

Please sign in to comment.