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 3 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
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",
"@types/jest": "^24.0.9",
"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,28 @@
* 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 {version: VERSION} = require("../../package.json");

const REPL_SCRIPT = path.resolve(__dirname, './repl.js');

module.exports = function() {
exports = function() {
natealcedo marked this conversation as resolved.
Show resolved Hide resolved
const argv = yargs.usage(args.usage).options(args.options).argv;

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

argv._ = [REPL_SCRIPT];

//@ts-ignore Get feedback first in PR
Copy link
Author

Choose a reason for hiding this comment

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

The compiler complains that argv has some keys missing. Not sure how to resolve this at the moment.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the argv typing is not really where it should be. this is fine for now 🙂

Runtime.runCLI(argv, [`Jest REPL v${VERSION}`]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@
* 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 var jestGlobalConfig: Config.GlobalConfig;
SimenB marked this conversation as resolved.
Show resolved Hide resolved
declare var 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,
_config?: any
SimenB marked this conversation as resolved.
Show resolved Hide resolved
) => {
let result;
try {
if (transformer) {
cmd = transformer.process(
cmd,
jestGlobalConfig.replname || 'jest.js',
SimenB marked this conversation as resolved.
Show resolved Hide resolved
jestProjectConfig,
);
) as string;
SimenB marked this conversation as resolved.
Show resolved Hide resolved
}
result = vm.runInThisContext(cmd);
} catch (e) {
Expand All @@ -37,7 +41,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 +63,6 @@ if (jestProjectConfig.transform) {
}
}
if (transformerPath) {
/* $FlowFixMe */
transformer = require(transformerPath);
if (typeof transformer.process !== 'function') {
throw new TypeError(
Expand All @@ -69,17 +72,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);
};
27 changes: 27 additions & 0 deletions packages/jest-repl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"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"
}
*/
]
}