Navigation Menu

Skip to content

Commit

Permalink
feat(config): type checking js files based on checkJs (#2283)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ahnpnl committed Jan 15, 2021
1 parent d80be39 commit 1e04433
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
43 changes: 39 additions & 4 deletions src/config/config-set.spec.ts
Expand Up @@ -549,13 +549,48 @@ describe('raiseDiagnostics', () => {
}) // raiseDiagnostics

describe('shouldReportDiagnostics', () => {
it('should return correct value', () => {
let cs = createConfigSet({ tsJestConfig: { tsconfig: false, diagnostics: { pathRegex: '/foo/' } } as any })
it('should return correct value for ts/tsx files', () => {
let cs = createConfigSet({
tsJestConfig: {
tsconfig: false,
diagnostics: { pathRegex: '/foo/' },
} as any,
})

expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(false)
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(false)

cs = createConfigSet({ tsJestConfig: { tsconfig: false } as any })

expect(cs.shouldReportDiagnostics('/foo/index.ts')).toBe(true)
expect(cs.shouldReportDiagnostics('/bar/index.ts')).toBe(true)
expect(cs.shouldReportDiagnostics('/bar/index.tsx')).toBe(true)
})

test('should return correct value for js/jsx files with checkJs compiler option', () => {
let cs = createConfigSet({
tsJestConfig: {
tsconfig: { checkJs: false },
diagnostics: { pathRegex: '/foo/' },
},
})

expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(false)
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(false)

cs = createConfigSet({
tsJestConfig: {
tsconfig: { checkJs: true },
diagnostics: { pathRegex: '/foo/' },
},
})

expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true)
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true)

cs = createConfigSet({ tsJestConfig: { tsconfig: { checkJs: true } } })

expect(cs.shouldReportDiagnostics('/foo/index.js')).toBe(true)
expect(cs.shouldReportDiagnostics('/foo/index.jsx')).toBe(true)
})
}) // shouldReportDiagnostics

Expand Down
8 changes: 5 additions & 3 deletions src/config/config-set.ts
Expand Up @@ -26,7 +26,7 @@ import {
ScriptTarget,
} from 'typescript'

import { DEFAULT_JEST_TEST_MATCH } from '../constants'
import { DEFAULT_JEST_TEST_MATCH, JS_JSX_EXTENSIONS } from '../constants'
import { factory as hoisting } from '../transformers/hoist-jest'
import type {
AstTransformer,
Expand Down Expand Up @@ -528,12 +528,14 @@ export class ConfigSet {

shouldReportDiagnostics(filePath: string): boolean {
const { pathRegex } = this._diagnostics
const fileExtension = extname(filePath)
const { checkJs } = this.parsedTsConfig.options
if (pathRegex) {
const regex = new RegExp(pathRegex)

return regex.test(filePath)
return JS_JSX_EXTENSIONS.includes(fileExtension) ? checkJs && regex.test(filePath) : regex.test(filePath)
} else {
return true
return JS_JSX_EXTENSIONS.includes(fileExtension) ? checkJs : true
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
@@ -1,7 +1,8 @@
export const LINE_FEED = '\n'
export const DECLARATION_TYPE_EXT = '.d.ts'
export const JS_JSX_EXTENSIONS = ['.js', '.jsx']
export const TS_TSX_REGEX = /\.tsx?$/
export const JS_JSX_REGEX = /\.jsx?$/
export const DECLARATION_TYPE_EXT = '.d.ts'
// `extensionsToTreatAsEsm` only accepts `.ts`, `.tsx` and `.jsx`. `.js`, `.cjs`, `.mjs` will throw error
export const TS_EXT_TO_TREAT_AS_ESM = ['.ts', '.tsx']
export const JS_EXT_TO_TREAT_AS_ESM = ['.jsx']
Expand Down

0 comments on commit 1e04433

Please sign in to comment.