Skip to content

Commit 1e04433

Browse files
authoredJan 15, 2021
feat(config): type checking js files based on checkJs (#2283)
When `allowJs` is `true`, `ts-jest` will process `js` files and also perform type checking. However, since default behavior of TypeScript is not doing type checking on `js` files (`checkJs` default value is `false`), we should respect that behavior. BREAKING CHANGE If one currently relies on type check for `js` files, please set `checkJs: true` in your tsconfig
1 parent d80be39 commit 1e04433

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed
 

‎src/config/config-set.spec.ts

+39-4
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,48 @@ describe('raiseDiagnostics', () => {
549549
}) // raiseDiagnostics
550550

551551
describe('shouldReportDiagnostics', () => {
552-
it('should return correct value', () => {
553-
let cs = createConfigSet({ tsJestConfig: { tsconfig: false, diagnostics: { pathRegex: '/foo/' } } as any })
552+
it('should return correct value for ts/tsx files', () => {
553+
let cs = createConfigSet({
554+
tsJestConfig: {
555+
tsconfig: false,
556+
diagnostics: { pathRegex: '/foo/' },
557+
} as any,
558+
})
559+
554560
expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
555-
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(false)
561+
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false)
562+
556563
cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any })
564+
557565
expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
558-
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(true)
566+
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(true)
567+
})
568+
569+
test('should return correct value for js/jsx files with checkJs compiler option', () => {
570+
let cs = createConfigSet({
571+
tsJestConfig: {
572+
tsconfig: { checkJs: false },
573+
diagnostics: { pathRegex: '/foo/' },
574+
},
575+
})
576+
577+
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false)
578+
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false)
579+
580+
cs = createConfigSet({
581+
tsJestConfig: {
582+
tsconfig: { checkJs: true },
583+
diagnostics: { pathRegex: '/foo/' },
584+
},
585+
})
586+
587+
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true)
588+
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true)
589+
590+
cs = createConfigSet({ tsJestConfig: { tsconfig: { checkJs: true } } })
591+
592+
expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true)
593+
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true)
559594
})
560595
}) // shouldReportDiagnostics
561596

‎src/config/config-set.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
ScriptTarget,
2727
} from 'typescript'
2828

29-
import { DEFAULT_JEST_TEST_MATCH } from '../constants'
29+
import { DEFAULT_JEST_TEST_MATCH, JS_JSX_EXTENSIONS } from '../constants'
3030
import { factory as hoisting } from '../transformers/hoist-jest'
3131
import type {
3232
AstTransformer,
@@ -528,12 +528,14 @@ export class ConfigSet {
528528

529529
shouldReportDiagnostics(filePath: string): boolean {
530530
const { pathRegex } = this._diagnostics
531+
const fileExtension = extname(filePath)
532+
const { checkJs } = this.parsedTsConfig.options
531533
if (pathRegex) {
532534
const regex = new RegExp(pathRegex)
533535

534-
return regex.test(filePath)
536+
return JS_JSX_EXTENSIONS.includes(fileExtension) ? checkJs && regex.test(filePath) : regex.test(filePath)
535537
} else {
536-
return true
538+
return JS_JSX_EXTENSIONS.includes(fileExtension) ? checkJs : true
537539
}
538540
}
539541

‎src/constants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export const LINE_FEED = '\n'
2+
export const DECLARATION_TYPE_EXT = '.d.ts'
3+
export const JS_JSX_EXTENSIONS = ['.js', '.jsx']
24
export const TS_TSX_REGEX = /\.tsx?$/
35
export const JS_JSX_REGEX = /\.jsx?$/
4-
export const DECLARATION_TYPE_EXT = '.d.ts'
56
// `extensionsToTreatAsEsm` only accepts `.ts`, `.tsx` and `.jsx`. `.js`, `.cjs`, `.mjs` will throw error
67
export const TS_EXT_TO_TREAT_AS_ESM = ['.ts', '.tsx']
78
export const JS_EXT_TO_TREAT_AS_ESM = ['.jsx']

0 commit comments

Comments
 (0)
Please sign in to comment.