Skip to content

Commit

Permalink
feat: warn about unsupported versions
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 20, 2018
1 parent 015e8f7 commit 1103071
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 58 deletions.
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module.exports = {
rootDir: '.',
transform: {
'\\.ts$': '<rootDir>/dist/index.js'
'\\.ts$': '<rootDir>/dist/index.js',
},
testMatch: ['<rootDir>/src/**/?(*.)+(spec|test).ts?(x)'],
collectCoverageFrom: [
'<rootDir>/src/**/*.ts',
'!<rootDir>/src/**/*.d.ts',
'!<rootDir>/src/**/*.spec.ts',
'!<rootDir>/src/**/*.test.ts',
'!<rootDir>/src/**/__*__/*'
'!<rootDir>/src/**/__*__/*',
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
testEnvironment: 'node'
testEnvironment: 'node',
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"fast-json-stable-stringify": "^2.0.0",
"json5": "^1.0.1",
"make-error": "^1.3.4",
"mkdirp": "^0.5.1"
"mkdirp": "^0.5.1",
"semver": "^5.5.1"
},
"peerDependencies": {
"babel-jest": "23.x",
Expand Down Expand Up @@ -74,7 +75,6 @@
"prettier": "^1.14.2",
"prettier-tslint": "^0.4.0",
"reflect-metadata": "^0.1.12",
"semver": "^5.5.1",
"source-map": "^0.7.3",
"tslint": "^5.11.0",
"typescript": "^3.0.1"
Expand Down
9 changes: 9 additions & 0 deletions preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
console.warn(
'ts-jest',
'[deprecated]',
`Replace any occurrences of "ts-jest/dist/preprocessor.js" or ` +
` "<rootDir>/node_modules/ts-jest/preprocessor.js"` +
` in the 'transform' section of your Jest config with just "ts-jest".`,
)

module.exports = require('./dist')
1 change: 0 additions & 1 deletion src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe('hello', () => {

export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
return {
version: '0.0.0-mock0',
typeCheck: false,
compiler: 'typescript',
babelConfig: undefined,
Expand Down
24 changes: 24 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// tslint:disable:max-line-length
import { __setup } from './lib/debug'
import * as tsJest from '.'
import { TsJestTransformer } from './lib/ts-jest-transformer'

Expand Down Expand Up @@ -31,6 +33,28 @@ describe('ts-jest', () => {
})
})

describe('old entry point', () => {
const MANIFEST = { tsJestIndex: true }
const spy = jest.spyOn(console, 'warn')
spy.mockImplementation(() => undefined)
afterAll(() => {
spy.mockRestore()
})

it('should warn when using old path to ts-jest', () => {
jest.mock('../dist/index', () => MANIFEST)
expect(require('../preprocessor.js')).toBe(MANIFEST)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"ts-jest",
"[deprecated]",
"Replace any occurrences of \\"ts-jest/dist/preprocessor.js\\" or \\"<rootDir>/node_modules/ts-jest/preprocessor.js\\" in the 'transform' section of your Jest config with just \\"ts-jest\\".",
]
`)
})
})

describe('createTransformer', () => {
it('should create different instances', () => {
const tr1 = tsJest.createTransformer()
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { TsJestTransformer } from './lib/ts-jest-transformer'
import { createJestPreset } from './lib/create-jest-preset'
import { TsJestGlobalOptions } from './lib/types'
import { VersionCheckers } from './lib/version-checkers'

// tslint:disable-next-line:no-var-requires
const version: string = require('../package.json').version

let transformer!: TsJestTransformer
function defaultTransformer(): TsJestTransformer {
return transformer || (transformer = new TsJestTransformer())
return transformer || (transformer = createTransformer())
}

function createTransformer(baseConfig?: TsJestGlobalOptions) {
VersionCheckers.jest.warn()
return new TsJestTransformer(baseConfig)
}
function tsProcess(...args: any[]): any {
Expand All @@ -26,14 +31,15 @@ const __singleton = () => transformer
const __resetModule = () => (transformer = undefined as any)

export {
// jest API
version,
// jest API ===============
createTransformer,
tsProcess as process,
getCacheKey,
// extra
// extra ==================
createJestPreset,
jestPreset,
// tests
// tests ==================
__singleton,
__resetModule,
}
22 changes: 11 additions & 11 deletions src/lib/__snapshots__/backports.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to false should wran the user 1`] = `"ts-jest \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead."`;
exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to false should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead."`;

exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to true should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -36,7 +36,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to true should wran the user 1`] = `"ts-jest \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead."`;
exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to true should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead."`;

exports[`backportJestConfig with "globals.__TS_CONFIG__" set to { foo: 'bar' } should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -60,7 +60,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.__TS_CONFIG__" set to { foo: 'bar' } should wran the user 1`] = `"ts-jest \\"[jest-config].globals.__TS_CONFIG__\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead."`;
exports[`backportJestConfig with "globals.__TS_CONFIG__" set to { foo: 'bar' } should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.__TS_CONFIG__\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead."`;

exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to '\\\\.spec\\\\.ts$' should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -84,7 +84,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to '\\\\.spec\\\\.ts$' should wran the user 1`] = `"ts-jest \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead."`;
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to '\\\\.spec\\\\.ts$' should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead."`;

exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to false should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -106,7 +106,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to false should wran the user 1`] = `"ts-jest \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead."`;
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to false should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead."`;

exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to true should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -128,7 +128,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to true should wran the user 1`] = `"ts-jest \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead."`;
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to true should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead."`;

exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to false should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -150,7 +150,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to false should wran the user 1`] = `"ts-jest \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead."`;
exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to false should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead."`;

exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to true should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -170,7 +170,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to true should wran the user 1`] = `"ts-jest \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead."`;
exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to true should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead."`;

exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -192,7 +192,7 @@ Object {
}
`;

exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should wran the user 1`] = `"ts-jest \\"[jest-config].globals.ts-jest.tsConfigFile\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead."`;
exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should wran the user 1`] = `"ts-jest: \\"[jest-config].globals.ts-jest.tsConfigFile\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead."`;

exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to false should have changed the config correctly: before 1`] = `
Object {
Expand All @@ -215,7 +215,7 @@ Object {
`;

exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to false should wran the user 1`] = `
"ts-jest \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
"ts-jest: \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
↳ See \`babel-jest\` related issue: https://github.com/facebook/jest/issues/3845"
`;

Expand All @@ -240,6 +240,6 @@ Object {
`;

exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to true should wran the user 1`] = `
"ts-jest \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
"ts-jest: \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
↳ See \`babel-jest\` related issue: https://github.com/facebook/jest/issues/3845"
`;
1 change: 0 additions & 1 deletion src/lib/__snapshots__/config-set.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ Object {
"value": undefined,
},
"typeCheck": false,
"version": "X.Y.Z",
}
`;
4 changes: 2 additions & 2 deletions src/lib/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('cache', () => {
expect(logger).toHaveBeenCalledTimes(1)
expect(logger).toHaveBeenCalledWith(
'log',
'ts-jest',
'ts-jest:',
'readThrough:cache-miss',
__filename,
)
Expand All @@ -96,7 +96,7 @@ describe('cache', () => {
expect(logger).toHaveBeenCalledTimes(1)
expect(logger).toHaveBeenCalledWith(
'log',
'ts-jest',
'ts-jest:',
'readThrough:cache-hit',
__filename,
)
Expand Down
1 change: 0 additions & 1 deletion src/lib/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { resolve } from 'path'
import { normalizeSlashes } from './normalize-slashes'

jest.mock('./backports')
jest.mock('../../package.json', () => ({ version: 'X.Y.Z' }))

const backports = mocked(_backports)

Expand Down
25 changes: 14 additions & 11 deletions src/lib/config-set.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* This is the core of settings and ts-jest.
* Since configuration are used to create a good cache key so that jest can be fast,
* everything depending on it is here.
* This is the core of settings and so ts-jest.
* Since configuration are used to create a good cache key, everything
* depending on it is here. Fast jest relies on correct cache keys
* depending on all settings that could affect the generated output.
*
* The big issue with jest is that it calls first `getCacheKey()` with stringified version
* of the `jest.ProjectConfig`, and then later it calls `process()` with the complete,
* object version of it.
* The big issue is that jest calls first `getCacheKey()` with stringified
* version of the `jest.ProjectConfig`, and then later it calls `process()`
* with the complete, object version of it.
*/
import { JsonableValue } from './jsonable-value'
import {
Expand Down Expand Up @@ -34,6 +35,8 @@ import { sha1 } from './sha1'
import { stringify } from './json'
import { normalizeSlashes } from './normalize-slashes'
import { createCompiler } from './compiler'
import { version as myVersion } from '..'
import semver from 'semver'

interface ReadTsConfigResult {
// what we get from reading the config file if any, or inline options
Expand Down Expand Up @@ -193,7 +196,6 @@ export class ConfigSet {

// parsed options
return {
version: require('../../package.json').version,
tsConfig,
babelConfig,
diagnostics,
Expand Down Expand Up @@ -249,10 +251,10 @@ export class ConfigSet {

// loadOptions is from babel 7+, and OptionManager is backward compatible but deprecated 6 API
const { OptionManager, loadOptions, version } = importer.babelCore(
ImportReasons.babelJest,
ImportReasons.BabelJest,
)
// cwd is only supported from babel >= 7
if (parseInt(version.split('.').shift() as string, 10) < 7) {
if (version && semver.satisfies(version, '>=6 <7')) {
delete base.cwd
}
// call babel to load options
Expand All @@ -268,15 +270,15 @@ export class ConfigSet {

@Memoize()
get compilerModule(): TTypeScript {
return importer.typescript(ImportReasons.tsJest, this.tsJest.compiler)
return importer.typescript(ImportReasons.TsJest, this.tsJest.compiler)
}

@Memoize()
get babelJestTransformer(): BabelJestTransformer | undefined {
const { babel } = this
if (!babel) return
return importer
.babelJest(ImportReasons.babelJest)
.babelJest(ImportReasons.BabelJest)
.createTransformer(babel) as BabelJestTransformer
}

Expand Down Expand Up @@ -478,6 +480,7 @@ export class ConfigSet {
@Memoize()
get jsonValue() {
return new JsonableValue({
version: myVersion,
jest: this.jest,
tsJest: this.tsJest,
babel: this.babel,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('debug', () => {
__setup()
debug('foo')
expect(consoleSpies.log).toHaveBeenCalledTimes(1)
expect(consoleSpies.log).toHaveBeenCalledWith('ts-jest', 'foo')
expect(consoleSpies.log).toHaveBeenCalledWith('ts-jest:', 'foo')
})
it('should NOT log when TS_JEST_DEBUG is falsy', () => {
process.env.TS_JEST_DEBUG = ''
Expand All @@ -42,7 +42,7 @@ describe('wrapWithDebug', () => {
__setup()
expect(wrapAndCall('bar')).toBe('hello bar')
expect(consoleSpies.log).toHaveBeenCalledTimes(1)
expect(consoleSpies.log).toHaveBeenCalledWith('ts-jest', 'foo')
expect(consoleSpies.log).toHaveBeenCalledWith('ts-jest:', 'foo')
})
it('should NOT log when TS_JEST_DEBUG is falsy', () => {
process.env.TS_JEST_DEBUG = ''
Expand Down
Loading

0 comments on commit 1103071

Please sign in to comment.