Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Migrate jest-repl to typescript #8000

Merged
merged 9 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968))
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988))
- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987))
- `[jest-repl]`: Migrate to TypeScript ([#8000](https://github.com/facebook/jest/pull/8000))

### Performance

Expand Down
3 changes: 3 additions & 0 deletions packages/jest-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/transform": "^24.1.0",
"@jest/types": "^24.1.0",
"jest-config": "^24.1.0",
"jest-runtime": "^24.1.0",
"jest-validate": "^24.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import Runtime from 'jest-runtime';

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

export const options = {
...Runtime.getCLIOptions(),
export const options = Object.assign({}, Runtime.getCLIOptions(), {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this gets a better type.

replname: {
alias: 'r',
description:
'The "name" of the file given to transformers to be ' +
'transformed. For example, "repl.ts" if using a TypeScript transformer.',
type: 'string',
},
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import path from 'path';

import Runtime from 'jest-runtime';
import yargs from 'yargs';
// @ts-ignore: Wait for jest-validate to get migrated
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries} from 'jest-config';
import {version as VERSION} from '../../package.json';
import * as args from './args';

const REPL_SCRIPT = path.resolve(__dirname, './repl.js');
const {version: VERSION} = require('../../package.json');

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

module.exports = function() {
export = function() {
const argv = yargs.usage(args.usage).options(args.options).argv;

validateCLIOptions(argv, {...args.options, deprecationEntries});

argv._ = [REPL_SCRIPT];

// @ts-ignore: not the same arguments
Runtime.runCLI(argv, [`Jest REPL v${VERSION}`]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,37 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {GlobalConfig, ProjectConfig} from 'types/Config';

declare var jestGlobalConfig: GlobalConfig;
declare var jestProjectConfig: ProjectConfig;
declare var jest: Object;
declare const jestGlobalConfig: Config.GlobalConfig;
declare const jestProjectConfig: Config.ProjectConfig;

import path from 'path';
import repl from 'repl';
import vm from 'vm';
import {Transformer} from '@jest/transform';
import {Config} from '@jest/types';

let transformer;
let transformer: Transformer;
natealcedo marked this conversation as resolved.
Show resolved Hide resolved

const evalCommand = (cmd, context, filename, callback, config) => {
const evalCommand: repl.REPLEval = (
cmd: string,
_context: any,
_filename: string,
callback: (e: Error | null, result?: any) => void,
) => {
let result;
try {
if (transformer) {
cmd = transformer.process(
const transformResult = transformer.process(
cmd,
jestGlobalConfig.replname || 'jest.js',
SimenB marked this conversation as resolved.
Show resolved Hide resolved
jestProjectConfig,
);
cmd =
typeof transformResult === 'string'
? transformResult
: transformResult.code;
}
result = vm.runInThisContext(cmd);
} catch (e) {
Expand All @@ -37,7 +44,7 @@ const evalCommand = (cmd, context, filename, callback, config) => {
return callback(null, result);
};

const isRecoverableError = error => {
const isRecoverableError = (error: Error) => {
if (error && error.name === 'SyntaxError') {
return [
'Unterminated template',
Expand All @@ -59,7 +66,6 @@ if (jestProjectConfig.transform) {
}
}
if (transformerPath) {
/* $FlowFixMe */
transformer = require(transformerPath);
if (typeof transformer.process !== 'function') {
throw new TypeError(
Expand All @@ -69,17 +75,15 @@ if (jestProjectConfig.transform) {
}
}

const replInstance = repl.start({
const replInstance: repl.REPLServer = repl.start({
eval: evalCommand,
prompt: '\u203A ',
useGlobal: true,
});

// $FlowFixMe: https://github.com/facebook/flow/pull/4713
replInstance.context.require = moduleName => {
replInstance.context.require = (moduleName: string) => {
if (/(\/|\\|\.)/.test(moduleName)) {
moduleName = path.resolve(process.cwd(), moduleName);
}
/* $FlowFixMe */
return require(moduleName);
};
15 changes: 15 additions & 0 deletions packages/jest-repl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../jest-config"},
{"path": "../jest-runtime"},
{"path": "../jest-transform"},
{"path": "../jest-types"}
// TODO: Wait for this to get migrated
// {"path": "../jest-validate"}
]
}