Skip to content

Commit

Permalink
feat: add typings for ts-jest options via transform config (#3805)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl authored Sep 11, 2022
1 parent 76c9095 commit 664b0f2
Show file tree
Hide file tree
Showing 42 changed files with 78 additions and 85 deletions.
2 changes: 1 addition & 1 deletion e2e/ast-transformers/hoist-jest/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
automock: true,
moduleNameMapper: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.[tj]sx?$': [
Expand Down
4 changes: 2 additions & 2 deletions e2e/config-typing/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { InitialOptionsTsJest } from 'ts-jest'
import type { JestConfigWithTsJest } from '../../dist'

const jestCfg: InitialOptionsTsJest = {
const jestCfg: JestConfigWithTsJest = {
transform: {
'^.+.tsx?$': [
'ts-jest',
Expand Down
2 changes: 1 addition & 1 deletion e2e/const-enum/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.tsx?$': [
Expand Down
2 changes: 1 addition & 1 deletion e2e/coverage-report/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
collectCoverageFrom: ['**/*.js', '**/*.ts', '!**/node_modules/**', '!**/coverage/**'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion e2e/diagnostics/jest-disabled.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.tsx?$': [
Expand Down
2 changes: 1 addition & 1 deletion e2e/diagnostics/jest-exclude.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.tsx?$': [
Expand Down
2 changes: 1 addition & 1 deletion e2e/diagnostics/jest-ignored-code.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.tsx?$': [
Expand Down
2 changes: 1 addition & 1 deletion e2e/diagnostics/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.tsx?$': [
Expand Down
2 changes: 1 addition & 1 deletion e2e/diagnostics/jest-warn.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.tsx?$': [
Expand Down
2 changes: 1 addition & 1 deletion e2e/enum/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
transform: {
'^.+.tsx?$': [
Expand Down
2 changes: 1 addition & 1 deletion e2e/native-esm-ts/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
extensionsToTreatAsEsm: ['.ts'],
resolver: '<rootDir>/mjs-resolver.ts',
Expand Down
2 changes: 1 addition & 1 deletion e2e/source-map/disabled/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'sourcemap-disabled-isolated',
roots: ['<rootDir>', '<rootDir>/../__tests__'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/source-map/disabled/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'sourcemap-disabled',
roots: ['<rootDir>', '<rootDir>/../__tests__'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/source-map/enabled/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'sourcemap-enabled-isolated',
roots: ['<rootDir>', '<rootDir>/../__tests__'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/source-map/enabled/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'sourcemap-enabled',
roots: ['<rootDir>', '<rootDir>/../__tests__'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform-js/babel-cjs-file/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'babel-cjs-file',
roots: ['<rootDir>', '<rootDir>/../__tests__/for-babel'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform-js/babel-enabled/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'babel-enabled',
roots: ['<rootDir>', '<rootDir>/../__tests__/for-babel'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform-js/babel-js-file/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'babel-js-file',
roots: ['<rootDir>', '<rootDir>/../__tests__/for-babel'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform-js/no-type-check/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'no-type-check',
roots: ['<rootDir>', '<rootDir>/../__tests__/for-ts'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform-js/type-check/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').JestConfigWithTsJest} */
module.exports = {
displayName: 'type-check',
roots: ['<rootDir>', '<rootDir>/../__tests__/for-ts'],
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform-tsx/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../dist').InitialOptionsTsJest} */
/** @type {import('../../dist').JestConfigWithTsJest} */
module.exports = {
testEnvironment: 'jsdom',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-babel/jest-esm-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-babel-esm',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-babel/jest-esm.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-babel-esm',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-babel/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-babel',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-babel/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-babel',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-ts/jest-esm-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-ts-esm',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-ts/jest-esm.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-ts-esm',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-ts/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/js-with-ts/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
}
2 changes: 1 addition & 1 deletion examples/react-app/jest-esm-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const baseEsmCfg = require('./jest-esm.config')
const baseCfg = require('./jest.config')

/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
...baseEsmCfg,
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/react-app/jest-esm.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const baseCfg = require('./jest.config')

/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
...baseCfg,
preset: 'ts-jest/presets/js-with-babel-esm',
Expand Down
2 changes: 1 addition & 1 deletion examples/react-app/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const baseCfg = require('./jest.config')

/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
...baseCfg,
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-only/jest-esm-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/default-esm',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-only/jest-esm.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/default-esm',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-only/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
transform: {
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-only/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('./dist').InitialOptionsTsJest} */
/** @type {import('./dist').JestConfigWithTsJest} */
module.exports = {
collectCoverageFrom: ['src/**/*.ts'],
modulePathIgnorePatterns: ['examples/.*', 'website/.*'],
Expand Down
9 changes: 8 additions & 1 deletion src/legacy/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import type {
BabelJestTransformer,
ProjectConfigTsJest,
TsJestAstTransformer,
TsJestDiagnosticsCfg,
TsJestGlobalOptions,
TTypeScript,
} from '../../types'
Expand All @@ -40,6 +39,14 @@ import { normalizeSlashes } from '../../utils/normalize-slashes'
import { sha1 } from '../../utils/sha1'
import { TSError } from '../../utils/ts-error'

interface TsJestDiagnosticsCfg {
pretty: boolean
ignoreCodes: number[]
exclude: string[]
throws: boolean
warnOnly?: boolean
}

/**
* @internal
*/
Expand Down
66 changes: 26 additions & 40 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ declare module '@jest/types' {
* @internal
*/
export type TBabelCore = typeof _babel
/**
* @internal
*/
export type TBabelJest = typeof babelJest
export type TTypeScript = typeof _ts
// Stimulate `esbuild` type to avoid import `esbuild` while building the assets which are shipped to npm
/**
* @internal
*/
export interface TEsBuild {
transformSync(
input: string,
Expand Down Expand Up @@ -62,6 +68,9 @@ export interface ConfigCustomTransformer {
afterDeclarations?: Array<string | AstTransformer>
}

/**
* @deprecated use `TsJestTransformerOptions` instead
*/
export interface TsJestGlobalOptions {
/**
* Compiler options. It can be:
Expand Down Expand Up @@ -156,67 +165,44 @@ export interface TsJestGlobalOptions {
useESM?: boolean
}

interface TsJestConfig$tsConfig$file {
kind: 'file'
value: string | undefined
}
interface TsJestConfig$tsConfig$inline {
kind: 'inline'
value: _ts.CompilerOptions
}
type TsJestConfig$tsConfig = TsJestConfig$tsConfig$file | TsJestConfig$tsConfig$inline | undefined
export interface TsJestDiagnosticsCfg {
pretty: boolean
ignoreCodes: number[]
exclude: string[]
throws: boolean
warnOnly?: boolean
}
interface TsJestConfig$babelConfig$file {
kind: 'file'
value: string | undefined
}
interface TsJestConfig$babelConfig$inline {
kind: 'inline'
value: BabelConfig
}
type TsJestConfig$babelConfig = TsJestConfig$babelConfig$file | TsJestConfig$babelConfig$inline | undefined
/**
* @internal
*/
export interface TsJestConfig {
tsconfig: TsJestConfig$tsConfig
isolatedModules: boolean
compiler: string
diagnostics: TsJestDiagnosticsCfg
babelConfig: TsJestConfig$babelConfig
transformers: ConfigCustomTransformer
// to deprecate / deprecated === === ===
stringifyContentPathRegex: string | undefined
}

/**
* For transformers which extends `ts-jest`
* @deprecated use `JestConfigWithTsJest` instead
*/
export interface ProjectConfigTsJest extends Config.ProjectConfig {
globals: GlobalConfigTsJest
}
/**
* @deprecated use `JestConfigWithTsJest` instead
*/
export interface TransformOptionsTsJest extends TransformOptions {
config: ProjectConfigTsJest
}

/**
* For typings in `jest.config.ts`
* @deprecated use `JestConfigWithTsJest` instead
*/
export interface GlobalConfigTsJest extends Config.ConfigGlobals {
'ts-jest': TsJestGlobalOptions
}
/**
* @deprecated use `JestConfigWithTsJest` instead
*/
export interface InitialOptionsTsJest extends Config.InitialOptions {
globals?: GlobalConfigTsJest
}
type TsJestTransformerOptions = TsJestGlobalOptions
export interface JestConfigWithTsJest extends Partial<Omit<Config.ProjectConfig, 'transform'>> {
transform: {
[regex: string]: 'ts-jest' | ['ts-jest', TsJestTransformerOptions] | string | [string, Record<string, unknown>]
}
}

export type StringMap = Map<string, string>

/**
* @internal
*/
export interface DepGraphInfo {
fileContent: string
resolvedModuleNames: string[]
Expand Down
6 changes: 3 additions & 3 deletions website/docs/getting-started/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = {
To utilize IDE suggestions, you can use `JSDoc` comments to provide suggested `ts-jest` configs for your Jest config:

```js
/** @type {import('ts-jest').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = config = {
// [...]
transform: {
Expand All @@ -67,9 +67,9 @@ Or through TypeScript (if `ts-node` is installed):

```ts
// jest.config.ts
import type { InitialOptionsTsJest } from 'ts-jest'
import type { JestConfigWithTsJest } from 'ts-jest'

const config: InitialOptionsTsJest = {
const config: JestConfigWithTsJest = {
transform: {
'<regex_match_files>': [
'ts-jest',
Expand Down
Loading

0 comments on commit 664b0f2

Please sign in to comment.