Skip to content

Commit dec54a3

Browse files
SimenBcpojer
authored andcommitted
Convert code base to ESM import (jestjs#3778)
* Convert code base to ESM import * [squash] specify correct babel plugin for import transpilation * [squash] specify correct babel plugin for import transpilation in babelrc * trigger ci
1 parent 21e8a2a commit dec54a3

File tree

146 files changed

+553
-554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+553
-554
lines changed

.babelrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"transform-es2015-destructuring",
66
"transform-es2015-parameters",
77
"transform-async-to-generator",
8-
"transform-strict-mode"
8+
"transform-strict-mode",
9+
["transform-es2015-modules-commonjs", {"allowTopLevelThis": true}]
910
],
1011
"retainLines": true
1112
}

.eslintrc.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ module.exports = {
1919
'flowtype/require-valid-file-annotation': 2,
2020
'max-len': 0,
2121
'no-multiple-empty-lines': 1,
22-
'import/order': 2,
2322
'import/no-duplicates': 2,
24-
'import/no-unresolved': [2, { 'ignore': ['^types/'] }]
23+
'import/no-unresolved': [2, { 'ignore': ['^types/'] }],
24+
// This has to be disabled until all type and module imports are combined
25+
// https://github.com/benmosher/eslint-plugin-import/issues/645
26+
'import/order': 0,
27+
// These has to be disabled until the whole code base is converted to ESM
28+
'import/default': 0,
29+
'import/named': 0,
2530
},
2631
'plugins': [
2732
'markdown',

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"babel-plugin-syntax-trailing-function-commas": "^6.13.0",
99
"babel-plugin-transform-async-to-generator": "^6.16.0",
1010
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
11+
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
1112
"babel-plugin-transform-es2015-parameters": "^6.23.0",
1213
"babel-plugin-transform-flow-strip-types": "^6.18.0",
1314
"babel-plugin-transform-runtime": "^6.23.0",

packages/babel-jest/src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import type {Path, ProjectConfig} from 'types/Config';
1212
import type {TransformOptions} from 'types/Transform';
1313

14-
const crypto = require('crypto');
15-
const fs = require('fs');
16-
const path = require('path');
17-
const jestPreset = require('babel-preset-jest');
14+
import crypto from 'crypto';
15+
import fs from 'fs';
16+
import path from 'path';
17+
import jestPreset from 'babel-preset-jest';
1818

1919
const BABELRC_FILENAME = '.babelrc';
2020
const BABELRC_JS_FILENAME = '.babelrc.js';

packages/eslint-config-fb-strict/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
*/
99

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

1213
const variableNamePattern = String.raw`\s*[a-zA-Z_$][a-zA-Z_$\d]*\s*`;

packages/jest-changed-files/src/git.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
import type {Path} from 'types/Config';
1212

13-
const path = require('path');
14-
const childProcess = require('child_process');
13+
import path from 'path';
14+
import childProcess from 'child_process';
1515

1616
type Options = {|
1717
lastCommit?: boolean,

packages/jest-changed-files/src/hg.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
import type {Path} from 'types/Config';
1212

13-
const path = require('path');
14-
const childProcess = require('child_process');
13+
import path from 'path';
14+
import childProcess from 'child_process';
1515

1616
const env = Object.assign({}, process.env, {
1717
HGPLAIN: 1,

packages/jest-circus/src/eventHandler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import type {EventHandler} from '../types';
1212

13-
const {makeDescribe, getTestDuration, makeTest} = require('./utils');
13+
import {makeDescribe, getTestDuration, makeTest} from './utils';
1414

1515
// To pass this value from Runtime object to state we need to use global[sym]
1616
const TEST_TIMEOUT_SYMBOL = Symbol.for('TEST_TIMEOUT_SYMBOL');

packages/jest-circus/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
BlockName,
1818
TestName,
1919
} from '../types';
20-
const {dispatch} = require('./state');
20+
import {dispatch} from './state';
2121

2222
const describe = (blockName: BlockName, blockFn: BlockFn) =>
2323
_dispatchDescribe(blockFn, blockName);

packages/jest-circus/src/legacy_code_todo_rewrite/jest-adapter-init.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import type {TestResult, Status} from 'types/TestResult';
1212
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
1313
import type {Event, TestEntry} from '../../types';
1414

15-
const {getState, setState} = require('jest-matchers');
16-
const {formatResultsErrors} = require('jest-message-util');
17-
const {SnapshotState, addSerializer} = require('jest-snapshot');
18-
const {addEventHandler, ROOT_DESCRIBE_BLOCK_NAME} = require('../state');
19-
const {getTestID} = require('../utils');
20-
const run = require('../run');
21-
const globals = require('../index');
15+
import {getState, setState} from 'jest-matchers';
16+
import {formatResultsErrors} from 'jest-message-util';
17+
import {SnapshotState, addSerializer} from 'jest-snapshot';
18+
import {addEventHandler, ROOT_DESCRIBE_BLOCK_NAME} from '../state';
19+
import {getTestID} from '../utils';
20+
import run from '../run';
21+
import globals from '../index';
2222

2323
const initialize = ({
2424
config,

packages/jest-circus/src/legacy_code_todo_rewrite/jest-adapter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {TestResult} from 'types/TestResult';
1414
import type Runtime from 'jest-runtime';
1515

1616
const FRAMEWORK_INITIALIZER = require.resolve('./jest-adapter-init');
17-
const path = require('path');
17+
import path from 'path';
1818

1919
const jestAdapter = async (
2020
globalConfig: GlobalConfig,

packages/jest-circus/src/legacy_code_todo_rewrite/jest-expect.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
import type {RawMatcherFn} from 'types/Matchers';
1212

13-
const expect = require('jest-matchers');
13+
import expect from 'jest-matchers';
1414

15-
const {
15+
import {
1616
addSerializer,
1717
toMatchSnapshot,
1818
toThrowErrorMatchingSnapshot,
19-
} = require('jest-snapshot');
19+
} from 'jest-snapshot';
2020

2121
type JasmineMatcher = {
2222
(): JasmineMatcher,

packages/jest-circus/src/run.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import type {
1616
DescribeBlock,
1717
} from '../types';
1818

19-
const {getState, dispatch} = require('./state');
20-
const {
19+
import {getState, dispatch} from './state';
20+
import {
2121
callAsyncFn,
2222
getAllHooksForDescribe,
2323
getEachHooksForTest,
2424
makeTestResults,
25-
} = require('./utils');
25+
} from './utils';
2626

2727
const run = async (): Promise<TestResults> => {
2828
const {rootDescribeBlock} = getState();

packages/jest-circus/src/state.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
import type {Event, State, EventHandler} from '../types';
1212

13-
const {makeDescribe} = require('./utils');
14-
const eventHandler = require('./eventHandler');
13+
import {makeDescribe} from './utils';
14+
import eventHandler from './eventHandler';
1515

1616
const eventHandlers: Array<EventHandler> = [eventHandler];
1717

packages/jest-cli/src/PatternPrompt.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
import type {ScrollOptions} from './lib/scrollList';
1414

15-
const chalk = require('chalk');
16-
const ansiEscapes = require('ansi-escapes');
17-
const Prompt = require('./lib/Prompt');
15+
import chalk from 'chalk';
16+
import ansiEscapes from 'ansi-escapes';
17+
import Prompt from './lib/Prompt';
1818

1919
const usage = (entity: string) =>
2020
`\n${chalk.bold('Pattern Mode Usage')}\n` +

packages/jest-cli/src/SearchSource.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import type {Glob, Path} from 'types/Config';
1313
import type {ResolveModuleConfig} from 'types/Resolve';
1414
import type {Test} from 'types/TestRunner';
1515

16-
const path = require('path');
17-
18-
const micromatch = require('micromatch');
19-
20-
const DependencyResolver = require('jest-resolve-dependencies');
21-
const changedFiles = require('jest-changed-files');
22-
const {escapePathForRegex, replacePathSepForRegex} = require('jest-regex-util');
16+
import path from 'path';
17+
import micromatch from 'micromatch';
18+
import DependencyResolver from 'jest-resolve-dependencies';
19+
import changedFiles from 'jest-changed-files';
20+
import {escapePathForRegex, replacePathSepForRegex} from 'jest-regex-util';
2321

2422
type SearchResult = {|
2523
noSCM?: boolean,

packages/jest-cli/src/TestNamePatternPrompt.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
import type {TestResult} from 'types/TestResult';
1212
import type {ScrollOptions} from './lib/scrollList';
1313

14-
const scroll = require('./lib/scrollList');
15-
const {getTerminalWidth} = require('./lib/terminalUtils');
16-
const Prompt = require('./lib/Prompt');
17-
const formatTestNameByPattern = require('./lib/formatTestNameByPattern');
18-
const {
14+
import scroll from './lib/scrollList';
15+
import {getTerminalWidth} from './lib/terminalUtils';
16+
import Prompt from './lib/Prompt';
17+
import formatTestNameByPattern from './lib/formatTestNameByPattern';
18+
import {
1919
formatTypeaheadSelection,
2020
printMore,
2121
printPatternCaret,
2222
printPatternMatches,
2323
printRestoredPatternCaret,
2424
printStartTyping,
2525
printTypeaheadItem,
26-
} = require('./lib/patternModeHelpers');
27-
const PatternPrompt = require('./PatternPrompt');
26+
} from './lib/patternModeHelpers';
27+
import PatternPrompt from './PatternPrompt';
2828

2929
module.exports = class TestNamePatternPrompt extends PatternPrompt {
3030
_cachedTestResults: Array<TestResult>;

packages/jest-cli/src/TestPathPatternPrompt.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ import type {ScrollOptions} from './lib/scrollList';
1414
// eslint-disable-next-line import/default
1515
import type SearchSource from './SearchSource';
1616

17-
const chalk = require('chalk');
18-
const stringLength = require('string-length');
19-
const scroll = require('./lib/scrollList');
20-
const {getTerminalWidth} = require('./lib/terminalUtils');
21-
const highlight = require('./lib/highlight');
22-
const {trimAndFormatPath} = require('./reporters/utils');
23-
const Prompt = require('./lib/Prompt');
24-
const {
17+
import chalk from 'chalk';
18+
import stringLength from 'string-length';
19+
import scroll from './lib/scrollList';
20+
import {getTerminalWidth} from './lib/terminalUtils';
21+
import highlight from './lib/highlight';
22+
import {trimAndFormatPath} from './reporters/utils';
23+
import Prompt from './lib/Prompt';
24+
import {
2525
formatTypeaheadSelection,
2626
printMore,
2727
printPatternCaret,
2828
printPatternMatches,
2929
printRestoredPatternCaret,
3030
printStartTyping,
3131
printTypeaheadItem,
32-
} = require('./lib/patternModeHelpers');
33-
const PatternPrompt = require('./PatternPrompt');
32+
} from './lib/patternModeHelpers';
33+
import PatternPrompt from './PatternPrompt';
3434

3535
type SearchSources = Array<{|
3636
context: Context,

packages/jest-cli/src/TestRunner.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ import type {Context} from 'types/Context';
1818
import type {Reporter, Test} from 'types/TestRunner';
1919
import type {PathPattern} from './SearchSource';
2020

21-
const {formatExecError} = require('jest-message-util');
22-
const snapshot = require('jest-snapshot');
23-
const pify = require('pify');
24-
const throat = require('throat');
25-
const workerFarm = require('worker-farm');
26-
27-
const DefaultReporter = require('./reporters/DefaultReporter');
28-
const NotifyReporter = require('./reporters/NotifyReporter');
29-
const SummaryReporter = require('./reporters/SummaryReporter');
30-
const VerboseReporter = require('./reporters/VerboseReporter');
31-
const runTest = require('./runTest');
32-
const TestWatcher = require('./TestWatcher');
33-
const ReporterDispatcher = require('./ReporterDispatcher');
21+
import {formatExecError} from 'jest-message-util';
22+
import snapshot from 'jest-snapshot';
23+
import pify from 'pify';
24+
import throat from 'throat';
25+
import workerFarm from 'worker-farm';
26+
import DefaultReporter from './reporters/DefaultReporter';
27+
import NotifyReporter from './reporters/NotifyReporter';
28+
import SummaryReporter from './reporters/SummaryReporter';
29+
import VerboseReporter from './reporters/VerboseReporter';
30+
import runTest from './runTest';
31+
import TestWatcher from './TestWatcher';
32+
import ReporterDispatcher from './ReporterDispatcher';
3433

3534
const SLOW_TEST_TIME = 3000;
3635

packages/jest-cli/src/TestSequencer.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import type {AggregatedResult} from 'types/TestResult';
1212
import type {Context} from 'types/Context';
1313
import type {Test} from 'types/TestRunner';
1414

15-
const fs = require('fs');
16-
const getCacheFilePath = require('jest-haste-map').getCacheFilePath;
15+
import fs from 'fs';
16+
// $FlowFixMe: Missing ESM export
17+
import {getCacheFilePath} from 'jest-haste-map';
1718

1819
const FAIL = 0;
1920
const SUCCESS = 1;

packages/jest-cli/src/TestWatcher.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @flow
99
*/
1010

11-
const {EventEmitter} = require('events');
11+
import EventEmitter from 'events';
1212

1313
type State = {|
1414
interrupted: boolean,

packages/jest-cli/src/TestWorker.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ process.on('uncaughtException', err => {
1818
process.exit(1);
1919
});
2020

21-
const {ModuleMap} = require('jest-haste-map');
22-
const {separateMessageFromStack} = require('jest-message-util');
23-
24-
const Runtime = require('jest-runtime');
25-
const runTest = require('./runTest');
21+
// $FlowFixMe: Missing ESM export
22+
import {ModuleMap} from 'jest-haste-map';
23+
import {separateMessageFromStack} from 'jest-message-util';
24+
import Runtime from 'jest-runtime';
25+
import runTest from './runTest';
2626

2727
type WorkerData = {|
2828
config: ProjectConfig,
@@ -59,6 +59,7 @@ const getResolver = (config, rawModuleMap) => {
5959
if (rawModuleMap) {
6060
return Runtime.createResolver(
6161
config,
62+
// $FlowFixMe: Missing ESM export
6263
new ModuleMap(rawModuleMap.map, rawModuleMap.mocks),
6364
);
6465
} else {

packages/jest-cli/src/cli/args.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import type {Argv} from 'types/Argv';
1212

13-
const isCI = require('is-ci');
13+
import isCI from 'is-ci';
1414

1515
const check = (argv: Argv) => {
1616
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {

packages/jest-cli/src/cli/getJest.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
import type {Path} from 'types/Config';
1212

13-
const path = require('path');
14-
const chalk = require('chalk');
15-
const fs = require('graceful-fs');
13+
import path from 'path';
14+
import chalk from 'chalk';
15+
import fs from 'graceful-fs';
1616

1717
function getJest(packageRoot: Path) {
1818
const packageJSONPath = path.join(packageRoot, 'package.json');

packages/jest-cli/src/cli/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import type {Path} from 'types/Config';
1212
import type {Argv} from 'types/Argv';
1313

14-
const {validateCLIOptions} = require('jest-util');
15-
const yargs = require('yargs');
16-
const args = require('./args');
17-
const getJest = require('./getJest');
18-
const runCLI = require('./runCLI');
14+
import {validateCLIOptions} from 'jest-util';
15+
import yargs from 'yargs';
16+
import args from './args';
17+
import getJest from './getJest';
18+
import runCLI from './runCLI';
1919

2020
function run(argv?: Argv, project?: Path) {
2121
argv = yargs(argv || process.argv.slice(2))

0 commit comments

Comments
 (0)