Skip to content

Commit 33ff29f

Browse files
committedOct 6, 2018
fix(helpers): deprecate import from ts-jest, now ts-jest/utils
Importing from `ts-jest` everything will possibly make collision with future jest API. Also any star export or import might not be compatible with the target project. Now all helpers to be used in tests or config files have been moved to `ts-jest/utils`. Original ones have been kept in `ts-jest` for now, with a deprecation warning when using them. Closes #782
1 parent 645558b commit 33ff29f

17 files changed

+133
-80
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { mocked } from 'ts-jest'
2+
import { foo } from './to-mock'
3+
jest.mock('./to-mock')
4+
5+
test('foo', () => {
6+
foo()
7+
// it should log that the helper moved
8+
expect(mocked(foo).mock.calls.length).toBe(1)
9+
})

‎e2e/__cases__/test-helpers/fail.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mocked } from 'ts-jest'
1+
import { mocked } from 'ts-jest/utils'
22
import { foo, bar } from './to-mock'
33
jest.mock('./to-mock')
44

‎e2e/__cases__/test-helpers/pass.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mocked } from 'ts-jest'
1+
import { mocked } from 'ts-jest/utils'
22
import { foo, bar } from './to-mock'
33
jest.mock('./to-mock')
44

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { jestPreset } = require('ts-jest')
1+
const presets = require('ts-jest/presets')
22

3-
module.exports = Object.assign({}, jestPreset, {
3+
module.exports = Object.assign({}, presets.defaults, {
44
testEnvironment: 'node',
55
globals: { 'ts-jest': { tsConfig: {} } },
66
})

‎e2e/__tests__/__snapshots__/test-helpers.test.ts.snap

-29
This file was deleted.

‎e2e/__tests__/test-helpers.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ test('test-helpers', () => {
44
const test = configureTestCase('test-helpers', { noCache: true })
55
expect(test.run(1)).toMatchSnapshot()
66
})
7+
8+
test('with esModuleInterop set to false', () => {
9+
const test = configureTestCase('test-helpers', {
10+
noCache: true,
11+
tsJestConfig: { tsConfig: { esModuleInterop: false, allowSyntheticDefaultImports: false } },
12+
})
13+
expect(test.run(1)).toMatchSnapshot()
14+
})

‎src/cli/config/migrate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { basename, resolve } from 'path'
66
import { Arguments } from 'yargs'
77

88
import { CliCommand } from '..'
9-
import { TsJestPresets } from '../../types'
9+
import { TsJestPresets } from '../../config/create-jest-preset'
1010
import { backportJestConfig } from '../../util/backports'
1111

1212
const DEFAULT_PRESET = 'ts-jest/presets/default'

‎src/config/create-jest-preset.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as jestConfig from 'jest-config'
22

3-
import { CreateJestPresetOptions, TsJestPresets } from '../types'
43
import { rootLogger } from '../util/logger'
54

65
const logger = rootLogger.child({ namespace: 'jest-preset' })
@@ -12,6 +11,16 @@ const defaults = jestConfig.defaults || {
1211
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
1312
}
1413

14+
export interface TsJestPresets {
15+
transform: Record<string, string>
16+
testMatch: string[]
17+
moduleFileExtensions: string[]
18+
}
19+
20+
export interface CreateJestPresetOptions {
21+
allowJs?: boolean
22+
}
23+
1524
export function createJestPreset(
1625
{ allowJs = false }: CreateJestPresetOptions = {},
1726
from?: jest.InitialOptions,

‎src/index.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// tslint:disable:max-line-length
2+
import { testing } from 'bs-logger'
3+
24
import * as tsJest from '.'
5+
import { logTargetMock } from './__helpers__/mocks'
36
import { TsJestTransformer } from './ts-jest-transformer'
47

58
jest.mock('./ts-jest-transformer', () => {
@@ -58,6 +61,41 @@ Array [
5861
})
5962
})
6063

64+
describe('moved helpers', () => {
65+
let target: testing.LogTargetMock
66+
beforeEach(() => {
67+
target = logTargetMock()
68+
target.clear()
69+
})
70+
it('should warn when using mocked', () => {
71+
tsJest.mocked(42)
72+
expect(target.lines.warn).toMatchInlineSnapshot(`
73+
Array [
74+
"[level:40] The \`mocked\` helper has been moved to \`ts-jest/utils\`. Use \`import { mocked } from 'ts-jest/utils'\` instead.
75+
",
76+
]
77+
`)
78+
})
79+
it('should warn when using createJestPreset', () => {
80+
tsJest.createJestPreset()
81+
expect(target.lines.warn).toMatchInlineSnapshot(`
82+
Array [
83+
"[level:40] The \`createJestPreset\` helper has been moved to \`ts-jest/utils\`. Use \`import { createJestPreset } from 'ts-jest/utils'\` instead.
84+
",
85+
]
86+
`)
87+
})
88+
it('should warn when using pathsToModuleNameMapper', () => {
89+
tsJest.pathsToModuleNameMapper({})
90+
expect(target.lines.warn).toMatchInlineSnapshot(`
91+
Array [
92+
"[level:40] The \`pathsToModuleNameMapper\` helper has been moved to \`ts-jest/utils\`. Use \`import { pathsToModuleNameMapper } from 'ts-jest/utils'\` instead.
93+
",
94+
]
95+
`)
96+
})
97+
})
98+
6199
describe('createTransformer', () => {
62100
it('should create different instances', () => {
63101
const tr1 = tsJest.createTransformer()

‎src/index.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
import { LogContexts, LogLevels } from 'bs-logger'
12
import { readFileSync } from 'fs'
23
import { resolve } from 'path'
34

4-
import { createJestPreset } from './config/create-jest-preset'
5-
import { pathsToModuleNameMapper } from './config/paths-to-module-name-mapper'
5+
import { createJestPreset as createJestPresetCore } from './config/create-jest-preset'
6+
import { pathsToModuleNameMapper as pathsToModuleNameMapperCore } from './config/paths-to-module-name-mapper'
67
import { TsJestTransformer } from './ts-jest-transformer'
78
import { TsJestGlobalOptions } from './types'
9+
import { rootLogger } from './util/logger'
10+
import { Deprecateds, interpolate } from './util/messages'
11+
import { mocked as mockedCore } from './util/testing'
812
import { VersionCheckers } from './util/version-checkers'
913

10-
export * from './util/testing'
14+
// deprecate helpers
15+
const warn = rootLogger.child({ [LogContexts.logLevel]: LogLevels.warn })
16+
const helperMoved = <T extends (...args: any[]) => any>(name: string, helper: T) =>
17+
warn.wrap(interpolate(Deprecateds.HelperMovedToUtils, { helper: name }), helper)
18+
19+
export const mocked = helperMoved('mocked', mockedCore)
20+
export const createJestPreset = helperMoved('createJestPreset', createJestPresetCore)
21+
export const pathsToModuleNameMapper = helperMoved('pathsToModuleNameMapper', pathsToModuleNameMapperCore)
1122

1223
// tslint:disable-next-line:no-var-requires
1324
export const version: string = require('../package.json').version
@@ -41,7 +52,7 @@ export function getCacheKey(...args: any[]): any {
4152
// we let jest doing the instrumentation, it does it well
4253
export const canInstrument = false
4354

44-
const jestPreset = createJestPreset()
55+
const jestPreset = createJestPresetCore()
4556

4657
/**
4758
* @internal
@@ -57,7 +68,5 @@ export const __resetModule = () => (transformer = undefined as any)
5768

5869
export {
5970
// extra ==================
60-
createJestPreset,
6171
jestPreset,
62-
pathsToModuleNameMapper,
6372
}

‎src/types.ts

-38
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ export interface TsJestGlobalOptions {
6969
stringifyContentPathRegex?: string | RegExp
7070
}
7171

72-
export interface TsJestPresets {
73-
transform: Record<string, string>
74-
testMatch: string[]
75-
moduleFileExtensions: string[]
76-
}
77-
7872
interface TsJestConfig$tsConfig$file {
7973
kind: 'file'
8074
value: string | undefined
@@ -117,10 +111,6 @@ export interface TsJestHooksMap {
117111
afterProcess?(args: any[], result: string | jest.TransformedSource): string | jest.TransformedSource | void
118112
}
119113

120-
export interface CreateJestPresetOptions {
121-
allowJs?: boolean
122-
}
123-
124114
/**
125115
* @internal
126116
*/
@@ -180,31 +170,3 @@ export interface AstTransformerDesc {
180170
version: number
181171
factory(cs: ConfigSet): TransformerFactory<SourceFile>
182172
}
183-
184-
// test helpers
185-
186-
interface MockWithArgs<T> extends Function, jest.MockInstance<T> {
187-
new (...args: ArgumentsOf<T>): T
188-
(...args: ArgumentsOf<T>): any
189-
}
190-
191-
// tslint:disable-next-line:ban-types
192-
type MethodKeysOf<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]
193-
// tslint:disable-next-line:ban-types
194-
type PropertyKeysOf<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
195-
type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never
196-
interface MockWithArgs<T> extends Function, jest.MockInstance<T> {
197-
new (...args: ArgumentsOf<T>): T
198-
(...args: ArgumentsOf<T>): any
199-
}
200-
201-
type MockedFunction<T> = MockWithArgs<T> & { [K in keyof T]: T[K] }
202-
type MockedFunctionDeep<T> = MockWithArgs<T> & MockedObjectDeep<T>
203-
type MockedObject<T> = { [K in MethodKeysOf<T>]: MockedFunction<T[K]> } & { [K in PropertyKeysOf<T>]: T[K] }
204-
type MockedObjectDeep<T> = { [K in MethodKeysOf<T>]: MockedFunctionDeep<T[K]> } &
205-
{ [K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]> }
206-
207-
// tslint:disable-next-line:ban-types
208-
export type MaybeMockedDeep<T> = T extends Function ? MockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T
209-
// tslint:disable-next-line:ban-types
210-
export type MaybeMocked<T> = T extends Function ? MockedFunction<T> : T extends object ? MockedObject<T> : T

‎src/util/exported.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createJestPreset } from '../config/create-jest-preset'
2+
import { pathsToModuleNameMapper } from '../config/paths-to-module-name-mapper'
3+
4+
import * as subject from './exported'
5+
import { mocked } from './testing'
6+
7+
describe('exported helpers', () => {
8+
it('should have mocked', () => {
9+
expect(subject).toHaveProperty('mocked', mocked)
10+
})
11+
it('should have createJestPreset', () => {
12+
expect(subject).toHaveProperty('createJestPreset', createJestPreset)
13+
})
14+
it('should have pathsToModuleNameMapper', () => {
15+
expect(subject).toHaveProperty('pathsToModuleNameMapper', pathsToModuleNameMapper)
16+
})
17+
})

‎src/util/exported.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { mocked } from './testing'
2+
export { createJestPreset } from '../config/create-jest-preset'
3+
export { pathsToModuleNameMapper } from '../config/paths-to-module-name-mapper'

‎src/util/messages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export enum Deprecateds {
4040
ConfigOption = '"[jest-config].{{oldPath}}" is deprecated, use "[jest-config].{{newPath}}" instead.',
4141
ConfigOptionWithNote = '"[jest-config].{{oldPath}}" is deprecated, use "[jest-config].{{newPath}}" instead.\n ↳ {{note}}',
4242
ConfigOptionUseBabelRcNote = 'See `babel-jest` related issue: https://github.com/facebook/jest/issues/3845',
43+
HelperMovedToUtils = "The `{{helper}}` helper has been moved to `ts-jest/utils`. Use `import { {{helper}} } from 'ts-jest/utils'` instead.",
4344
}
4445

4546
/**

‎src/util/testing.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
import { MaybeMocked, MaybeMockedDeep } from '../types'
1+
interface MockWithArgs<T> extends Function, jest.MockInstance<T> {
2+
new (...args: ArgumentsOf<T>): T
3+
(...args: ArgumentsOf<T>): any
4+
}
5+
6+
// tslint:disable-next-line:ban-types
7+
type MethodKeysOf<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]
8+
// tslint:disable-next-line:ban-types
9+
type PropertyKeysOf<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
10+
type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never
11+
interface MockWithArgs<T> extends Function, jest.MockInstance<T> {
12+
new (...args: ArgumentsOf<T>): T
13+
(...args: ArgumentsOf<T>): any
14+
}
15+
16+
type MockedFunction<T> = MockWithArgs<T> & { [K in keyof T]: T[K] }
17+
type MockedFunctionDeep<T> = MockWithArgs<T> & MockedObjectDeep<T>
18+
type MockedObject<T> = { [K in MethodKeysOf<T>]: MockedFunction<T[K]> } & { [K in PropertyKeysOf<T>]: T[K] }
19+
type MockedObjectDeep<T> = { [K in MethodKeysOf<T>]: MockedFunctionDeep<T[K]> } &
20+
{ [K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]> }
21+
22+
// tslint:disable-next-line:ban-types
23+
export type MaybeMockedDeep<T> = T extends Function ? MockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T
24+
// tslint:disable-next-line:ban-types
25+
export type MaybeMocked<T> = T extends Function ? MockedFunction<T> : T extends object ? MockedObject<T> : T
226

327
// the typings test helper
428
export function mocked<T>(item: T, deep?: false): MaybeMocked<T>

‎utils/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../dist/util/exported'

‎utils/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../dist/util/exported')

0 commit comments

Comments
 (0)
Please sign in to comment.