Skip to content

Commit

Permalink
Add --showConfig argument (jestjs#3296)
Browse files Browse the repository at this point in the history
* Add --showConfig argument

* Update CLI.md
  • Loading branch information
wyze authored and cpojer committed Apr 18, 2017
1 parent 7f68960 commit 2774f20
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 54 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": "/tmp/jest",
"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/jest/integration_tests/verbose_reporter"
],
"snapshotSerializers": [],
"testEnvironment": "/mocked/root/path/jest/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/jest/integration_tests/verbose_reporter",
"name": "[md5 hash]",
"setupFiles": [
"/mocked/root/path/jest/node_modules/regenerator-runtime/runtime.js"
],
"testRunner": "/mocked/root/path/jest/packages/jest-jasmine2/build/index.js",
"transform": [
[
"^.+\\\\.jsx?$",
"/mocked/root/path/jest/integration_tests/verbose_reporter/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
});
});
39 changes: 39 additions & 0 deletions integration_tests/__tests__/showConfig-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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, '..', 'verbose_reporter');

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')
.replace(/"name": "(.+)"/, '"name": "[md5 hash]"')
.replace(/"cacheDirectory": "(.+)"/, '"cacheDirectory": "/tmp/jest"'),
test: val => typeof val === 'string',
});
const {stdout} = runJest(dir, ['--showConfig', '--no-cache']);
expect(stdout).toMatchSnapshot();
});
});
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
48 changes: 25 additions & 23 deletions packages/jest-cli/src/lib/__tests__/logDebugMessages-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,51 @@
* @emails oncall+jsinfra
*/

'use strict';

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

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;
30 changes: 6 additions & 24 deletions packages/jest-editor-support/src/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

const {ChildProcess} = require('child_process');
const EventEmitter = require('events');
const {EOL} = require('os');
const ProjectWorkspace = require('./ProjectWorkspace');
const {createProcess} = require('./Process');

Expand Down Expand Up @@ -58,35 +57,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();
});
}
};
51 changes: 51 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,51 @@
/**
* 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 {EventEmitter} = require('events');
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 workspace = new ProjectWorkspace('root_path', 'path_to_jest');
const completed = jest.fn();
const config = {cacheDirectory: '/tmp/jest', name: '[md5 hash]'};
const json = {
config,
version: '19.0.0',
};
const createProcess = () => ({stdout: new EventEmitter()});
const buffer = makeBuffer(JSON.stringify(json));
const settings = new Settings(workspace, {createProcess});

settings.getConfig(completed);
settings.debugprocess.stdout.emit('data', buffer);

expect(completed).toHaveBeenCalled();
expect(settings.jestVersionMajor).toBe(19);
expect(settings.settings).toEqual(config);
});
});

const makeBuffer = (content: string) => {
// Buffer.from is not supported in < Node 5.10
if (typeof Buffer.from === 'function') {
return Buffer.from(content);
}

return new Buffer(content);
};

0 comments on commit 2774f20

Please sign in to comment.