Skip to content

Commit bb28849

Browse files
committed
fix(diagnostics): throws only for category warning and error
Diagnostics have a category. By default if warnOnly is not set, when diagnostic(s) of kind message or suggestion will be raised, ts-jest will log it but not throw. This also changes the category of the internal TS151000. Closes #748
1 parent 9efc6a0 commit bb28849

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/config/config-set.spec.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { testing } from 'bs-logger'
22
import { resolve } from 'path'
3-
import ts, { Diagnostic, ModuleKind, ScriptTarget } from 'typescript'
3+
import ts, { Diagnostic, DiagnosticCategory, ModuleKind, ScriptTarget } from 'typescript'
44

55
import * as _myModule from '..'
66
import * as fakers from '../__helpers__/fakers'
@@ -295,7 +295,7 @@ describe('typescript', () => {
295295
target.clear()
296296
const cs = createConfigSet({
297297
tsJestConfig: {
298-
tsConfig: { module: 'ES6', esModuleInterop: false, allowSyntheticDefaultImports: false } as any,
298+
tsConfig: { module: 'ES6', esModuleInterop: false } as any,
299299
diagnostics: { warnOnly: true, pretty: false },
300300
},
301301
resolve: null,
@@ -307,7 +307,7 @@ describe('typescript', () => {
307307
})
308308
expect(target.lines.warn.join()).toMatchInlineSnapshot(`
309309
"[level:40] TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
310-
warning TS151001: If you have issues related to imports, you should consider setting \`esModuleInterop\` to \`true\` in your TypeScript configuration file (usually \`tsconfig.json\`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
310+
message TS151001: If you have issues related to imports, you should consider setting \`esModuleInterop\` to \`true\` in your TypeScript configuration file (usually \`tsconfig.json\`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
311311
"
312312
`)
313313
})
@@ -540,15 +540,20 @@ Object {
540540

541541
describe('raiseDiagnostics', () => {
542542
const createTsError = jest.fn(
543-
(list: Diagnostic[]) => new Error(list.map(d => `[${d.code}] ${d.messageText}`).join('\n')),
543+
(list: Diagnostic[]) => new Error(list.map(d => `[TS${d.code}] ${d.messageText}`).join('\n')),
544544
)
545545
const filterDiagnostics = jest.fn(list => list)
546546
const logger = testing.createLoggerMock()
547-
const diagnostic: Diagnostic = { messageText: 'foo', code: 'TS9999' } as any
547+
const makeDiagnostic = ({
548+
messageText = 'foo',
549+
code = 9999,
550+
category = DiagnosticCategory.Warning,
551+
}: Partial<Diagnostic> = {}): Diagnostic => ({ messageText, code, category } as any)
548552
it('should throw when warnOnly is false', () => {
549553
const { raiseDiagnostics } = createConfigSet({ createTsError, filterDiagnostics })
550554
expect(() => raiseDiagnostics([])).not.toThrow()
551-
expect(() => raiseDiagnostics([diagnostic])).toThrowErrorMatchingInlineSnapshot(`"[TS9999] foo"`)
555+
expect(() => raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(`"[TS9999] foo"`)
556+
expect(() => raiseDiagnostics([makeDiagnostic({ category: DiagnosticCategory.Message })])).not.toThrow()
552557
})
553558
it('should not throw when warnOnly is true', () => {
554559
const { raiseDiagnostics } = createConfigSet({
@@ -559,7 +564,7 @@ describe('raiseDiagnostics', () => {
559564
})
560565
logger.target.clear()
561566
expect(() => raiseDiagnostics([])).not.toThrow()
562-
expect(() => raiseDiagnostics([diagnostic])).not.toThrow()
567+
expect(() => raiseDiagnostics([makeDiagnostic()])).not.toThrow()
563568
expect(logger.target.lines).toMatchInlineSnapshot(`
564569
Array [
565570
"[level:40] [TS9999] foo

src/config/config-set.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,17 @@ export class ConfigSet {
265265
tsJest: {
266266
diagnostics: { throws },
267267
},
268+
compilerModule: { DiagnosticCategory },
268269
} = this
269270
return (diagnostics: Diagnostic[], filePath?: string, logger: Logger = this.logger): void | never => {
270271
const filteredDiagnostics = filterDiagnostics(diagnostics, filePath)
271272
if (filteredDiagnostics.length === 0) return
272273
const error = createTsError(filteredDiagnostics)
273-
if (throws) throw error
274+
// only throw if `warnOnly` and it is a warning or error
275+
const importantCategories = [DiagnosticCategory.Warning, DiagnosticCategory.Error]
276+
if (throws && filteredDiagnostics.some(d => importantCategories.includes(d.category))) {
277+
throw error
278+
}
274279
logger.warn({ error }, error.message)
275280
}
276281
}
@@ -621,10 +626,19 @@ export class ConfigSet {
621626
? ts.ModuleKind.CommonJS
622627
: ts.ModuleKind.ESNext
623628
const moduleValue = finalOptions.module == null ? defaultModule : finalOptions.module
624-
if (moduleValue !== forcedOptions.module && !finalOptions.esModuleInterop) {
625-
result.errors.push(this.makeDiagnostic(DiagnosticCodes.ConfigModuleOption, Errors.ConfigNoModuleInterop))
626-
// at least enable synthetic default imports
627-
finalOptions.allowSyntheticDefaultImports = true
629+
if (
630+
moduleValue !== forcedOptions.module &&
631+
!(finalOptions.esModuleInterop || finalOptions.allowSyntheticDefaultImports)
632+
) {
633+
result.errors.push(
634+
this.makeDiagnostic(DiagnosticCodes.ConfigModuleOption, Errors.ConfigNoModuleInterop, {
635+
category: ts.DiagnosticCategory.Message,
636+
}),
637+
)
638+
// at least enable synthetic default imports (except if it's set in the input config)
639+
if (!('allowSyntheticDefaultImports' in config.compilerOptions)) {
640+
finalOptions.allowSyntheticDefaultImports = true
641+
}
628642
}
629643

630644
// ensure undefined are removed and other values are overridden

0 commit comments

Comments
 (0)