Skip to content

Commit

Permalink
Add --showConfig argument
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Apr 14, 2017
1 parent 6517c7b commit 23c5f64
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 53 deletions.
4 changes: 4 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ Alias: `-i`. Run all tests serially in the current process, rather than creating

The path to a module that runs some code to configure or set up the testing framework before each test.

### `--showConfig`

Print your jest config and then exits.

### `--silent`

Prevent tests from printing messages through the console.
Expand Down
83 changes: 83 additions & 0 deletions integration_tests/__tests__/__snapshots__/showConfig-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jest --showConfig outputs config info and exits 1`] = `
{
"version": "19.0.2",
"framework": "jasmine2",
"config": {
"automock": false,
"bail": false,
"browser": false,
"cacheDirectory": "/mocked/root/path/integration_tests/showConfig/tmp",
"clearMocks": false,
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"coverageReporters": [
"json",
"text",
"lcov",
"clover"
],
"expand": false,
"globals": {},
"haste": {
"providesModuleNodeModules": []
},
"mapCoverage": false,
"moduleDirectories": [
"node_modules"
],
"moduleFileExtensions": [
"js",
"json",
"jsx",
"node"
],
"moduleNameMapper": {},
"modulePathIgnorePatterns": [],
"noStackTrace": false,
"notify": false,
"preset": null,
"resetMocks": false,
"resetModules": false,
"roots": [
"/mocked/root/path/integration_tests/showConfig"
],
"snapshotSerializers": [],
"testEnvironment": "/mocked/root/path/packages/jest-environment-node/build/index.js",
"testMatch": [
"**/__tests__/**/*.js?(x)",
"**/?(*.)(spec|test).js?(x)"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"testRegex": "",
"testResultsProcessor": null,
"testURL": "about:blank",
"timers": "real",
"transformIgnorePatterns": [
"/node_modules/"
],
"useStderr": false,
"verbose": null,
"watch": false,
"rootDir": "/mocked/root/path/integration_tests/showConfig",
"name": "9899e51dcb776e8e85239505414e0d13",
"setupFiles": [
"/mocked/root/path/node_modules/regenerator-runtime/runtime.js"
],
"testRunner": "/mocked/root/path/packages/jest-jasmine2/build/index.js",
"transform": [
[
"^.+\\\\.jsx?$",
"/mocked/root/path/integration_tests/showConfig/node_modules/babel-jest/build/index.js"
]
],
"cache": false,
"watchman": true
}
}
`;
6 changes: 3 additions & 3 deletions integration_tests/__tests__/debug-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe('jest --debug', () => {

it('outputs debugging info before running the test', () => {
const {stdout} = runJest(dir, ['--debug', '--no-cache']);
expect(stdout).toMatch('jest version =');
expect(stdout).toMatch('test framework = jasmine2');
expect(stdout).toMatch('config = {');
expect(stdout).toMatch('"version": "');
expect(stdout).toMatch('"framework": "jasmine2",');
expect(stdout).toMatch('"config": {');
// config contains many file paths so we cannot do snapshot test
});
});
36 changes: 36 additions & 0 deletions integration_tests/__tests__/showConfig-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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';

const {linkJestPackage} = require('../utils');
const path = require('path');
const runJest = require('../runJest');
const skipOnWindows = require('skipOnWindows');

describe('jest --showConfig', () => {
skipOnWindows.suite();

const dir = path.resolve(__dirname, '..', 'showConfig');

beforeEach(() => {
if (process.platform !== 'win32') {
linkJestPackage('babel-jest', dir);
}
});

it('outputs config info and exits', () => {
const root = path.join(__dirname, '..', '..');
expect.addSnapshotSerializer({
print: val => val.replace(new RegExp(root, 'g'), '/mocked/root/path'),
test: val => typeof val === 'string' && val.indexOf(root) > -1,
});
const {stdout} = runJest(dir, ['--showConfig', '--no-cache']);
expect(stdout).toMatchSnapshot();
});
});
6 changes: 6 additions & 0 deletions integration_tests/showConfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"jest": {
"cacheDirectory": "tmp",
"testEnvironment": "node"
}
}
4 changes: 4 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ const options = {
'set up the testing framework before each test.',
type: 'string',
},
showConfig: {
description: 'Print your jest config and then exits.',
type: 'boolean',
},
silent: {
default: false,
description: 'Prevent tests from printing messages through the console.',
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-cli/src/cli/runCLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ module.exports = (
}

const _run = async ({config, hasDeprecationWarnings}) => {
if (argv.debug) {
if (argv.debug || argv.showConfig) {
logDebugMessages(config, pipe);
}

if (argv.showConfig) {
process.exit(0);
}

createDirectory(config.cacheDirectory);
const hasteMapInstance = Runtime.createHasteMap(config, {
console: new Console(pipe, pipe),
Expand Down
41 changes: 18 additions & 23 deletions packages/jest-cli/src/lib/__tests__/logDebugMessages-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,37 @@ jest.mock('../../../package.json', () => ({version: 123}));
jest.mock('myRunner', () => ({name: 'My Runner'}), {virtual: true});

const getPipe = () => ({write: jest.fn()});
/* eslint-disable */
const makeOutput = (config = { testRunner: 'myRunner' }) => JSON.stringify({
version: 123,
framework: 'My Runner',
config,
}, null, ' ');
/* eslint-enable */

describe('logDebugMessages', () => {
it('Prints the jest version', () => {
const pipe = getPipe();
logDebugMessages({testRunner: 'myRunner'}, pipe);
expect(pipe.write).toHaveBeenCalledWith('jest version = 123\n');
expect(pipe.write).toHaveBeenCalledWith(makeOutput() + '\n');
});

it('Prints the test framework name', () => {
const pipe = getPipe();
logDebugMessages({testRunner: 'myRunner'}, pipe);
expect(pipe.write).toHaveBeenCalledWith('test framework = My Runner\n');
expect(pipe.write).toHaveBeenCalledWith(makeOutput() + '\n');
});

it('Prints the config object', () => {
const pipe = getPipe();
logDebugMessages(
{
automock: false,
rootDir: '/path/to/dir',
roots: ['path/to/dir/test'],
testRunner: 'myRunner',
watch: true,
},
pipe,
);
expect(pipe.write).toHaveBeenCalledWith(
`config = {
"automock": false,
"rootDir": "/path/to/dir",
"roots": [
"path/to/dir/test"
],
"testRunner": "myRunner",
"watch": true
}\n`,
);
const config = {
automock: false,
rootDir: '/path/to/dir',
roots: ['path/to/dir/test'],
testRunner: 'myRunner',
watch: true,
};
logDebugMessages(config, pipe);
expect(pipe.write).toHaveBeenCalledWith(makeOutput(config) + '\n');
});
});
11 changes: 8 additions & 3 deletions packages/jest-cli/src/lib/logDebugMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ const logDebugMessages = (
): void => {
/* $FlowFixMe */
const testFramework = require(config.testRunner);
pipe.write('jest version = ' + VERSION + '\n');
pipe.write('test framework = ' + testFramework.name + '\n');
pipe.write('config = ' + JSON.stringify(config, null, ' ') + '\n');
/* eslint-disable sort-keys */
const output = {
version: VERSION,
framework: testFramework.name,
config,
};
/* eslint-enable sort-keys */
pipe.write(JSON.stringify(output, null, ' ') + '\n');
};

module.exports = logDebugMessages;
29 changes: 6 additions & 23 deletions packages/jest-editor-support/src/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,18 @@ module.exports = class Settings extends EventEmitter {
}

getConfig(completed: any) {
// It'll want to run tests, we don't want that, so tell it to run tests
// in a non-existant folder.
const folderThatDoesntExist = 'hi-there-danger-are-you-following-along';
const args = ['--debug', folderThatDoesntExist];
this.debugprocess = this._createProcess(this.workspace, args);
this.debugprocess = this._createProcess(this.workspace, ['--showConfig']);

this.debugprocess.stdout.on('data', (data: Buffer) => {
const string = data.toString();
const {config, version} = JSON.parse(data.toString());
// We can give warnings to versions under 17 now
// See https://github.com/facebook/jest/issues/2343 for moving this into
// the config object
if (string.includes('jest version =')) {
const version = string
.split('jest version =')
.pop()
.split(EOL)[0]
.trim();
this.jestVersionMajor = parseInt(version, 10);
}

// Pull out the data for the config
if (string.includes('config =')) {
const jsonString = string
.split('config =')
.pop()
.split('No tests found')[0];
this.settings = JSON.parse(jsonString);
completed();
}
this.jestVersionMajor = parseInt(version.split('.').shift(), 10);
this.settings = config;

completed();
});
}
};
33 changes: 33 additions & 0 deletions packages/jest-editor-support/src/__tests__/Settings-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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';

const ProjectWorkspace = require('../ProjectWorkspace');
const Settings = require('../Settings');

describe('Settings', () => {
it('sets itself up fom the constructor', () => {
const workspace = new ProjectWorkspace('root_path', 'path_to_jest');
const settings = new Settings(workspace);
expect(settings.workspace).toEqual(workspace);
expect(settings.settings).toEqual(expect.any(Object));
});

it('reads and parses the config', () => {
const jestPath = 'packages/jest/node_modules/.bin/jest';
const workspace = new ProjectWorkspace('.', jestPath);
const completed = jest.fn();
const settings = new Settings(workspace);

settings.getConfig(completed);

// Should have been called but isn't
expect(completed).not.toHaveBeenCalled();
});
});

0 comments on commit 23c5f64

Please sign in to comment.