From ba82a9e8828023df10a8dc303b0e1aadb3f375b2 Mon Sep 17 00:00:00 2001 From: Andrew Leedham Date: Tue, 28 Jan 2020 10:17:34 +0000 Subject: [PATCH] fix: jest 25 type definitions A lot of the Jest typings were moved into the core repo rather than the `@types/jest` definitions. See https://github.com/DefinitelyTyped/DefinitelyTyped/commit/ef7406af8cb8c9f89db41518358806a6177bdeed --- .../test-case/processed-file-io.ts | 7 +++++-- e2e/__helpers__/test-case/run-result.ts | 3 ++- e2e/__helpers__/test-case/runtime.ts | 5 +++-- e2e/__helpers__/test-case/types.ts | 4 +++- package-lock.json | 11 ++++++---- package.json | 4 +++- src/__helpers__/fakers.ts | 5 +++-- src/cli/config/migrate.ts | 5 +++-- src/compiler.spec.ts | 3 ++- src/config/config-set.spec.ts | 6 ++++-- src/config/config-set.ts | 7 ++++--- src/config/create-jest-preset.ts | 6 ++++-- src/config/paths-to-module-name-mapper.ts | 3 ++- src/ts-jest-transformer.spec.ts | 3 ++- src/ts-jest-transformer.ts | 20 ++++++++++--------- src/types.ts | 7 ++++--- src/util/backports.ts | 3 ++- 17 files changed, 64 insertions(+), 38 deletions(-) diff --git a/e2e/__helpers__/test-case/processed-file-io.ts b/e2e/__helpers__/test-case/processed-file-io.ts index 2efa006b3b..28415ea94f 100644 --- a/e2e/__helpers__/test-case/processed-file-io.ts +++ b/e2e/__helpers__/test-case/processed-file-io.ts @@ -1,3 +1,6 @@ +import { TransformOptions, TransformedSource } from '@jest/transform/build/types' +import { Config } from '@jest/types' + import ProcessedSource from '../../../src/__helpers__/processed-source' // tslint:disable-next-line:no-default-export @@ -5,8 +8,8 @@ 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) } diff --git a/e2e/__helpers__/test-case/run-result.ts b/e2e/__helpers__/test-case/run-result.ts index a8202c30b8..b434fc1cca 100644 --- a/e2e/__helpers__/test-case/run-result.ts +++ b/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' @@ -19,7 +20,7 @@ export default class RunResult { cmd: string args: string[] env: { [key: string]: string } - config: jest.InitialOptions + config: Config.InitialOptions digest: string }>, ) {} diff --git a/e2e/__helpers__/test-case/runtime.ts b/e2e/__helpers__/test-case/runtime.ts index 280f9188bb..718c9e5aa6 100644 --- a/e2e/__helpers__/test-case/runtime.ts +++ b/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') @@ -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[] = [] diff --git a/e2e/__helpers__/test-case/types.ts b/e2e/__helpers__/test-case/types.ts index 459ada4fe5..fc6028af03 100644 --- a/e2e/__helpers__/test-case/types.ts +++ b/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' @@ -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 diff --git a/package-lock.json b/package-lock.json index 43eec1a8d9..acae16793a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1351,10 +1351,13 @@ } }, "@types/jest": { - "version": "23.3.14", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-23.3.14.tgz", - "integrity": "sha512-Q5hTcfdudEL2yOmluA1zaSyPbzWPmJ3XfSWeP3RyoYvS9hnje1ZyagrZOuQ6+1nQC1Gw+7gap3pLNL3xL6UBug==", - "dev": true + "version": "25.1.0", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-25.1.0.tgz", + "integrity": "sha512-MYX8LpNQboef1NDZQTchM5poL8WITRVG/4/1XLK/Xsamjptsvgb8ELTRoXixBARi+a81mMT4n2hooqaydEOE9A==", + "dev": true, + "requires": { + "jest-diff": "^25.1.0" + } }, "@types/js-yaml": { "version": "3.12.2", diff --git a/package.json b/package.json index 43e72c7fad..f8b3d26925 100644 --- a/package.json +++ b/package.json @@ -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", "@types/js-yaml": "latest", "@types/json5": "latest", "@types/lodash.memoize": "4.x", diff --git a/src/__helpers__/fakers.ts b/src/__helpers__/fakers.ts index 0628f2d540..a803c481ac 100644 --- a/src/__helpers__/fakers.ts +++ b/src/__helpers__/fakers.ts @@ -1,3 +1,4 @@ +import { Config } from '@jest/types' import { resolve } from 'path' import { BabelConfig, TsJestConfig, TsJestGlobalOptions } from '../types' @@ -67,8 +68,8 @@ export function tsJestConfig(options?: Partial): TsJestConfig { } } -export function jestConfig( - options?: Partial, +export function jestConfig( + options?: Partial, tsJestOptions?: TsJestGlobalOptions, ): T { const res = { diff --git a/src/cli/config/migrate.ts b/src/cli/config/migrate.ts index 7c48ac0bc7..fe824c07e6 100644 --- a/src/cli/config/migrate.ts +++ b/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' @@ -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 } @@ -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'] diff --git a/src/compiler.spec.ts b/src/compiler.spec.ts index 5618f94fd9..4217605c88 100644 --- a/src/compiler.spec.ts +++ b/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' @@ -18,7 +19,7 @@ function makeCompiler({ tsJestConfig, parentConfig, }: { - jestConfig?: Partial + jestConfig?: Partial tsJestConfig?: TsJestGlobalOptions parentConfig?: TsJestGlobalOptions } = {}) { diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 27b6b342b9..1fc47ac7fe 100644 --- a/src/config/config-set.spec.ts +++ b/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' @@ -35,7 +37,7 @@ function createConfigSet({ resolve = defaultResolve, ...others }: { - jestConfig?: jest.ProjectConfig + jestConfig?: Config.ProjectConfig tsJestConfig?: TsJestGlobalOptions parentConfig?: TsJestGlobalOptions resolve?: ((path: string) => string) | null @@ -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') diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 27a302f1ae..0d3144969c 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -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') @@ -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 = {}) @@ -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 } diff --git a/src/config/create-jest-preset.ts b/src/config/create-jest-preset.ts index 1d63815e6e..bfeec6c035 100644 --- a/src/config/create-jest-preset.ts +++ b/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 + transform: Config.InitialOptions['transform'] testMatch?: string[] moduleFileExtensions?: string[] } @@ -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 { diff --git a/src/config/paths-to-module-name-mapper.ts b/src/config/paths-to-module-name-mapper.ts index fc17a9962d..f1b1338c8c 100644 --- a/src/config/paths-to-module-name-mapper.ts +++ b/src/config/paths-to-module-name-mapper.ts @@ -1,3 +1,4 @@ +import { Config } from '@jest/types' import { LogContexts } from 'bs-logger' import { CompilerOptions } from 'typescript' @@ -5,7 +6,7 @@ import { rootLogger } from '../util/logger' import { Errors, interpolate } from '../util/messages' type TsPathMapping = Exclude -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, '\\$&') diff --git a/src/ts-jest-transformer.spec.ts b/src/ts-jest-transformer.spec.ts index ace13901a2..c0cc3e4209 100644 --- a/src/ts-jest-transformer.spec.ts +++ b/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' @@ -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(() => { diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index 8403c2fcf7..30a51f2ce5 100644 --- a/src/ts-jest-transformer.ts +++ b/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' @@ -16,10 +18,10 @@ export const INSPECT_CUSTOM = inspect.custom || 'inspect' interface ConfigSetIndexItem { configSet: ConfigSet - jestConfig: JsonableValue + jestConfig: JsonableValue } -export class TsJestTransformer implements jest.Transformer { +export class TsJestTransformer implements Transformer { /** * @internal */ @@ -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 @@ -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) diff --git a/src/types.ts b/src/types.ts index d8f9fb7f46..53fbfe6563 100644 --- a/src/types.ts +++ b/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 @@ -7,9 +8,9 @@ import { ConfigSet } from './config/config-set' export type TBabelCore = typeof _babel export type TTypeScript = typeof _ts -export type TBabelJest = Required +export type TBabelJest = Required export type BabelJestTransformer = { - [K in Exclude]: Exclude + [K in Exclude]: Exclude } export type BabelConfig = _babel.TransformOptions @@ -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 } /** diff --git a/src/util/backports.ts b/src/util/backports.ts index 22f4d58efa..66530daf25 100644 --- a/src/util/backports.ts +++ b/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' @@ -7,7 +8,7 @@ const context = { [LogContexts.namespace]: 'backports' } /** * @internal */ -export const backportJestConfig = ( +export const backportJestConfig = ( logger: Logger, config: T, ): T => {