Skip to content

Commit

Permalink
[WIP] Migrate jest-repl to typescript (#8000)
Browse files Browse the repository at this point in the history
  • Loading branch information
natealcedo authored and SimenB committed Feb 27, 2019
1 parent 4023b8c commit 438a178
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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(), {
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',
},
};
});
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}`]);
};
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;

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',
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
@@ -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"}
]
}

0 comments on commit 438a178

Please sign in to comment.