Skip to content

Commit

Permalink
Added jest-runtime CLI (#1193)
Browse files Browse the repository at this point in the history
* Move Console/NullConsole from jest-cli to jest-util

* Added CLI to jest-runtime

This allows any script to be run within the Jest environment.

Usage: jest-runtime --config <config> <path to script>

* Feedback for jest-runtime CLI

* Moved types out of jest-runtime
  • Loading branch information
sampepose authored and cpojer committed Jun 21, 2016
1 parent 598b775 commit 4f2ce5f
Show file tree
Hide file tree
Showing 20 changed files with 338 additions and 135 deletions.
2 changes: 1 addition & 1 deletion packages/jest-cli/src/SearchSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

import type {HasteResolverContext} from './types';
import type {HasteResolverContext} from 'types/Runtime';
import type {Path} from 'types/Config';
import type {ResolveModuleConfig} from '../../jest-resolve/src';

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/src/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import type {Path, Config} from 'types/Config';
import type Resolver from '../../jest-resolve/src';

const Console = require('./Console');
const NullConsole = require('./NullConsole');
const Console = require('jest-util').Console;
const NullConsole = require('jest-util').NullConsole;

class Test {

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
TestResult,
} from 'types/TestResult';
import type {Config, Path} from 'types/Config';
import type {HasteResolverContext} from './types';
import type {HasteResolverContext} from 'types/Runtime';

const Test = require('./Test');

Expand Down
26 changes: 13 additions & 13 deletions packages/jest-cli/src/__tests__/SearchSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ let normalizeConfig;

describe('SearchSource', () => {
const name = 'SearchSource';
let buildHasteMap;
let Runtime;
let hasteMap;
let SearchSource;
let searchSource;

beforeEach(() => {
buildHasteMap = require('../lib/buildHasteMap');
Runtime = require('jest-runtime');
SearchSource = require('../SearchSource');
normalizeConfig = require('jest-config').normalize;
});
Expand All @@ -41,7 +41,7 @@ describe('SearchSource', () => {
rootDir: '.',
testPathDirs: [],
});
hasteMap = buildHasteMap(config, {maxWorkers});
hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
searchSource = new SearchSource(hasteMap, config);
});

Expand Down Expand Up @@ -75,7 +75,7 @@ describe('SearchSource', () => {
moduleFileExtensions: ['js', 'jsx', 'txt'],
testRegex: 'not-really-a-test',
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -94,7 +94,7 @@ describe('SearchSource', () => {
moduleFileExtensions: ['js', 'jsx'],
testRegex: 'test\.jsx?',
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -113,7 +113,7 @@ describe('SearchSource', () => {
rootDir,
testRegex,
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -132,7 +132,7 @@ describe('SearchSource', () => {
testRegex,
moduleFileExtensions: ['jsx'],
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -151,7 +151,7 @@ describe('SearchSource', () => {
testRegex,
moduleFileExtensions: ['foobar'],
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -170,7 +170,7 @@ describe('SearchSource', () => {
testRegex,
moduleFileExtensions: ['js', 'jsx'],
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -189,7 +189,7 @@ describe('SearchSource', () => {
rootDir,
testDirectoryName: '__testtests__',
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -207,7 +207,7 @@ describe('SearchSource', () => {
rootDir,
testFileExtensions: ['js', 'jsx'],
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand All @@ -228,7 +228,7 @@ describe('SearchSource', () => {
testDirectoryName: '__testtests__',
testFileExtensions: ['js', 'jsx', 'foobar'],
});
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const searchSource = new SearchSource(hasteMap, config);
return searchSource.findMatchingTests().then(data => {
const relPaths = data.paths.map(absPath => (
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('SearchSource', () => {
name: 'SearchSource-findRelatedTests-tests',
rootDir,
});
hasteMap = buildHasteMap(config, {maxWorkers});
hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
searchSource = new SearchSource(hasteMap, config);
});

Expand Down
39 changes: 2 additions & 37 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,7 @@

'use strict';

function wrap(desccription: string) {
const indent = '\n ';
return indent + desccription.split(' ').reduce((wrappedDesc, word) => {
const lastLineIdx = wrappedDesc.length - 1;
const lastLine = wrappedDesc[lastLineIdx];

const appendedLastLine = lastLine === '' ? word : (lastLine + ' ' + word);

if (appendedLastLine.length > 80) {
wrappedDesc.push(word);
} else {
wrappedDesc[lastLineIdx] = appendedLastLine;
}

return wrappedDesc;
}, ['']).join(indent);
}
const wrap = require('jest-util').wrap;

const check = (argv: Object) => {
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
Expand Down Expand Up @@ -57,23 +41,6 @@ const check = (argv: Object) => {
return true;
};

const warnAboutUnrecognizedOptions = (argv: Object, options: Object) => {
const yargsSpecialOptions = ['$0', '_'];
const allowedOptions = Object.keys(options).reduce((acc, option) => (
acc
.add(option)
.add(options[option].alias)
), new Set(yargsSpecialOptions));
const unrecognizedOptions = Object.keys(argv).filter(arg => (
!allowedOptions.has(arg)
));
if (unrecognizedOptions.length) {
console.warn(
'Unrecognized options: ' + unrecognizedOptions.join(', ')
);
}
};

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

const options = {
Expand Down Expand Up @@ -286,9 +253,7 @@ const options = {
};

module.exports = {
wrap,
check,
usage,
options,
warnAboutUnrecognizedOptions,
usage,
};
20 changes: 3 additions & 17 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,19 @@
const args = require('./args');
const fs = require('fs');
const getJest = require('./getJest');
const getPackageRoot = require('jest-util').getPackageRoot;
const path = require('path');
const warnAboutUnrecognizedOptions = require('jest-util').warnAboutUnrecognizedOptions;
const yargs = require('yargs');

function getPackageRoot() {
const cwd = process.cwd();

// Is the cwd somewhere within an npm package?
let root = cwd;
while (!fs.existsSync(path.join(root, 'package.json'))) {
if (root === '/' || root.match(/^[A-Z]:\\/)) {
root = cwd;
break;
}
root = path.resolve(root, '..');
}

return root;
}

function Run() {
const argv = yargs
.usage(args.usage)
.options(args.options)
.check(args.check)
.argv;

args.warnAboutUnrecognizedOptions(argv, args.options);
warnAboutUnrecognizedOptions(argv, args.options);

if (argv.help) {
yargs.showHelp();
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-cli/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fs.gracefulify(realFs);
const TestRunner = require('./TestRunner');
const SearchSource = require('./SearchSource');

const buildHasteMap = require('./lib/buildHasteMap');
const Runtime = require('jest-runtime');
const chalk = require('chalk');
const constants = require('./constants');
const formatTestResults = require('./lib/formatTestResults');
Expand Down Expand Up @@ -85,7 +85,7 @@ function getWatcher(config, packageRoot, callback) {
function runJest(config, argv, pipe, onComplete) {
const patternInfo = buildTestPathPatternInfo(argv);
const maxWorkers = getMaxWorkers(argv);
const hasteMap = buildHasteMap(config, {maxWorkers});
const hasteMap = Runtime.buildHasteMap(config, {maxWorkers});
const source = new SearchSource(hasteMap, config);
return source.getTestPaths(patternInfo)
.then(data => {
Expand Down Expand Up @@ -196,7 +196,6 @@ function runCLI(argv: Object, root: Path, onComplete: () => void) {
}

module.exports = {
buildHasteMap,
getVersion: () => constants.VERSION,
runCLI,
SearchSource,
Expand Down
43 changes: 0 additions & 43 deletions packages/jest-cli/src/lib/buildHasteMap.js

This file was deleted.

16 changes: 16 additions & 0 deletions packages/jest-runtime/bin/jest-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'test';
}

require('../build/cli').Run();
12 changes: 9 additions & 3 deletions packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,29 @@
"main": "build/index.js",
"dependencies": {
"graceful-fs": "^4.1.3",
"jest-config": "^12.1.0",
"jest-environment-jsdom": "^12.1.0",
"jest-haste-map": "^12.1.0",
"jest-mock": "^12.1.0",
"jest-resolve": "^12.1.0",
"jest-snapshot": "^12.1.0",
"jest-util": "^12.1.0",
"json-stable-stringify": "^1.0.0"
"json-stable-stringify": "^1.0.0",
"yargs": "^4.7.1"
},
"bin": {
"jest-runtime": "./bin/jest-runtime.js"
},
"devDependencies": {
"jest-config": "^12.1.0"
},
"jest": {
"automock": false,
"rootDir": "./build",
"testEnvironment": "jest-environment-node",
"testPathIgnorePatterns": [
"/__tests__/[^/]*/.+"
],
"testEnvironment": "jest-environment-node"
]
},
"scripts": {
"test": "../../packages/jest-cli/bin/jest.js"
Expand Down

0 comments on commit 4f2ce5f

Please sign in to comment.