Skip to content

Commit

Permalink
Merge pull request #351 from kulshekhar/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
kulshekhar authored Dec 18, 2017
2 parents fac86e3 + 59af832 commit c610c13
Show file tree
Hide file tree
Showing 29 changed files with 721 additions and 2,707 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ dist: trusty
sudo: required
language: node_js
node_js:
- "9"
- "8"
- "6"
- "4"
before_install:
- sudo sysctl fs.inotify.max_user_watches=524288
- yarn global add greenkeeper-lockfile@1 rimraf
before_script:
- greenkeeper-lockfile-update
# - greenkeeper-lockfile-update
script:
- yarn test
after_script:
- greenkeeper-lockfile-upload
# - greenkeeper-lockfile-upload
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ init:
# what combinations to test
environment:
matrix:
- nodejs_version: 4
- nodejs_version: 6
- nodejs_version: 8
- nodejs_version: 9

# get the latest stable version of Node 0.STABLE.latest
install:
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-jest",
"version": "21.2.4",
"version": "22.0.0",
"main": "index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
Expand Down Expand Up @@ -49,6 +49,10 @@
"^.+\\.tsx?$": "<rootDir>/dist/preprocessor.js"
},
"testRegex": "tests/__tests__/.*\\.spec\\.ts$",
"testPathIgnorePatterns": [
"/node_modules/",
"tests/__tests__/watch.spec.ts"
],
"coverageReporters": [
"text"
],
Expand All @@ -69,16 +73,16 @@
"babel-core": "^6.24.1",
"babel-plugin-istanbul": "^4.1.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"babel-preset-jest": "^21.2.0",
"babel-preset-jest": "^22.0.1",
"cpx": "^1.5.0",
"fs-extra": "4.0.3",
"jest-config": "^21.2.1",
"jest-config": "^22.0.1",
"pkg-dir": "^2.0.0",
"source-map-support": "^0.5.0",
"yargs": "^10.0.3"
},
"peerDependencies": {
"jest": "^21.1.0 || ^21.3.0-alpha.1 || ^22.0.0-alpha.1",
"jest": "^22.0.1 || ^22.1.0-alpha.1 || ^23.0.0-alpha.1",
"typescript": "2.x"
},
"devDependencies": {
Expand All @@ -94,7 +98,7 @@
"cross-spawn-with-kill": "latest",
"doctoc": "latest",
"husky": "^0.14.3",
"jest": "^21.1.0",
"jest": "^22.0.1",
"lint-staged": "^6.0.0",
"prettier": "^1.5.3",
"react": "latest",
Expand Down
2 changes: 1 addition & 1 deletion src/jest-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ export interface TsJestConfig {
skipBabel?: boolean;
useBabelrc?: boolean;
babelConfig?: BabelTransformOpts;
tsConfigFile?: String;
tsConfigFile?: string;
}
93 changes: 42 additions & 51 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,82 @@
import * as crypto from 'crypto';
import * as fs from 'fs-extra';
import * as nodepath from 'path';
import * as tsc from 'typescript';
import { JestConfig, Path, TransformOptions } from './jest-types';
import { getPostProcessHook } from './postprocess';
import { getTSConfig, getTSJestConfig } from './utils';
import {
cacheFile,
getTSConfig,
getTSJestConfig,
injectSourcemapHook,
} from './utils';

export function process(
src: string,
path: Path,
config: JestConfig,
filePath: Path,
jestConfig: JestConfig,
transformOptions: TransformOptions = { instrument: false },
) {
// transformOptions.instrument is a proxy for collectCoverage
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
const compilerOptions = getTSConfig(
config.globals,
jestConfig.globals,
transformOptions.instrument,
);
const tsJestConfig = getTSJestConfig(config.globals);

const isTsFile = path.endsWith('.ts') || path.endsWith('.tsx');
const isJsFile = path.endsWith('.js') || path.endsWith('.jsx');
const isHtmlFile = path.endsWith('.html');

const postHook = getPostProcessHook(compilerOptions, config, tsJestConfig);
const isTsFile = /\.tsx?$/.test(filePath);
const isJsFile = /\.jsx?$/.test(filePath);
const isHtmlFile = /\.html$/.test(filePath);

if (isHtmlFile && config.globals.__TRANSFORM_HTML__) {
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) {
src = 'module.exports=`' + src + '`;';
}

const processFile =
compilerOptions.allowJs === true ? isTsFile || isJsFile : isTsFile;

if (processFile) {
const tsTranspiled = tsc.transpileModule(src, {
compilerOptions,
fileName: path,
});

const outputText = postHook(
tsTranspiled.outputText,
path,
config,
transformOptions,
);
if (!processFile) {
return src;
}

// store transpiled code contains source map into cache, except test cases
if (!config.testRegex || !path.match(config.testRegex)) {
const outputFilePath = nodepath.join(
config.cacheDirectory,
'/ts-jest/',
crypto
.createHash('md5')
.update(path)
.digest('hex'),
);
const tsTranspiled = tsc.transpileModule(src, {
compilerOptions,
fileName: filePath,
});

fs.outputFileSync(outputFilePath, outputText);
}
const postHook = getPostProcessHook(
compilerOptions,
jestConfig,
getTSJestConfig(jestConfig.globals),
);

const start = outputText.length > 12 ? outputText.substr(1, 10) : '';
const outputText = postHook(
tsTranspiled.outputText,
filePath,
jestConfig,
transformOptions,
);

const modified =
start === 'use strict'
? `'use strict';require('ts-jest').install();${outputText}`
: `require('ts-jest').install();${outputText}`;
const modified = injectSourcemapHook(outputText);

return modified;
}
cacheFile(jestConfig, filePath, modified);

return src;
return modified;
}

export function getCacheKey(
fileData: string,
filePath: Path,
configStr: string,
options: TransformOptions = { instrument: false },
jestConfigStr: string,
transformOptions: TransformOptions = { instrument: false },
): string {
const jestConfig: JestConfig = JSON.parse(configStr);
const tsConfig = getTSConfig(jestConfig.globals, options.instrument);
const jestConfig: JestConfig = JSON.parse(jestConfigStr);

const tsConfig = getTSConfig(jestConfig.globals, transformOptions.instrument);

return crypto
.createHash('md5')
.update(JSON.stringify(tsConfig), 'utf8')
.update(JSON.stringify(options), 'utf8')
.update(fileData + filePath + configStr, 'utf8')
.update(JSON.stringify(transformOptions), 'utf8')
.update(fileData + filePath + jestConfigStr, 'utf8')
.digest('hex');
}
66 changes: 66 additions & 0 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as fs from 'fs';
import { normalize } from 'jest-config';
import * as setFromArgv from 'jest-config/build/set_from_argv';
import * as path from 'path';

function readRawConfig(argv, root) {
const rawConfig = parseConfig(argv);

if (typeof rawConfig === 'string') {
return loadJestConfigFromFile(path.resolve(process.cwd(), rawConfig), argv);
}

if (typeof rawConfig === 'object') {
const config = Object.assign({}, rawConfig);
config.rootDir = config.rootDir || root;
return normalize(config, argv);
}

const packageConfig = loadJestConfigFromPackage(
path.join(root, 'package.json'),
argv,
);
return packageConfig || normalize({ rootDir: root }, argv);
}

function loadJestConfigFromPackage(filePath, argv) {
/* tslint:disable */
const R_OK = (fs.constants && fs.constants.R_OK) || (fs['R_OK'] as number);
/* tslint:enable */
try {
fs.accessSync(filePath, R_OK);
} catch (e) {
return null;
}

const packageData = require(filePath);
const config = packageData.jest || {};
const root = path.dirname(filePath);
config.rootDir = config.rootDir ? path.resolve(root, config.rootDir) : root;
return normalize(config, argv);
}

function parseConfig(argv) {
if (argv.config && typeof argv.config === 'string') {
// If the passed in value looks like JSON, treat it as an object.
if (argv.config[0] === '{' && argv.config[argv.config.length - 1] === '}') {
return JSON.parse(argv.config);
}
}
return argv.config;
}

function loadJestConfigFromFile(filePath, argv) {
const config = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
config.rootDir = config.rootDir
? path.resolve(path.dirname(filePath), config.rootDir)
: process.cwd();
return normalize(config, argv);
}

export function getJestConfig(root) {
const yargs = require('yargs');
const argv = yargs(process.argv.slice(2)).argv;
const rawConfig = readRawConfig(argv, root);
return Object.freeze(setFromArgv.default(rawConfig, argv));
}
Loading

0 comments on commit c610c13

Please sign in to comment.