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

fix: jest 25 type definitions #1363

Merged
merged 1 commit into from Jan 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions e2e/__helpers__/test-case/processed-file-io.ts
@@ -1,12 +1,15 @@
import { TransformOptions, TransformedSource } from '@jest/transform/build/types'
Copy link
Contributor

@SimenB SimenB Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please send PRs for any types you need that are not exported at the top level of the module. Reaching into build/ is not supported and can break at any time

Copy link
Collaborator

@ahnpnl ahnpnl Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean FR and PRs ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull requests 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, we already export TransformOptions at least. TransformedSource is not exported though (ReturnType<Transformer['process']> is clunky), so PR for that is welcome

https://www.runpkg.com/?@jest/transform@25.2.4/build/index.d.ts

import { Config } from '@jest/types'

import ProcessedSource from '../../../src/__helpers__/processed-source'

// tslint:disable-next-line:no-default-export
export default class ProcessedFileIo extends ProcessedSource {
constructor(
cwd: string,
filename: string,
readonly input: [string, jest.Path, jest.ProjectConfig, jest.TransformOptions],
output: string | jest.TransformedSource,
readonly input: [string, Config.Path, Config.ProjectConfig, TransformOptions],
output: string | TransformedSource,
) {
super(typeof output === 'string' ? output : output.code, filename, cwd)
}
Expand Down
3 changes: 2 additions & 1 deletion e2e/__helpers__/test-case/run-result.ts
@@ -1,3 +1,4 @@
import { Config } from '@jest/types'
import { LogMessage } from 'bs-logger'
import { SpawnSyncReturns } from 'child_process'
import { readFileSync, realpathSync } from 'fs'
Expand All @@ -19,7 +20,7 @@ export default class RunResult {
cmd: string
args: string[]
env: { [key: string]: string }
config: jest.InitialOptions
config: Config.InitialOptions
digest: string
}>,
) {}
Expand Down
5 changes: 3 additions & 2 deletions e2e/__helpers__/test-case/runtime.ts
@@ -1,3 +1,4 @@
import { Config } from '@jest/types'
import { sync as spawnSync } from 'cross-spawn'
import { createHash } from 'crypto'
import stringifyJson = require('fast-json-stable-stringify')
Expand Down Expand Up @@ -64,10 +65,10 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
const jestConfigPath = (path: string = dir) => resolve(path, configFile)

// grab base configuration
let baseConfig: jest.InitialOptions = require(jestConfigPath())
let baseConfig: Config.InitialOptions = require(jestConfigPath())
if (configFile === 'package.json') baseConfig = (baseConfig as any).jest

const extraConfig = {} as jest.InitialOptions
const extraConfig = {} as Config.InitialOptions

let shortCmd: string
let cmdArgs: string[] = []
Expand Down
4 changes: 3 additions & 1 deletion e2e/__helpers__/test-case/types.ts
@@ -1,3 +1,5 @@
import { Config } from '@jest/types'

import { TsJestConfig } from '../../../src/types'

import RunResult from './run-result'
Expand All @@ -7,7 +9,7 @@ export interface RunTestOptions {
env?: {}
inject?: (() => any) | string
writeIo?: boolean
jestConfig?: jest.ProjectConfig | any
jestConfig?: Config.ProjectConfig | any
tsJestConfig?: TsJestConfig | any
noCache?: boolean
jestConfigPath?: string
Expand Down
11 changes: 7 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -82,11 +82,13 @@
"devDependencies": {
"@commitlint/cli": "8.x",
"@commitlint/config-conventional": "7.x",
"@jest/transform": "25.x",
"@jest/types": "25.x",
"@types/babel__core": "7.x",
"@types/buffer-from": "latest",
"@types/cross-spawn": "latest",
"@types/fs-extra": "latest",
"@types/jest": "23.x",
"@types/jest": "25.x",
kulshekhar marked this conversation as resolved.
Show resolved Hide resolved
"@types/js-yaml": "latest",
"@types/json5": "latest",
"@types/lodash.memoize": "4.x",
Expand Down
5 changes: 3 additions & 2 deletions src/__helpers__/fakers.ts
@@ -1,3 +1,4 @@
import { Config } from '@jest/types'
import { resolve } from 'path'

import { BabelConfig, TsJestConfig, TsJestGlobalOptions } from '../types'
Expand Down Expand Up @@ -67,8 +68,8 @@ export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
}
}

export function jestConfig<T extends jest.ProjectConfig>(
options?: Partial<jest.InitialOptions | jest.ProjectConfig>,
export function jestConfig<T extends Config.ProjectConfig>(
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
tsJestOptions?: TsJestGlobalOptions,
): T {
const res = {
Expand Down
5 changes: 3 additions & 2 deletions src/cli/config/migrate.ts
@@ -1,3 +1,4 @@
import { Config } from '@jest/types'
import { createLogger } from 'bs-logger'
import * as stringifyJson from 'fast-json-stable-stringify'
import { existsSync } from 'fs'
Expand Down Expand Up @@ -25,7 +26,7 @@ export const run: CliCommand = async (args: Arguments /*, logger: Logger*/) => {
if (!/\.(js|json)$/.test(name)) {
throw new TypeError(`Configuration file ${file} must be a JavaScript or JSON file.`)
}
let actualConfig: jest.InitialOptions = require(filePath)
let actualConfig: Config.InitialOptions = require(filePath)
if (isPackage) {
actualConfig = (actualConfig as any).jest
}
Expand Down Expand Up @@ -173,7 +174,7 @@ ${footNotes.join('\n')}
}
}

function cleanupConfig(config: jest.InitialOptions): void {
function cleanupConfig(config: Config.InitialOptions): void {
if (config.globals) {
if ((config as any).globals['ts-jest'] && Object.keys((config as any).globals['ts-jest']).length === 0) {
delete (config as any).globals['ts-jest']
Expand Down
3 changes: 2 additions & 1 deletion src/compiler.spec.ts
@@ -1,4 +1,5 @@
// tslint:disable:max-line-length
import { Config } from '@jest/types'
import { LogLevels } from 'bs-logger'
import { removeSync, writeFileSync } from 'fs-extra'

Expand All @@ -18,7 +19,7 @@ function makeCompiler({
tsJestConfig,
parentConfig,
}: {
jestConfig?: Partial<jest.ProjectConfig>
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
parentConfig?: TsJestGlobalOptions
} = {}) {
Expand Down
6 changes: 4 additions & 2 deletions src/config/config-set.spec.ts
@@ -1,3 +1,5 @@
import { Transformer } from '@jest/transform/build/types'
import { Config } from '@jest/types'
import { testing } from 'bs-logger'
import { resolve } from 'path'
import { Diagnostic, DiagnosticCategory, ModuleKind, ParsedCommandLine, ScriptTarget } from 'typescript'
Expand Down Expand Up @@ -35,7 +37,7 @@ function createConfigSet({
resolve = defaultResolve,
...others
}: {
jestConfig?: jest.ProjectConfig
jestConfig?: Config.ProjectConfig
tsJestConfig?: TsJestGlobalOptions
parentConfig?: TsJestGlobalOptions
resolve?: ((path: string) => string) | null
Expand Down Expand Up @@ -543,7 +545,7 @@ describe('babelJestTransformer', () => {
let cs = createConfigSet({ tsJestConfig: { tsConfig: false } as any })
expect(cs.babelJestTransformer).toBeUndefined()
cs = createConfigSet({ tsJestConfig: { tsConfig: false, babelConfig: {} } as any })
const babelJest = cs.babelJestTransformer as jest.Transformer
const babelJest = cs.babelJestTransformer as Transformer
expect(babelJest.canInstrument).toBe(true)
expect(babelJest.createTransformer).toBeUndefined()
expect(typeof babelJest.getCacheKey).toBe('function')
Expand Down
7 changes: 4 additions & 3 deletions src/config/config-set.ts
Expand Up @@ -8,6 +8,7 @@
* version of the `jest.ProjectConfig`, and then later it calls `process()`
* with the complete, object version of it.
*/
import { Config } from '@jest/types'
import { LogContexts, Logger } from 'bs-logger'
import { existsSync, readFileSync, realpathSync } from 'fs'
import json5 = require('json5')
Expand Down Expand Up @@ -165,7 +166,7 @@ export class ConfigSet {
}

@Memoize()
get jest(): jest.ProjectConfig {
get jest(): Config.ProjectConfig {
const config = backportJestConfig(this.logger, this._jestConfig)
if (this.parentOptions) {
const globals: any = config.globals || (config.globals = {})
Expand Down Expand Up @@ -622,9 +623,9 @@ export class ConfigSet {
/**
* @internal
*/
private readonly _jestConfig: jest.ProjectConfig
private readonly _jestConfig: Config.ProjectConfig

constructor(jestConfig: jest.ProjectConfig, readonly parentOptions?: TsJestGlobalOptions, parentLogger?: Logger) {
constructor(jestConfig: Config.ProjectConfig, readonly parentOptions?: TsJestGlobalOptions, parentLogger?: Logger) {
this._jestConfig = jestConfig
this.logger = parentLogger ? parentLogger.child({ [LogContexts.namespace]: 'config' }) : logger
}
Expand Down
6 changes: 4 additions & 2 deletions src/config/create-jest-preset.ts
@@ -1,9 +1,11 @@
import { Config } from '@jest/types'

import { rootLogger } from '../util/logger'

const logger = rootLogger.child({ namespace: 'jest-preset' })

export interface TsJestPresets {
transform: Record<string, string>
transform: Config.InitialOptions['transform']
testMatch?: string[]
moduleFileExtensions?: string[]
}
Expand All @@ -14,7 +16,7 @@ export interface CreateJestPresetOptions {

export function createJestPreset(
{ allowJs = false }: CreateJestPresetOptions = {},
from: jest.InitialOptions = {},
from: Config.InitialOptions = {},
): TsJestPresets {
logger.debug({ allowJs }, 'creating jest presets', allowJs ? 'handling' : 'not handling', 'JavaScript files')
return {
Expand Down
3 changes: 2 additions & 1 deletion src/config/paths-to-module-name-mapper.ts
@@ -1,11 +1,12 @@
import { Config } from '@jest/types'
import { LogContexts } from 'bs-logger'
import { CompilerOptions } from 'typescript'

import { rootLogger } from '../util/logger'
import { Errors, interpolate } from '../util/messages'

type TsPathMapping = Exclude<CompilerOptions['paths'], undefined>
type JestPathMapping = jest.InitialOptions['moduleNameMapper']
type JestPathMapping = Config.InitialOptions['moduleNameMapper']

// we don't need to escape all chars, so commented out is the real one
// const escapeRegex = (str: string) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
Expand Down
3 changes: 2 additions & 1 deletion src/ts-jest-transformer.spec.ts
@@ -1,3 +1,4 @@
import { Config } from '@jest/types'
import stringify = require('fast-json-stable-stringify')
import { sep } from 'path'
import { ParsedCommandLine } from 'typescript'
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('process', () => {
}
const INPUT = 'export default "foo"'
const FILE = '/foo/bar.ts'
const JEST_CONFIG = {} as jest.ProjectConfig
const JEST_CONFIG = {} as Config.ProjectConfig
const OPTIONS = { instrument: false }
const process = () => tr.process(...args)
beforeEach(() => {
Expand Down
20 changes: 11 additions & 9 deletions src/ts-jest-transformer.ts
@@ -1,3 +1,5 @@
import { TransformOptions, TransformedSource, Transformer } from '@jest/transform/build/types'
import { Config } from '@jest/types'
import { Logger } from 'bs-logger'
import { inspect } from 'util'

Expand All @@ -16,10 +18,10 @@ export const INSPECT_CUSTOM = inspect.custom || 'inspect'

interface ConfigSetIndexItem {
configSet: ConfigSet
jestConfig: JsonableValue<jest.ProjectConfig>
jestConfig: JsonableValue<Config.ProjectConfig>
}

export class TsJestTransformer implements jest.Transformer {
export class TsJestTransformer implements Transformer {
/**
* @internal
*/
Expand Down Expand Up @@ -60,9 +62,9 @@ export class TsJestTransformer implements jest.Transformer {
return `[object TsJestTransformer<#${this.id}>]`
}

configsFor(jestConfig: jest.ProjectConfig | string) {
configsFor(jestConfig: Config.ProjectConfig | string) {
let csi: ConfigSetIndexItem | undefined
let jestConfigObj: jest.ProjectConfig
let jestConfigObj: Config.ProjectConfig
if (typeof jestConfig === 'string') {
csi = TsJestTransformer._configSetsIndex.find(cs => cs.jestConfig.serialized === jestConfig)
if (csi) return csi.configSet
Expand Down Expand Up @@ -95,12 +97,12 @@ export class TsJestTransformer implements jest.Transformer {

process(
input: string,
filePath: jest.Path,
jestConfig: jest.ProjectConfig,
transformOptions?: jest.TransformOptions,
): jest.TransformedSource | string {
filePath: Config.Path,
jestConfig: Config.ProjectConfig,
transformOptions?: TransformOptions,
): TransformedSource | string {
this.logger.debug({ fileName: filePath, transformOptions }, 'processing', filePath)
let result: string | jest.TransformedSource
let result: string | TransformedSource
const source: string = input

const configs = this.configsFor(jestConfig)
Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
@@ -1,3 +1,4 @@
import { TransformedSource, Transformer } from '@jest/transform/build/types'
import * as _babel from 'babel__core'
import { CompilerOptions, SourceFile, TransformerFactory } from 'typescript'
// tslint:disable-next-line:no-duplicate-imports
Expand All @@ -7,9 +8,9 @@ import { ConfigSet } from './config/config-set'

export type TBabelCore = typeof _babel
export type TTypeScript = typeof _ts
export type TBabelJest = Required<jest.Transformer>
export type TBabelJest = Required<Transformer>
export type BabelJestTransformer = {
[K in Exclude<keyof jest.Transformer, 'createTransformer'>]: Exclude<jest.Transformer[K], undefined>
[K in Exclude<keyof Transformer, 'createTransformer'>]: Exclude<Transformer[K], undefined>
}
export type BabelConfig = _babel.TransformOptions

Expand Down Expand Up @@ -128,7 +129,7 @@ export interface TsJestConfig {
}

export interface TsJestHooksMap {
afterProcess?(args: any[], result: string | jest.TransformedSource): string | jest.TransformedSource | void
afterProcess?(args: any[], result: string | TransformedSource): string | TransformedSource | void
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/util/backports.ts
@@ -1,3 +1,4 @@
import { Config } from '@jest/types'
import { LogContexts, Logger } from 'bs-logger'

import { Deprecateds, Helps, interpolate } from './messages'
Expand All @@ -7,7 +8,7 @@ const context = { [LogContexts.namespace]: 'backports' }
/**
* @internal
*/
export const backportJestConfig = <T extends jest.InitialOptions | jest.ProjectConfig>(
export const backportJestConfig = <T extends Config.InitialOptions | Config.ProjectConfig>(
logger: Logger,
config: T,
): T => {
Expand Down