Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): type checking js files based on checkJs #2283

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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