Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 457b17f

Browse files
committed
Move jest-runtime CLI into jest-repl
Fixes #10011
1 parent 0e0eeed commit 457b17f

File tree

15 files changed

+121
-127
lines changed

15 files changed

+121
-127
lines changed

packages/jest-runtime/bin/jest-runtime.js renamed to packages/jest-repl/bin/jest-runtime-cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ if (process.env.NODE_ENV == null) {
1010
process.env.NODE_ENV = 'test';
1111
}
1212

13-
require('../build/cli').run();
13+
require('../build/cli/runtime-cli').run();

packages/jest-repl/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@
1010
"main": "build/index.js",
1111
"types": "build/index.d.ts",
1212
"dependencies": {
13+
"@jest/console": "^26.0.1",
14+
"@jest/environment": "^26.0.1",
1315
"@jest/transform": "^26.0.1",
1416
"@jest/types": "^26.0.1",
17+
"@types/yargs": "^15.0.0",
18+
"chalk": "^4.0.0",
1519
"jest-config": "^26.0.1",
1620
"jest-runtime": "^26.0.1",
21+
"jest-util": "^26.0.1",
1722
"jest-validate": "^26.0.1",
1823
"repl": "^0.1.3",
1924
"yargs": "^15.3.1"
2025
},
2126
"devDependencies": {
2227
"@jest/test-utils": "^26.0.0",
23-
"@types/yargs": "^15.0.0"
28+
"@types/yargs": "^15.0.0",
29+
"execa": "^4.0.0"
2430
},
2531
"bin": "./bin/jest-repl.js",
2632
"engines": {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import path from 'path';
9+
// eslint-disable-next-line import/named
10+
import {sync as spawnSync} from 'execa';
11+
import {skipSuiteOnWindows} from '@jest/test-utils';
12+
13+
skipSuiteOnWindows();
14+
15+
const JEST_RUNTIME = path.resolve(__dirname, '../../bin/jest-runtime-cli.js');
16+
17+
const run = args =>
18+
spawnSync(JEST_RUNTIME, args, {
19+
cwd: process.cwd(),
20+
env: process.env,
21+
reject: false,
22+
});
23+
24+
describe('Runtime CLI', () => {
25+
it('fails with no path', () => {
26+
const expectedOutput =
27+
'Please provide a path to a script. (See --help for details)';
28+
expect(run([]).stdout).toBe(expectedOutput);
29+
});
30+
31+
it('displays script output', () => {
32+
const scriptPath = path.resolve(__dirname, './test_root/logging.js');
33+
expect(run([scriptPath, '--no-cache']).stdout).toMatch('Hello, world!');
34+
});
35+
36+
it('always disables automocking', () => {
37+
const scriptPath = path.resolve(__dirname, './test_root/logging.js');
38+
const output = run([
39+
scriptPath,
40+
'--no-cache',
41+
'--config=' +
42+
JSON.stringify({
43+
automock: true,
44+
}),
45+
]);
46+
expect(output.stdout).toMatch('Hello, world!');
47+
});
48+
49+
it('throws script errors', () => {
50+
const scriptPath = path.resolve(__dirname, './test_root/throwing.js');
51+
expect(run([scriptPath, '--no-cache']).stderr).toMatch('Error: throwing');
52+
});
53+
});

packages/jest-runtime/src/__tests__/test_root/logging.js renamed to packages/jest-repl/src/__tests__/test_root/logging.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
'use strict';
99

10-
if (require('./RegularModule').getModuleStateValue()) {
10+
if (
11+
require('jest-runtime/src/__tests__/test_root/RegularModule').getModuleStateValue()
12+
) {
1113
console.log('Hello, world!');
1214
} else {
1315
console.log('Automocking is not properly disabled in jest-runtime.');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
throw new Error('throwing');

packages/jest-repl/src/cli/args.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,45 @@
77
*/
88

99
import type {Options} from 'yargs';
10-
import Runtime = require('jest-runtime');
1110

1211
export const usage = 'Usage: $0 [--config=<pathToConfigFile>]';
1312

13+
const runtimeCLIOptions: Record<
14+
'cache' | 'config' | 'debug' | 'version' | 'watchman',
15+
Options
16+
> = {
17+
cache: {
18+
default: true,
19+
description:
20+
'Whether to use the preprocessor cache. Disable ' +
21+
'the cache using --no-cache.',
22+
type: 'boolean',
23+
},
24+
config: {
25+
alias: 'c',
26+
description: 'The path to a Jest config file.',
27+
type: 'string',
28+
},
29+
debug: {
30+
description: 'Print debugging info about your jest config.',
31+
type: 'boolean',
32+
},
33+
version: {
34+
alias: 'v',
35+
description: 'Print the version and exit',
36+
type: 'boolean',
37+
},
38+
watchman: {
39+
default: true,
40+
description:
41+
'Whether to use watchman for file crawling. Disable using ' +
42+
'--no-watchman.',
43+
type: 'boolean',
44+
},
45+
};
46+
1447
export const options: Record<string, Options> = {
15-
...Runtime.getCLIOptions(),
48+
...runtimeCLIOptions,
1649
replname: {
1750
alias: 'r',
1851
description:

packages/jest-repl/src/cli/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
*
88
*/
99

10-
import Runtime = require('jest-runtime');
1110
import yargs = require('yargs');
1211
import {validateCLIOptions} from 'jest-validate';
1312
import {deprecationEntries} from 'jest-config';
1413
import type {Config} from '@jest/types';
1514
import * as args from './args';
16-
17-
const {version: VERSION} = require('../../package.json');
15+
import {run as runtimeCLI} from './runtime-cli';
16+
import {VERSION} from './version';
1817

1918
const REPL_SCRIPT = require.resolve('./repl.js');
2019

@@ -26,5 +25,5 @@ export = function (): void {
2625

2726
argv._ = [REPL_SCRIPT];
2827

29-
Runtime.runCLI(argv, [`Jest REPL v${VERSION}`]);
28+
runtimeCLI(argv, [`Jest REPL v${VERSION}`]);
3029
};

packages/jest-runtime/src/cli/index.ts renamed to packages/jest-repl/src/cli/runtime-cli.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {cpus} from 'os';
99
import * as path from 'path';
1010
import chalk = require('chalk');
1111
import yargs = require('yargs');
12+
import Runtime = require('jest-runtime');
1213
import type {Config} from '@jest/types';
1314
import type {JestEnvironment} from '@jest/environment';
1415
import {CustomConsole} from '@jest/console';
1516
import {setGlobal, tryRealpath} from 'jest-util';
1617
import {validateCLIOptions} from 'jest-validate';
1718
import {deprecationEntries, readConfig} from 'jest-config';
18-
import {VERSION} from '../version';
19-
import type {Context} from '../types';
19+
import type {Context} from 'jest-runtime';
20+
import {VERSION} from './version';
2021
import * as args from './args';
2122

2223
export async function run(
@@ -67,9 +68,6 @@ export async function run(
6768
automock: false,
6869
};
6970

70-
// Break circular dependency
71-
const Runtime: any = require('..');
72-
7371
try {
7472
const hasteMap: Context = await Runtime.createContext(config, {
7573
maxWorkers: Math.max(cpus().length - 1, 1),

packages/jest-runtime/src/version.ts renamed to packages/jest-repl/src/cli/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
*/
77

88
// For some reason, doing `require`ing here works, while inside `cli` fails
9-
export const VERSION: string = require('../package.json').version;
9+
export const VERSION: string = require('../../package.json').version;

packages/jest-repl/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
},
77
"references": [
88
{"path": "../jest-config"},
9+
{"path": "../jest-console"},
10+
{"path": "../jest-environment"},
911
{"path": "../jest-runtime"},
1012
{"path": "../jest-transform"},
1113
{"path": "../jest-types"},
12-
{"path": "../jest-validate"}
14+
{"path": "../jest-util"},
15+
{"path": "../jest-validate"},
16+
{"path": "../test-utils"}
1317
]
1418
}

0 commit comments

Comments
 (0)