Skip to content

Commit 33ff29f

Browse files
committed
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

File tree

17 files changed

+133
-80
lines changed

17 files changed

+133
-80
lines changed
Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 29 deletions
This file was deleted.

e2e/__tests__/test-helpers.test.ts

Lines changed: 8 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 38 additions & 0 deletions
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

Lines changed: 15 additions & 6 deletions
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
}

0 commit comments

Comments
 (0)