From 3f11accb85e7eb02a2a7bed4453dd883c8d4e761 Mon Sep 17 00:00:00 2001 From: Neil Kistner Date: Wed, 12 Apr 2017 17:32:11 -0500 Subject: [PATCH 1/2] Add --showConfig argument --- docs/CLI.md | 4 + .../__snapshots__/showConfig-test.js.snap | 83 +++++++++++++++++++ integration_tests/__tests__/debug-test.js | 6 +- .../__tests__/showConfig-test.js | 39 +++++++++ packages/jest-cli/src/cli/args.js | 4 + packages/jest-cli/src/cli/runCLI.js | 6 +- .../lib/__tests__/logDebugMessages-test.js | 48 ++++++----- packages/jest-cli/src/lib/logDebugMessages.js | 11 ++- packages/jest-editor-support/src/Settings.js | 30 ++----- .../src/__tests__/Settings-test.js | 51 ++++++++++++ 10 files changed, 228 insertions(+), 54 deletions(-) create mode 100644 integration_tests/__tests__/__snapshots__/showConfig-test.js.snap create mode 100644 integration_tests/__tests__/showConfig-test.js create mode 100644 packages/jest-editor-support/src/__tests__/Settings-test.js diff --git a/docs/CLI.md b/docs/CLI.md index 4829c754b261..b99aef240e96 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -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. diff --git a/integration_tests/__tests__/__snapshots__/showConfig-test.js.snap b/integration_tests/__tests__/__snapshots__/showConfig-test.js.snap new file mode 100644 index 000000000000..7647012a32d0 --- /dev/null +++ b/integration_tests/__tests__/__snapshots__/showConfig-test.js.snap @@ -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 + } +} + +`; diff --git a/integration_tests/__tests__/debug-test.js b/integration_tests/__tests__/debug-test.js index 5c6ac611a982..79b16a28ceef 100644 --- a/integration_tests/__tests__/debug-test.js +++ b/integration_tests/__tests__/debug-test.js @@ -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 }); }); diff --git a/integration_tests/__tests__/showConfig-test.js b/integration_tests/__tests__/showConfig-test.js new file mode 100644 index 000000000000..99721035addb --- /dev/null +++ b/integration_tests/__tests__/showConfig-test.js @@ -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(); + }); +}); diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index aee0ec265809..884e8441e033 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -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.', diff --git a/packages/jest-cli/src/cli/runCLI.js b/packages/jest-cli/src/cli/runCLI.js index c84f06c2afbd..d0f5526d0dcb 100644 --- a/packages/jest-cli/src/cli/runCLI.js +++ b/packages/jest-cli/src/cli/runCLI.js @@ -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), diff --git a/packages/jest-cli/src/lib/__tests__/logDebugMessages-test.js b/packages/jest-cli/src/lib/__tests__/logDebugMessages-test.js index 1b1d1120b677..78ecba3b21d6 100644 --- a/packages/jest-cli/src/lib/__tests__/logDebugMessages-test.js +++ b/packages/jest-cli/src/lib/__tests__/logDebugMessages-test.js @@ -8,6 +8,8 @@ * @emails oncall+jsinfra */ +'use strict'; + const logDebugMessages = require('../logDebugMessages'); jest.mock('../../../package.json', () => ({version: 123})); @@ -15,42 +17,42 @@ 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'); }); }); diff --git a/packages/jest-cli/src/lib/logDebugMessages.js b/packages/jest-cli/src/lib/logDebugMessages.js index 5551a1d74187..e223fb3ba928 100644 --- a/packages/jest-cli/src/lib/logDebugMessages.js +++ b/packages/jest-cli/src/lib/logDebugMessages.js @@ -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; diff --git a/packages/jest-editor-support/src/Settings.js b/packages/jest-editor-support/src/Settings.js index 8779edde703c..c26da37f19a4 100644 --- a/packages/jest-editor-support/src/Settings.js +++ b/packages/jest-editor-support/src/Settings.js @@ -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'); @@ -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(); }); } }; diff --git a/packages/jest-editor-support/src/__tests__/Settings-test.js b/packages/jest-editor-support/src/__tests__/Settings-test.js new file mode 100644 index 000000000000..6c91c4cf23cd --- /dev/null +++ b/packages/jest-editor-support/src/__tests__/Settings-test.js @@ -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); +}; From 5617a2f995c5d6294bdbfdde6d73eff361feb75f Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Mon, 17 Apr 2017 17:04:10 -0700 Subject: [PATCH 2/2] Update CLI.md --- docs/CLI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CLI.md b/docs/CLI.md index b99aef240e96..3116b0321803 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -163,7 +163,7 @@ The path to a module that runs some code to configure or set up the testing fram ### `--showConfig` -Print your jest config and then exits. +Print your Jest config and then exits. ### `--silent`