Skip to content

Commit

Permalink
Extract existent inline requires to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 14, 2017
1 parent 00f48ed commit c19c945
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 34 deletions.
14 changes: 5 additions & 9 deletions packages/babel-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import jestPreset from 'babel-preset-jest';
import {transform as babelTransform, util as babelUtil} from 'babel-core';
import babelIstanbulPlugin from 'babel-plugin-istanbul';

const BABELRC_FILENAME = '.babelrc';
const BABELRC_JS_FILENAME = '.babelrc.js';
const BABEL_CONFIG_KEY = 'babel';
const PACKAGE_JSON = 'package.json';
const THIS_FILE = fs.readFileSync(__filename);

let babel;

const createTransformer = (options: any) => {
const cache = Object.create(null);

Expand Down Expand Up @@ -98,11 +98,7 @@ const createTransformer = (options: any) => {
config: ProjectConfig,
transformOptions: TransformOptions,
): string {
if (!babel) {
babel = require('babel-core');
}

if (babel.util && !babel.util.canCompile(filename)) {
if (babelUtil && !babelUtil.canCompile(filename)) {
return src;
}

Expand All @@ -112,7 +108,7 @@ const createTransformer = (options: any) => {
// Copied from jest-runtime transform.js
theseOptions.plugins = theseOptions.plugins.concat([
[
require('babel-plugin-istanbul').default,
babelIstanbulPlugin,
{
// files outside `cwd` will not be instrumented
cwd: config.rootDir,
Expand All @@ -122,7 +118,7 @@ const createTransformer = (options: any) => {
]);
}

return babel.transform(src, theseOptions).code;
return babelTransform(src, theseOptions).code;
},
};
};
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import SummaryReporter from './reporters/SummaryReporter';
import VerboseReporter from './reporters/VerboseReporter';
import runTest from './runTest';
import TestWatcher from './TestWatcher';
import CoverageReporter from './reporters/CoverageReporter';
import ReporterDispatcher from './ReporterDispatcher';

const SLOW_TEST_TIME = 3000;
Expand Down Expand Up @@ -295,9 +296,6 @@ class TestRunner {
}

if (collectCoverage) {
// coverage reporter dependency graph is pretty big and we don't
// want to require it if we're not in the `--coverage` mode
const CoverageReporter = require('./reporters/CoverageReporter');
this.addReporter(
new CoverageReporter(this._globalConfig, {
maxWorkers: this._options.maxWorkers,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/cli/getJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {Path} from 'types/Config';
import path from 'path';
import chalk from 'chalk';
import fs from 'graceful-fs';
import jest from '../jest';

function getJest(packageRoot: Path) {
const packageJSONPath = path.join(packageRoot, 'package.json');
Expand All @@ -21,7 +22,6 @@ function getJest(packageRoot: Path) {
/* $FlowFixMe */
return require(binPath);
} else {
const jest = require('../jest');
// Check if Jest is specified in `package.json` but not installed.
if (fs.existsSync(packageJSONPath)) {
/* $FlowFixMe */
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-cli/src/reporters/CoverageReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import libSourceMaps from 'istanbul-lib-source-maps';
import pify from 'pify';
import workerFarm from 'worker-farm';
import BaseReporter from './BaseReporter';
import CoverageWorker from './CoverageWorker';

const FAIL_COLOR = chalk.bold.red;
const RUNNING_TEST_COLOR = chalk.bold.dim;
Expand Down Expand Up @@ -143,7 +144,7 @@ class CoverageReporter extends BaseReporter {
let worker;
let farm;
if (this._maxWorkers <= 1) {
worker = pify(require('./CoverageWorker'));
worker = pify(CoverageWorker);
} else {
farm = workerFarm(
{
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-config/src/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
*/

import chalk from 'chalk';
import prettyFormat from 'pretty-format';

const format = (value: mixed) => require('pretty-format')(value, {min: true});
const format = (value: mixed) => prettyFormat(value, {min: true});

const deprecatedOptions = {
preprocessorIgnorePatterns: (options: {
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {ModuleMocker} from 'jest-mock';

import {FakeTimers, installCommonGlobals} from 'jest-util';
import mock from 'jest-mock';
import JSDom from 'jsdom';

class JSDOMEnvironment {
document: ?Object;
Expand All @@ -23,7 +24,7 @@ class JSDOMEnvironment {

constructor(config: ProjectConfig): void {
// lazy require
this.document = require('jsdom').jsdom(/* markup */ undefined, {
this.document = JSDom.jsdom(/* markup */ undefined, {
url: config.testURL,
});
const global = (this.global = this.document.defaultView);
Expand All @@ -50,7 +51,7 @@ class JSDOMEnvironment {

runScript(script: Script): ?any {
if (this.global) {
return require('jsdom').evalVMScript(this.global, script);
return JSDom.evalVMScript(this.global, script);
}
return null;
}
Expand Down
13 changes: 5 additions & 8 deletions packages/jest-runtime/src/ScriptTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import path from 'path';
import vm from 'vm';
import {createDirectory} from 'jest-util';
import fs from 'graceful-fs';
import {transform as babelTransform} from 'babel-core';
import babelPluginIstanbul from 'babel-plugin-istanbul';
import convertSourceMap from 'convert-source-map';
// $FlowFixMe: Missing ESM export
import {getCacheFilePath} from 'jest-haste-map';
import stableStringify from 'json-stable-stringify';
Expand Down Expand Up @@ -148,12 +151,7 @@ class ScriptTransformer {
}

_instrumentFile(filename: Path, content: string): string {
// Keeping these requires inside this function reduces a single run
// time by 2sec if not running in `--coverage` mode
const babel = require('babel-core');
const babelPluginIstanbul = require('babel-plugin-istanbul').default;

return babel.transform(content, {
return babelTransform(content, {
auxiliaryCommentBefore: ' istanbul ignore next ',
babelrc: false,
filename,
Expand Down Expand Up @@ -217,8 +215,7 @@ class ScriptTransformer {

if (mapCoverage) {
if (!transformed.map) {
const convert = require('convert-source-map');
const inlineSourceMap = convert.fromSource(transformed.code);
const inlineSourceMap = convertSourceMap.fromSource(transformed.code);
if (inlineSourceMap) {
transformed.map = inlineSourceMap.toJSON();
}
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import fs from 'graceful-fs';
import stripBOM from 'strip-bom';
import ScriptTransformer from './ScriptTransformer';
import shouldInstrument from './shouldInstrument';
import cli from './cli';
import cliArgs from './cli/args';

type Module = {|
children?: Array<any>,
Expand Down Expand Up @@ -264,11 +266,11 @@ class Runtime {
}

static runCLI(args?: Argv, info?: Array<string>) {
return require('./cli').run(args, info);
return cli.run(args, info);
}

static getCLIOptions() {
return require('./cli/args').options;
return cliArgs.options;
}

requireModule(
Expand Down
14 changes: 9 additions & 5 deletions packages/jest-validate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
* @flow
*/

import {createDidYouMeanMessage, format, logValidationWarning} from './utils';
import {ValidationError} from './errors';
import validate from './validate';

module.exports = {
ValidationError: require('./errors').ValidationError,
createDidYouMeanMessage: require('./utils').createDidYouMeanMessage,
format: require('./utils').format,
logValidationWarning: require('./utils').logValidationWarning,
validate: require('./validate'),
ValidationError,
createDidYouMeanMessage,
format,
logValidationWarning,
validate,
};
5 changes: 3 additions & 2 deletions packages/jest-validate/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/

import chalk from 'chalk';
import prettyFormat from 'pretty-format';
import leven from 'leven';

const BULLET: string = chalk.bold('\u25cf');
const DEPRECATION = `${BULLET} Deprecation Warning`;
Expand All @@ -18,7 +20,7 @@ const WARNING = `${BULLET} Validation Warning`;
const format = (value: any): string =>
typeof value === 'function'
? value.toString()
: require('pretty-format')(value, {min: true});
: prettyFormat(value, {min: true});

class ValidationError extends Error {
name: string;
Expand Down Expand Up @@ -46,7 +48,6 @@ const createDidYouMeanMessage = (
unrecognized: string,
allowedOptions: Array<string>,
) => {
const leven = require('leven');
const suggestion = allowedOptions.find(option => {
const steps: number = leven(option, unrecognized);
return steps < 3;
Expand Down

0 comments on commit c19c945

Please sign in to comment.