Skip to content

Commit 777edf5

Browse files
committed
feat: allow env var to add diagnostic codes to ignore
1 parent 83d7517 commit 777edf5

6 files changed

Lines changed: 61 additions & 29 deletions

File tree

e2e/__tests__/__snapshots__/type-checking.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jest exit code: 1
88
FAIL ./main.spec.ts
99
● Test suite failed to run
1010
11-
Unable to compile TypeScript:
11+
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
1212
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
1313
1414
2 return input
@@ -30,7 +30,7 @@ jest exit code: 1
3030
FAIL ./main.spec.ts
3131
● Test suite failed to run
3232
33-
Unable to compile TypeScript:
33+
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
3434
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
3535
3636
2 return input
@@ -52,7 +52,7 @@ jest exit code: 1
5252
FAIL ./main.spec.ts
5353
● Test suite failed to run
5454
55-
Unable to compile TypeScript:
55+
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
5656
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
5757
5858
2 return input
@@ -74,7 +74,7 @@ jest exit code: 1
7474
FAIL ./main.spec.ts
7575
● Test suite failed to run
7676
77-
Unable to compile TypeScript:
77+
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
7878
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
7979
8080
2 return input
@@ -96,7 +96,7 @@ jest exit code: 1
9696
FAIL ./main.spec.ts
9797
● Test suite failed to run
9898
99-
Unable to compile TypeScript:
99+
Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
100100
main.ts:2:3 - error TS2322: Type 'string' is not assignable to type 'number'.
101101
102102
2 return input

scripts/test-external-project.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,10 @@ cmdLine.push(...jestArgs)
8888
logger.log('starting the tests using:', ...cmdLine)
8989
logger.log()
9090

91-
spawnSync(cmdLine.shift(), cmdLine, { cwd: projectPath, stdio: 'inherit' })
91+
spawnSync(cmdLine.shift(), cmdLine, {
92+
cwd: projectPath,
93+
stdio: 'inherit',
94+
env: Object.assign({}, process.env, {
95+
TS_JEST_IGNORE_DIAGNOSTICS: '5023,5024',
96+
}),
97+
})

src/lib/compiler.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ const f = (v: number) => v
5050
const t: string = f(5)
5151
const v: boolean = t
5252
`,
53-
'[eval].ts',
53+
'foo.ts',
5454
),
5555
).toThrowErrorMatchingInlineSnapshot(`
56-
"Unable to compile TypeScript:
57-
[eval].ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'.
58-
[eval].ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'."
56+
"Unable to compile TypeScript (add code(s) in \`[jest-config].globals.ts-jest.diagnostics.ignoreCodes\` to ignore):
57+
foo.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'.
58+
foo.ts(4,7): error TS2322: Type 'string' is not assignable to type 'boolean'."
5959
`)
6060
})
6161
})

src/lib/config-set.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ const normalizeRegex = (
9191
: undefined
9292
}
9393

94+
const toDiagnosticCode = (code: any): number | undefined => {
95+
return code
96+
? parseInt(('' + code).trim().replace(/^TS/, ''), 10) || undefined
97+
: undefined
98+
}
99+
100+
const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
101+
if (!Array.isArray(items)) items = [items]
102+
for (let item of items) {
103+
if (!item) continue
104+
if (Array.isArray(item)) {
105+
toDiagnosticCodeList(item, into)
106+
continue
107+
} else if (typeof item === 'string') {
108+
const children = item.trim().split(/\s*,\s*/g)
109+
if (children.length > 1) {
110+
toDiagnosticCodeList(children, into)
111+
continue
112+
}
113+
item = children[0]
114+
}
115+
if (!item) continue
116+
const code = toDiagnosticCode(item)
117+
if (code && !into.includes(code)) into.push(code)
118+
}
119+
return into
120+
}
121+
94122
export class ConfigSet {
95123
constructor(
96124
private _jestConfig: jest.ProjectConfig,
@@ -160,6 +188,12 @@ export class ConfigSet {
160188
// diagnostics
161189
let diagnostics: TsJestConfig['diagnostics']
162190
const { diagnostics: diagnosticsOpt = true } = options
191+
// messy list of stuff to ignore (will be casted later)
192+
const ignoreList: any[] = [
193+
IGNORE_DIAGNOSTIC_CODES,
194+
process.env.TS_JEST_IGNORE_DIAGNOSTICS,
195+
]
196+
163197
if (diagnosticsOpt === true || diagnosticsOpt == null) {
164198
diagnostics = { ignoreCodes: [], pretty: true }
165199
} else if (diagnosticsOpt === false) {
@@ -169,25 +203,15 @@ export class ConfigSet {
169203
pathRegex: MATCH_NOTHING.source, // matches nothing
170204
}
171205
} else {
172-
let ignoreCodes: any[] = []
173-
if (diagnosticsOpt.ignoreCodes) {
174-
ignoreCodes = Array.isArray(diagnosticsOpt.ignoreCodes)
175-
? diagnosticsOpt.ignoreCodes
176-
: `${diagnosticsOpt.ignoreCodes}`.trim().split(/\s*,\s*/g)
177-
}
178-
206+
ignoreList.push(diagnosticsOpt.ignoreCodes)
179207
diagnostics = {
180208
pretty: diagnosticsOpt.pretty == null ? true : !!diagnosticsOpt.pretty,
181-
ignoreCodes: ignoreCodes.map(Number),
209+
ignoreCodes: [],
182210
pathRegex: normalizeRegex(diagnosticsOpt.pathRegex),
183211
}
184212
}
185-
diagnostics.ignoreCodes = IGNORE_DIAGNOSTIC_CODES.concat(
186-
diagnostics.ignoreCodes,
187-
).filter(
188-
(code, index, list) =>
189-
code && !isNaN(code) && list.indexOf(code) === index,
190-
)
213+
// now we clean and flaten the list
214+
diagnostics.ignoreCodes = toDiagnosticCodeList(ignoreList)
191215

192216
// stringifyContentPathRegex option
193217
const stringifyContentPathRegex = normalizeRegex(

src/lib/messages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ export enum Errors {
77
FileNotFound = 'File not found: {{inputPath}} (resolved as: {{resolvedPath}})',
88
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.",
99
MissingDependency = "Module {{module}} is not installed. If you're experiencing issues, consider installing a supported version ({{expectedVersion}}).",
10+
UnableToCompileTypeScript = 'Unable to compile TypeScript ({{help}}):\n{{diagnostics}}',
1011
}
1112

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

1718
export enum Deprecateds {

src/lib/ts-error.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inspect } from 'util'
22
import { BaseError } from 'make-error'
3-
import { Helps } from './messages'
3+
import { Helps, Errors, interpolate } from './messages'
44

55
/**
66
* @internal
@@ -15,9 +15,10 @@ export class TSError extends BaseError {
1515

1616
constructor(public diagnosticText: string, public diagnosticCodes: number[]) {
1717
super(
18-
`⨯ Unable to compile TypeScript:\n${diagnosticText.trim()}\n ↳ ${
19-
Helps.IgnoreDiagnosticCode
20-
}`,
18+
interpolate(Errors.UnableToCompileTypeScript, {
19+
diagnostics: diagnosticText.trim(),
20+
help: Helps.IgnoreDiagnosticCode,
21+
}),
2122
)
2223
// ensure we blacklist any of our code
2324
Object.defineProperty(this, 'stack', { value: '' })

0 commit comments

Comments
 (0)