Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
[squash] package up jestjs#3780
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jun 9, 2017
1 parent dec54a3 commit 56858d6
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 59 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 @@ -102,11 +102,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 @@ -116,7 +112,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 @@ -126,7 +122,7 @@ const createTransformer = (options: any) => {
]);
}

return babel.transform(src, theseOptions).code;
return babelTransform(src, theseOptions).code;
},
};
};
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

module.exports = {
plugins: [
// Cannot be `import` as this file is not compiled
require('babel-plugin-jest-hoist'),
],
};
2 changes: 1 addition & 1 deletion packages/eslint-config-fb-strict/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

// Can't be ESModules as this is not transpiled
// Can't be ESModules as this is not compiled
const fbjsConfig = require('eslint-config-fbjs');

const variableNamePattern = String.raw`\s*[a-zA-Z_$][a-zA-Z_$\d]*\s*`;
Expand Down
13 changes: 9 additions & 4 deletions packages/eslint-plugin-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
* @flow
*/

import noDisabledTests from './rules/no-disabled-tests';
import noFocusedTests from './rules/no-focused-tests';
import noIdenticalTitle from './rules/no-identical-title';
import validExpect from './rules/valid-expect';

module.exports = {
configs: {
recommended: {
Expand Down Expand Up @@ -42,9 +47,9 @@ module.exports = {
},
},
rules: {
'no-disabled-tests': require('./rules/no-disabled-tests'),
'no-focused-tests': require('./rules/no-focused-tests'),
'no-identical-title': require('./rules/no-identical-title'),
'valid-expect': require('./rules/valid-expect'),
'no-disabled-tests': noDisabledTests,
'no-focused-tests': noFocusedTests,
'no-identical-title': noIdenticalTitle,
'valid-expect': validExpect,
},
};
7 changes: 5 additions & 2 deletions packages/jest-changed-files/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
* @flow
*/

import git from './git';
import hg from './hg';

module.exports = {
git: require('./git'),
hg: require('./hg'),
git,
hg,
};
3 changes: 2 additions & 1 deletion packages/jest-cli/src/TestNamePatternPrompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import type {TestResult} from 'types/TestResult';
import chalk from 'chalk';
import type {ScrollOptions} from './lib/scrollList';

import scroll from './lib/scrollList';
Expand Down Expand Up @@ -53,7 +54,7 @@ module.exports = class TestNamePatternPrompt extends PatternPrompt {
total,
'test',
pipe,
` from ${require('chalk').yellow('cached')} test suites`,
` from ${chalk.yellow('cached')} test suites`,
);

const width = getTerminalWidth();
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
1 change: 1 addition & 0 deletions packages/jest-cli/src/cli/getJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function getJest(packageRoot: Path) {
/* $FlowFixMe */
return require(binPath);
} else {
// TODO: Because of a dependency cycle, this can only be inlined once the babel plugin for inlining is merged
const jest = require('../jest');
// Check if Jest is specified in `package.json` but not installed.
if (fs.existsSync(packageJSONPath)) {
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
7 changes: 5 additions & 2 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import fs from 'fs';
import path from 'path';
import nodeModulesPaths from 'resolve/lib/node-modules-paths';
import isBuiltinModule from 'is-builtin-module';
import defaultResolver from './defaultResolver.js';

type ResolverConfig = {|
browser?: boolean,
Expand Down Expand Up @@ -89,8 +90,10 @@ class Resolver {
}

static findNodeModule(path: Path, options: FindNodeModuleConfig): ?Path {
/* $FlowFixMe */
const resolver = require(options.resolver || './defaultResolver.js');
const resolver = options.resolver
? /* $FlowFixMe */
require(options.resolver)
: defaultResolver;
const paths = options.paths;

try {
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
3 changes: 2 additions & 1 deletion packages/jest-runtime/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import chalk from 'chalk';
import yargs from 'yargs';
import {Console, setGlobal, validateCLIOptions} from 'jest-util';
import {readConfig} from 'jest-config';
const VERSION = (require('../../package.json').version: string);
import Runtime from '../';
import args from './args';

const VERSION = (require('../../package.json').version: string);

function run(cliArgv?: Argv, cliInfo?: Array<string>) {
let argv;
if (cliArgv) {
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import fs from 'graceful-fs';
import stripBOM from 'strip-bom';
import ScriptTransformer from './ScriptTransformer';
import shouldInstrument from './shouldInstrument';
import cliArgs from './cli/args';

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

static runCLI(args?: Argv, info?: Array<string>) {
// TODO: If this is not inline, the repl test fails
return require('./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
4 changes: 3 additions & 1 deletion packages/jest/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
* @flow
*/

module.exports = require('jest-cli');
import cli from 'jest-cli';

module.exports = cli;
19 changes: 13 additions & 6 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import type {

import style from 'ansi-styles';

import AsymmetricMatcher from './plugins/AsymmetricMatcher';
import ConvertAnsi from './plugins/ConvertAnsi';
import HTMLElement from './plugins/HTMLElement';
import Immutable from './plugins/ImmutablePlugins';
import ReactElement from './plugins/ReactElement';
import ReactTestComponent from './plugins/ReactTestComponent';

type Theme = {|
comment?: string,
content?: string,
Expand Down Expand Up @@ -959,12 +966,12 @@ function prettyFormat(val: any, initialOptions?: InitialOptions): string {
}

prettyFormat.plugins = {
AsymmetricMatcher: require('./plugins/AsymmetricMatcher'),
ConvertAnsi: require('./plugins/ConvertAnsi'),
HTMLElement: require('./plugins/HTMLElement'),
Immutable: require('./plugins/ImmutablePlugins'),
ReactElement: require('./plugins/ReactElement'),
ReactTestComponent: require('./plugins/ReactTestComponent'),
AsymmetricMatcher,
ConvertAnsi,
HTMLElement,
Immutable,
ReactElement,
ReactTestComponent,
};

module.exports = prettyFormat;
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ConvertAnsi.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat';

import ansiRegex from 'ansi-regex';
import style from 'ansi-styles';

const toHumanReadableAnsi = text => {
const style = require('ansi-styles');
return text.replace(ansiRegex(), (match, offset, string) => {
switch (match) {
case style.red.close:
Expand Down
Loading

0 comments on commit 56858d6

Please sign in to comment.