Skip to content

Commit

Permalink
Move jest-runtime CLI into jest-repl
Browse files Browse the repository at this point in the history
Fixes #10011
  • Loading branch information
ghostd committed May 10, 2020
1 parent 0e0eeed commit 457b17f
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'test';
}

require('../build/cli').run();
require('../build/cli/runtime-cli').run();
8 changes: 7 additions & 1 deletion packages/jest-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/console": "^26.0.1",
"@jest/environment": "^26.0.1",
"@jest/transform": "^26.0.1",
"@jest/types": "^26.0.1",
"@types/yargs": "^15.0.0",
"chalk": "^4.0.0",
"jest-config": "^26.0.1",
"jest-runtime": "^26.0.1",
"jest-util": "^26.0.1",
"jest-validate": "^26.0.1",
"repl": "^0.1.3",
"yargs": "^15.3.1"
},
"devDependencies": {
"@jest/test-utils": "^26.0.0",
"@types/yargs": "^15.0.0"
"@types/yargs": "^15.0.0",
"execa": "^4.0.0"
},
"bin": "./bin/jest-repl.js",
"engines": {
Expand Down
53 changes: 53 additions & 0 deletions packages/jest-repl/src/__tests__/runtime_cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
// eslint-disable-next-line import/named
import {sync as spawnSync} from 'execa';
import {skipSuiteOnWindows} from '@jest/test-utils';

skipSuiteOnWindows();

const JEST_RUNTIME = path.resolve(__dirname, '../../bin/jest-runtime-cli.js');

const run = args =>
spawnSync(JEST_RUNTIME, args, {
cwd: process.cwd(),
env: process.env,
reject: false,
});

describe('Runtime CLI', () => {
it('fails with no path', () => {
const expectedOutput =
'Please provide a path to a script. (See --help for details)';
expect(run([]).stdout).toBe(expectedOutput);
});

it('displays script output', () => {
const scriptPath = path.resolve(__dirname, './test_root/logging.js');
expect(run([scriptPath, '--no-cache']).stdout).toMatch('Hello, world!');
});

it('always disables automocking', () => {
const scriptPath = path.resolve(__dirname, './test_root/logging.js');
const output = run([
scriptPath,
'--no-cache',
'--config=' +
JSON.stringify({
automock: true,
}),
]);
expect(output.stdout).toMatch('Hello, world!');
});

it('throws script errors', () => {
const scriptPath = path.resolve(__dirname, './test_root/throwing.js');
expect(run([scriptPath, '--no-cache']).stderr).toMatch('Error: throwing');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

'use strict';

if (require('./RegularModule').getModuleStateValue()) {
if (
require('jest-runtime/src/__tests__/test_root/RegularModule').getModuleStateValue()
) {
console.log('Hello, world!');
} else {
console.log('Automocking is not properly disabled in jest-runtime.');
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-repl/src/__tests__/test_root/throwing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

throw new Error('throwing');
37 changes: 35 additions & 2 deletions packages/jest-repl/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,45 @@
*/

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

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

const runtimeCLIOptions: Record<
'cache' | 'config' | 'debug' | 'version' | 'watchman',
Options
> = {
cache: {
default: true,
description:
'Whether to use the preprocessor cache. Disable ' +
'the cache using --no-cache.',
type: 'boolean',
},
config: {
alias: 'c',
description: 'The path to a Jest config file.',
type: 'string',
},
debug: {
description: 'Print debugging info about your jest config.',
type: 'boolean',
},
version: {
alias: 'v',
description: 'Print the version and exit',
type: 'boolean',
},
watchman: {
default: true,
description:
'Whether to use watchman for file crawling. Disable using ' +
'--no-watchman.',
type: 'boolean',
},
};

export const options: Record<string, Options> = {
...Runtime.getCLIOptions(),
...runtimeCLIOptions,
replname: {
alias: 'r',
description:
Expand Down
7 changes: 3 additions & 4 deletions packages/jest-repl/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
*
*/

import Runtime = require('jest-runtime');
import yargs = require('yargs');
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries} from 'jest-config';
import type {Config} from '@jest/types';
import * as args from './args';

const {version: VERSION} = require('../../package.json');
import {run as runtimeCLI} from './runtime-cli';
import {VERSION} from './version';

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

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

argv._ = [REPL_SCRIPT];

Runtime.runCLI(argv, [`Jest REPL v${VERSION}`]);
runtimeCLI(argv, [`Jest REPL v${VERSION}`]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {cpus} from 'os';
import * as path from 'path';
import chalk = require('chalk');
import yargs = require('yargs');
import Runtime = require('jest-runtime');
import type {Config} from '@jest/types';
import type {JestEnvironment} from '@jest/environment';
import {CustomConsole} from '@jest/console';
import {setGlobal, tryRealpath} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries, readConfig} from 'jest-config';
import {VERSION} from '../version';
import type {Context} from '../types';
import type {Context} from 'jest-runtime';
import {VERSION} from './version';
import * as args from './args';

export async function run(
Expand Down Expand Up @@ -67,9 +68,6 @@ export async function run(
automock: false,
};

// Break circular dependency
const Runtime: any = require('..');

try {
const hasteMap: Context = await Runtime.createContext(config, {
maxWorkers: Math.max(cpus().length - 1, 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
*/

// For some reason, doing `require`ing here works, while inside `cli` fails
export const VERSION: string = require('../package.json').version;
export const VERSION: string = require('../../package.json').version;
6 changes: 5 additions & 1 deletion packages/jest-repl/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
},
"references": [
{"path": "../jest-config"},
{"path": "../jest-console"},
{"path": "../jest-environment"},
{"path": "../jest-runtime"},
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-validate"}
{"path": "../jest-util"},
{"path": "../jest-validate"},
{"path": "../test-utils"}
]
}
1 change: 0 additions & 1 deletion packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"exit": "^0.1.2",
"glob": "^7.1.3",
"graceful-fs": "^4.2.4",
"jest-config": "^26.0.1",
"jest-haste-map": "^26.0.1",
"jest-message-util": "^26.0.1",
"jest-mock": "^26.0.1",
Expand Down
55 changes: 0 additions & 55 deletions packages/jest-runtime/src/__tests__/runtime_cli.test.js

This file was deleted.

44 changes: 0 additions & 44 deletions packages/jest-runtime/src/cli/args.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ import {
import type {V8CoverageResult} from '@jest/test-result';
import {CoverageInstrumenter, V8Coverage} from 'collect-v8-coverage';
import * as fs from 'graceful-fs';
import {run as cliRun} from './cli';
import {options as cliOptions} from './cli/args';
import {findSiblingsWithFileExtension} from './helpers';
import type {Context as JestContext} from './types';
import jestMock = require('jest-mock');
Expand Down Expand Up @@ -322,14 +320,6 @@ class Runtime {
});
}

static runCLI(args?: Config.Argv, info?: Array<string>): Promise<void> {
return cliRun(args, info);
}

static getCLIOptions(): typeof cliOptions {
return cliOptions;
}

// unstable as it should be replaced by https://github.com/nodejs/modules/issues/393, and we don't want people to use it
unstable_shouldLoadAsEsm = Resolver.unstable_shouldLoadAsEsm;

Expand Down
1 change: 0 additions & 1 deletion packages/jest-runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"outDir": "build"
},
"references": [
{"path": "../jest-config"},
{"path": "../jest-console"},
{"path": "../jest-environment"},
{"path": "../jest-environment-node"},
Expand Down

0 comments on commit 457b17f

Please sign in to comment.