Skip to content

Commit

Permalink
fix(diagnostics): throws only for category warning and error
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huafu committed Sep 22, 2018
1 parent 9efc6a0 commit bb28849
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/config/config-set.spec.ts
@@ -1,6 +1,6 @@
import { testing } from 'bs-logger'
import { resolve } from 'path'
import ts, { Diagnostic, ModuleKind, ScriptTarget } from 'typescript'
import ts, { Diagnostic, DiagnosticCategory, ModuleKind, ScriptTarget } from 'typescript'

import * as _myModule from '..'
import * as fakers from '../__helpers__/fakers'
Expand Down Expand Up @@ -295,7 +295,7 @@ describe('typescript', () => {
target.clear()
const cs = createConfigSet({
tsJestConfig: {
tsConfig: { module: 'ES6', esModuleInterop: false, allowSyntheticDefaultImports: false } as any,
tsConfig: { module: 'ES6', esModuleInterop: false } as any,
diagnostics: { warnOnly: true, pretty: false },
},
resolve: null,
Expand All @@ -307,7 +307,7 @@ describe('typescript', () => {
})
expect(target.lines.warn.join()).toMatchInlineSnapshot(`
"[level:40] TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
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.
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.
"
`)
})
Expand Down Expand Up @@ -540,15 +540,20 @@ Object {

describe('raiseDiagnostics', () => {
const createTsError = jest.fn(
(list: Diagnostic[]) => new Error(list.map(d => `[${d.code}] ${d.messageText}`).join('\n')),
(list: Diagnostic[]) => new Error(list.map(d => `[TS${d.code}] ${d.messageText}`).join('\n')),
)
const filterDiagnostics = jest.fn(list => list)
const logger = testing.createLoggerMock()
const diagnostic: Diagnostic = { messageText: 'foo', code: 'TS9999' } as any
const makeDiagnostic = ({
messageText = 'foo',
code = 9999,
category = DiagnosticCategory.Warning,
}: Partial<Diagnostic> = {}): Diagnostic => ({ messageText, code, category } as any)
it('should throw when warnOnly is false', () => {
const { raiseDiagnostics } = createConfigSet({ createTsError, filterDiagnostics })
expect(() => raiseDiagnostics([])).not.toThrow()
expect(() => raiseDiagnostics([diagnostic])).toThrowErrorMatchingInlineSnapshot(`"[TS9999] foo"`)
expect(() => raiseDiagnostics([makeDiagnostic()])).toThrowErrorMatchingInlineSnapshot(`"[TS9999] foo"`)
expect(() => raiseDiagnostics([makeDiagnostic({ category: DiagnosticCategory.Message })])).not.toThrow()
})
it('should not throw when warnOnly is true', () => {
const { raiseDiagnostics } = createConfigSet({
Expand All @@ -559,7 +564,7 @@ describe('raiseDiagnostics', () => {
})
logger.target.clear()
expect(() => raiseDiagnostics([])).not.toThrow()
expect(() => raiseDiagnostics([diagnostic])).not.toThrow()
expect(() => raiseDiagnostics([makeDiagnostic()])).not.toThrow()
expect(logger.target.lines).toMatchInlineSnapshot(`
Array [
"[level:40] [TS9999] foo
Expand Down
24 changes: 19 additions & 5 deletions src/config/config-set.ts
Expand Up @@ -265,12 +265,17 @@ export class ConfigSet {
tsJest: {
diagnostics: { throws },
},
compilerModule: { DiagnosticCategory },
} = this
return (diagnostics: Diagnostic[], filePath?: string, logger: Logger = this.logger): void | never => {
const filteredDiagnostics = filterDiagnostics(diagnostics, filePath)
if (filteredDiagnostics.length === 0) return
const error = createTsError(filteredDiagnostics)
if (throws) throw error
// only throw if `warnOnly` and it is a warning or error
const importantCategories = [DiagnosticCategory.Warning, DiagnosticCategory.Error]
if (throws && filteredDiagnostics.some(d => importantCategories.includes(d.category))) {
throw error
}
logger.warn({ error }, error.message)
}
}
Expand Down Expand Up @@ -621,10 +626,19 @@ export class ConfigSet {
? ts.ModuleKind.CommonJS
: ts.ModuleKind.ESNext
const moduleValue = finalOptions.module == null ? defaultModule : finalOptions.module
if (moduleValue !== forcedOptions.module && !finalOptions.esModuleInterop) {
result.errors.push(this.makeDiagnostic(DiagnosticCodes.ConfigModuleOption, Errors.ConfigNoModuleInterop))
// at least enable synthetic default imports
finalOptions.allowSyntheticDefaultImports = true
if (
moduleValue !== forcedOptions.module &&
!(finalOptions.esModuleInterop || finalOptions.allowSyntheticDefaultImports)
) {
result.errors.push(
this.makeDiagnostic(DiagnosticCodes.ConfigModuleOption, Errors.ConfigNoModuleInterop, {
category: ts.DiagnosticCategory.Message,
}),
)
// at least enable synthetic default imports (except if it's set in the input config)
if (!('allowSyntheticDefaultImports' in config.compilerOptions)) {
finalOptions.allowSyntheticDefaultImports = true
}
}

// ensure undefined are removed and other values are overridden
Expand Down

0 comments on commit bb28849

Please sign in to comment.