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

Added jest-runtime CLI #1193

Merged
merged 4 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 '../../jest-runtime/src/types';
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 '../../jest-runtime/src/types';

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
17 changes: 1 addition & 16 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,7 @@ const fs = require('fs');
const getJest = require('./getJest');
const path = require('path');
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;
}
const getPackageRoot = require('jest-util').getPackageRoot;

function Run() {
const argv = yargs
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';
Copy link
Member

Choose a reason for hiding this comment

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

Originally I thought we won't need this, but I think it is a good default for preprocessors etc. that require a test environment. I think it's fine.

}

require('../build/cli').Run();
10 changes: 8 additions & 2 deletions packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
"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"
Expand All @@ -25,7 +30,8 @@
"testPathIgnorePatterns": [
"/__tests__/[^/]*/.+"
],
"testEnvironment": "jest-environment-node"
"testEnvironment": "jest-environment-node",
"automock": false
Copy link
Member

Choose a reason for hiding this comment

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

put that on top of the jest config please. alphabetize the config (I realize it wasn't alphabetized before).

},
"scripts": {
"test": "../../packages/jest-cli/bin/jest.js"
Expand Down
52 changes: 52 additions & 0 deletions packages/jest-runtime/src/__tests__/Runtime-cli-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2014-present, 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.
*
* @emails oncall+jsinfra
*/
'use strict';

const spawnSync = require('child_process').spawnSync;
const path = require('path');

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

jest.disableAutomock();

describe('Runtime', () => {
describe('cli', () => {
it('fails with no path', () => {
const expectedOutput =
'Please provide a path to a script. (See --help for details)';
const output = spawnSync(JEST_RUNTIME, [], {
encoding : 'utf8',
cwd: process.cwd(),
env: process.env,
});
expect(output.stdout.trim()).toBe(expectedOutput);
});

it('displays script output', () => {
const scriptPath = path.resolve(__dirname, './test_root/logging.js');
const output = spawnSync(JEST_RUNTIME, [scriptPath], {
encoding : 'utf8',
cwd: process.cwd(),
env: process.env,
});
expect(output.stdout.trim()).toBe('Hello, world!');
});

it('throws script errors', () => {
const scriptPath = path.resolve(__dirname, './test_root/throwing.js');
const output = spawnSync(JEST_RUNTIME, [scriptPath], {
encoding : 'utf8',
cwd: process.cwd(),
env: process.env,
});
expect(output.stderr.trim()).toMatch('Error: throwing');
});
});
});
11 changes: 11 additions & 0 deletions packages/jest-runtime/src/__tests__/test_root/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2014-present, 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';

console.log('Hello, world!');