Skip to content

Commit

Permalink
Merge pull request #652 from huafu/hotfix/babel-and-preprocess-bridges
Browse files Browse the repository at this point in the history
[HOTFIX] babel and preprocess.js bridges
  • Loading branch information
kulshekhar authored Aug 3, 2018
2 parents 6916e7b + 3068e73 commit 125d06b
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 8,115 deletions.
8,090 changes: 0 additions & 8,090 deletions package-lock.json

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-jest",
"version": "23.1.0",
"version": "23.1.1",
"main": "dist/index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
Expand Down Expand Up @@ -81,16 +81,17 @@
"@babel/core": "^7.0.0-beta.54",
"@babel/preset-env": "^7.0.0-beta.54",
"@types/babel-core": "^6.25.5",
"@types/babel__core": "^7.0.1",
"@types/es6-shim": "0.31.37",
"@types/fs-extra": "5.0.4",
"@types/jest": "^23.3.0",
"@types/lodash": "^4.14.109",
"@types/node": "10.5.5",
"@types/react": "16.4.7",
"@types/yargs": "^11.0.0",
"babel-core": "^7.0.0-0",
"babel-jest": "^23.4.0",
"babel-preset-jest": "^23.2.0",
"babel-core": "^7.0.0-0",
"cpx": "^1.5.0",
"cross-spawn": "latest",
"cross-spawn-with-kill": "latest",
Expand Down Expand Up @@ -118,5 +119,8 @@
"prettier --write --single-quote --trailing-comma all",
"git add"
]
},
"engines": {
"node": ">= 6"
}
}
7 changes: 7 additions & 0 deletions preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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');
7 changes: 7 additions & 0 deletions scripts/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ function createIntegrationMock() {
path.resolve(testCaseModuleFolder, 'package.json')
);

// TODO: remove this in next major version as well as the test, and the preprocessor.js file in root
// Copy preprocessor.js
fs.copySync(
path.resolve(rootDir, 'preprocessor.js'),
path.resolve(testCaseModuleFolder, 'preprocessor.js')
);

// Copy dist folder
fs.copySync(
path.resolve(rootDir, 'dist'),
Expand Down
33 changes: 21 additions & 12 deletions src/postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@
* Postprocess step. Based on babel-jest: https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js
* https://github.com/facebook/jest/blob/9b157c3a7c325c3971b2aabbe4c8ab4ce0b0c56d/packages/babel-jest/src/index.js
*/
import * as __types__babel from 'babel-core';
import __types__istanbulPlugin from 'babel-plugin-istanbul';
import * as __types__jestPreset from 'babel-preset-jest';
import {
BabelTransformOptions,
PostProcessHook,
JestCacheKeyOptions,
TBabel,
TBabelPluginIstanbul,
TBabelPresetJest,
} from './types';
import { logOnce } from './utils/logger';
import getTSJestConfig from './utils/get-ts-jest-config';
import {
importBabelCore,
importBabelPluginIstanbul,
importBabelPresetJest,
} from './utils/imports';
import { TransformOptions } from '@babel/core';

let babel: typeof __types__babel;
let istanbulPlugin: typeof __types__istanbulPlugin;
let jestPreset: typeof __types__jestPreset;
let babel: TBabel;
let istanbulPlugin: TBabelPluginIstanbul;
let jestPreset: TBabelPresetJest;

function importBabelDeps() {
if (babel) return; // tslint:disable-line
// we must use babel until we handle hoisting of jest.mock() internally
babel = require('@babel/core');
istanbulPlugin = require('babel-plugin-istanbul').default;
jestPreset = require('babel-preset-jest');
babel = importBabelCore();
istanbulPlugin = importBabelPluginIstanbul();
jestPreset = importBabelPresetJest();
}

// Function that takes the transpiled typescript and runs it through babel/whatever.
Expand Down Expand Up @@ -62,13 +67,17 @@ function createBabelTransformer(
codeSourcemapPair: jest.TransformedSource,
filename: string,
config: jest.ProjectConfig,
transformOptions: JestCacheKeyOptions,
transformOptions: jest.TransformOptions,
): jest.TransformedSource => {
const inputSourceMap =
typeof codeSourcemapPair.map === 'string'
? JSON.parse(codeSourcemapPair.map)
: codeSourcemapPair.map;
const theseOptions = { ...optionsBase, filename, inputSourceMap };
const theseOptions = {
...optionsBase,
filename,
inputSourceMap,
} as TransformOptions;
if (transformOptions && transformOptions.instrument) {
theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { TransformOptions as BabelTransformOpts } from 'babel-core';

import * as _babel from '@babel/core';
import * as _babelEnv from '@babel/preset-env';
import * as _babelJest from 'babel-preset-jest';
import _babelIstanbul from 'babel-plugin-istanbul';

export type TBabel = typeof _babel;
export type TBabelPluginIstanbul = typeof _babelIstanbul;
export type TBabelPresetEnv = typeof _babelEnv;
export type TBabelPresetJest = typeof _babelJest;

export interface JestCacheKeyOptions {
rootDir: string;
instrument: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/get-ts-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function findTSConfigPath(
`${jestConfig.rootDir}${sep}`,
);
// ensure the path is resolved
if (!tsConfigFile.startsWith('/')) {
if (!tsConfigFile.startsWith('/') && jestConfig.rootDir) {
tsConfigFile = resolve(jestConfig.rootDir, tsConfigFile);
} else {
tsConfigFile = resolve(tsConfigFile);
Expand Down
57 changes: 57 additions & 0 deletions src/utils/imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
TBabel,
TBabelPresetEnv,
TBabelPluginIstanbul,
TBabelPresetJest,
} from '../types';

export function importBabelCore(): TBabel {
const mod = tryRequire('@babel/core') || tryRequire('babel-core');
if (!mod) {
throw new Error(
`[ts-jest] You must install the '@babel/core' or 'babel-core'` +
` package (depending on the version you want to use).`,
);
}
return mod;
}

export function importBabelPresetEnv(): TBabelPresetEnv {
const mod = tryRequire('@babel/preset-env') || tryRequire('babel-preset-env');
if (!mod) {
throw new Error( // babel-jest has the env preset as a dep
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`,
);
}
return mod;
}

export function importBabelPresetJest(): TBabelPresetJest {
const mod = tryRequire('babel-preset-jest');
if (!mod) {
throw new Error( // babel-jest has the jest preset as a dep
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`,
);
}
return mod;
}

export function importBabelPluginIstanbul(): TBabelPluginIstanbul {
const mod = tryRequire('babel-plugin-istanbul');
if (!mod) {
throw new Error( // babel-jest has the istanbul plugin as a dep
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`,
);
}
return mod.default;
}

function tryRequire<T = any>(packageName: string): T | void {
let mod: T;
try {
mod = require(packageName);
} catch (err) {
if (err.code !== 'ENOENT') throw err; // tslint:disable-line
}
return mod;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Deprecated Jest transform value should log the depration message 1`] = `"[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\\"."`;
14 changes: 14 additions & 0 deletions tests/__tests__/deprecated-transform.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import runJest from '../__helpers__/runJest';

describe('Deprecated Jest transform value', () => {
it('should log the depration message', () => {
const result = runJest('../deprecated-transform', ['--no-cache']);

const output = result.stderr;

// get only the line with deprecation message (we dont want the snapshot to contain any timing or test name)
const msg = output.split('\n').find(line => /deprecated/i.test(line));

expect(msg).toMatchSnapshot();
});
});
3 changes: 3 additions & 0 deletions tests/deprecated-transform/Hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Hello {
constructor(readonly msg: string) {}
}
10 changes: 10 additions & 0 deletions tests/deprecated-transform/__tests__/Hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare var jest, describe, it, expect;

import { Hello } from '../Hello';

describe('Hello Class', () => {
it('should create a new Hello', () => {
const hello = new Hello('foo');
expect(hello.msg).toBe('foo');
});
});
9 changes: 9 additions & 0 deletions tests/deprecated-transform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "js"]
}
}
5 changes: 5 additions & 0 deletions tests/deprecated-transform/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "es5",
}
}
34 changes: 24 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,14 @@
lodash "^4.17.5"
to-fast-properties "^2.0.0"

"@babel/types@^7.0.0-beta.54":
version "7.0.0-beta.55"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.55.tgz#7755c9d2e58315a64f05d8cf3322379be16d9199"
dependencies:
esutils "^2.0.2"
lodash "^4.17.10"
to-fast-properties "^2.0.0"

"@samverschueren/stream-to-observable@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz#ecdf48d532c58ea477acfcab80348424f8d0662f"
Expand Down Expand Up @@ -554,6 +562,12 @@
version "7.0.4"
resolved "https://registry.yarnpkg.com/@types/babel-types/-/babel-types-7.0.4.tgz#bfd5b0d0d1ba13e351dff65b6e52783b816826c8"

"@types/babel__core@^7.0.1":
version "7.0.1"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.0.1.tgz#8b608a1c55cb4c752352cc6c78f7beb61a3fd51f"
dependencies:
"@babel/types" "^7.0.0-beta.54"

"@types/babylon@*":
version "6.16.3"
resolved "https://registry.yarnpkg.com/@types/babylon/-/babylon-6.16.3.tgz#c2937813a89fcb5e79a00062fc4a8b143e7237bb"
Expand Down Expand Up @@ -3979,22 +3993,22 @@ rc@^1.1.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

react-is@^16.4.1:
version "16.4.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.4.1.tgz#d624c4650d2c65dbd52c72622bbf389435d9776e"
react-is@^16.4.2:
version "16.4.2"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.4.2.tgz#84891b56c2b6d9efdee577cc83501dfc5ecead88"

react-test-renderer@16.4.1:
version "16.4.1"
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.4.1.tgz#f2fb30c2c7b517db6e5b10ed20bb6b0a7ccd8d70"
react-test-renderer@16.4.2:
version "16.4.2"
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.4.2.tgz#4e03eca9359bb3210d4373f7547d1364218ef74e"
dependencies:
fbjs "^0.8.16"
object-assign "^4.1.1"
prop-types "^15.6.0"
react-is "^16.4.1"
react-is "^16.4.2"

react@16.4.1:
version "16.4.1"
resolved "https://registry.yarnpkg.com/react/-/react-16.4.1.tgz#de51ba5764b5dbcd1f9079037b862bd26b82fe32"
react@16.4.2:
version "16.4.2"
resolved "https://registry.yarnpkg.com/react/-/react-16.4.2.tgz#2cd90154e3a9d9dd8da2991149fdca3c260e129f"
dependencies:
fbjs "^0.8.16"
loose-envify "^1.1.0"
Expand Down

0 comments on commit 125d06b

Please sign in to comment.