From ca97ebfeac093c491b7b4d4b19d86df3912c95de Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 12:37:32 -0500 Subject: [PATCH 01/24] add support for argv.config as an object to readConfig --- packages/jest-config/src/index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index b1832b94af6a..0cd41fd2b379 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -60,16 +60,20 @@ export function readConfig( 'Jest: Cannot use configuration as an object without a file path.', ); } - } else if (isJSONString(argv.config)) { + } else if (argv.config && (typeof argv.config === 'object' || isJSONString(argv.config))) { // A JSON string was passed to `--config` argument and we can parse it // and use as is. let config; - try { - config = JSON.parse(argv.config); - } catch (e) { - throw new Error( - 'There was an error while parsing the `--config` argument as a JSON string.', - ); + if (typeof argv.config === 'object') { + config = argv.config; + } else { + try { + config = JSON.parse(argv.config); + } catch (e) { + throw new Error( + 'There was an error while parsing the `--config` argument as a JSON string.', + ); + } } // NOTE: we might need to resolve this dir to an absolute path in the future From 2d27b56c464210d9236a1cf20e02dbf5224f72b7 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 12:38:23 -0500 Subject: [PATCH 02/24] only normalize config.transform if it is a string --- packages/jest-config/src/normalize.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index b29e3e690091..4491127e6573 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -535,11 +535,15 @@ export default function normalize(options: InitialOptions, argv: Argv) { transform && Object.keys(transform).map(regex => [ regex, - resolve(newOptions.resolver, { - filePath: transform[regex], - key, - rootDir: options.rootDir, - }), + typeof transform[regex] === 'string' + ? + resolve(newOptions.resolver, { + filePath: transform[regex], + key, + rootDir: options.rootDir, + }) + : + transform[regex] ]); break; case 'coveragePathIgnorePatterns': From 100017e0ad62e8ab01042ce368c85c06f1a6358b Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 12:41:02 -0500 Subject: [PATCH 03/24] preprocess using the transform if it is a function else require it --- .../jest-runtime/src/script_transformer.js | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index a25683156afd..f733ba924122 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -131,7 +131,7 @@ export default class ScriptTransformer { return cachePath; } - _getTransformPath(filename: Path) { + _getTransformFunctionOrPath(filename: Path) { for (let i = 0; i < this._config.transform.length; i++) { if (new RegExp(this._config.transform[i][0]).test(filename)) { return this._config.transform[i][1]; @@ -146,15 +146,21 @@ export default class ScriptTransformer { return null; } - const transformPath = this._getTransformPath(filename); - if (transformPath) { - const transformer = this._transformCache.get(transformPath); - if (transformer != null) { - return transformer; + const transformFunctionOrPath = this._getTransformFunctionOrPath(filename); + if (transformFunctionOrPath) { + let isTransformFunction = typeof transformFunctionOrPath === 'function'; + if (isTransformFunction) { + transform = transformFunctionOrPath(); + } + else { + const transformer = this._transformCache.get(transformFunctionOrPath); + if (transformer != null) { + return transformer; + } + + // $FlowFixMe + transform = (require(transformFunctionOrPath): Transformer); } - - // $FlowFixMe - transform = (require(transformPath): Transformer); if (typeof transform.createTransformer === 'function') { transform = transform.createTransformer(); } @@ -163,7 +169,9 @@ export default class ScriptTransformer { 'Jest: a transform must export a `process` function.', ); } - this._transformCache.set(transformPath, transform); + if (!isTransformFunction) { + this._transformCache.set(transformFunctionOrPath, transform); + } } return transform; } From a48205a5040e76938524e9ba3ebe4face7400de4 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 12:41:45 -0500 Subject: [PATCH 04/24] add normalize unit tests for transform as a function --- .../src/__tests__/normalize.test.js | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 28c2aea79422..8026aa904b5b 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -285,7 +285,7 @@ describe('transform', () => { Resolver.findNodeModule = jest.fn(name => name); }); - it('normalizes the path', () => { + it('normalizes the path if it is a string', () => { const {options} = normalize( { rootDir: '/root/', @@ -304,6 +304,28 @@ describe('transform', () => { ['abs-path', '/qux/quux'], ]); }); + + it('does not normalize the path if it is a function', () => { + const theFunction = () => {} + + const { options } = normalize( + { + rootDir: '/root/', + transform: { + [DEFAULT_CSS_PATTERN]: '/node_modules/jest-regex-util', + [DEFAULT_JS_PATTERN]: theFunction, + 'abs-path': '/qux/quux', + }, + }, + {}, + ); + + expect(options.transform).toEqual([ + [DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'], + [DEFAULT_JS_PATTERN, theFunction], + ['abs-path', '/qux/quux'], + ]); + }); }); describe('haste', () => { @@ -1077,6 +1099,8 @@ describe('preset', () => { }); test('merges with options', () => { + const aFunction = () => {}; + const {options} = normalize( { moduleNameMapper: {a: 'a'}, @@ -1084,7 +1108,7 @@ describe('preset', () => { preset: 'react-native', rootDir: '/root/path/foo', setupFiles: ['a'], - transform: {a: 'a'}, + transform: {a: 'a', c: aFunction}, }, {}, ); @@ -1098,6 +1122,7 @@ describe('preset', () => { expect(options.transform).toEqual([ ['a', '/node_modules/a'], ['b', '/node_modules/b'], + ['c', aFunction] ]); }); From 61e9fdd178966f3fbdfa18e0baa6443f5370d8e3 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 13:42:37 -0500 Subject: [PATCH 05/24] fix formatting for lint / flow --- .../jest-config/src/__tests__/normalize.test.js | 6 +++--- packages/jest-config/src/index.js | 5 ++++- packages/jest-config/src/normalize.js | 14 ++++++-------- packages/jest-runtime/src/script_transformer.js | 10 ++++------ 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 8026aa904b5b..2cbfd3be4689 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -306,9 +306,9 @@ describe('transform', () => { }); it('does not normalize the path if it is a function', () => { - const theFunction = () => {} + const theFunction = () => {}; - const { options } = normalize( + const {options} = normalize( { rootDir: '/root/', transform: { @@ -1122,7 +1122,7 @@ describe('preset', () => { expect(options.transform).toEqual([ ['a', '/node_modules/a'], ['b', '/node_modules/b'], - ['c', aFunction] + ['c', aFunction], ]); }); diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 0cd41fd2b379..e8f0dac8bbff 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -60,7 +60,10 @@ export function readConfig( 'Jest: Cannot use configuration as an object without a file path.', ); } - } else if (argv.config && (typeof argv.config === 'object' || isJSONString(argv.config))) { + } else if ( + argv.config && + (typeof argv.config === 'object' || isJSONString(argv.config)) + ) { // A JSON string was passed to `--config` argument and we can parse it // and use as is. let config; diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 4491127e6573..97b04b9b0619 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -536,14 +536,12 @@ export default function normalize(options: InitialOptions, argv: Argv) { Object.keys(transform).map(regex => [ regex, typeof transform[regex] === 'string' - ? - resolve(newOptions.resolver, { - filePath: transform[regex], - key, - rootDir: options.rootDir, - }) - : - transform[regex] + ? resolve(newOptions.resolver, { + filePath: transform[regex], + key, + rootDir: options.rootDir, + }) + : transform[regex], ]); break; case 'coveragePathIgnorePatterns': diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index f733ba924122..c8dfa2edf0d3 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -148,11 +148,9 @@ export default class ScriptTransformer { const transformFunctionOrPath = this._getTransformFunctionOrPath(filename); if (transformFunctionOrPath) { - let isTransformFunction = typeof transformFunctionOrPath === 'function'; - if (isTransformFunction) { - transform = transformFunctionOrPath(); - } - else { + if (typeof transformFunctionOrPath === 'function') { + transform = (transformFunctionOrPath(): Transformer); + } else { const transformer = this._transformCache.get(transformFunctionOrPath); if (transformer != null) { return transformer; @@ -169,7 +167,7 @@ export default class ScriptTransformer { 'Jest: a transform must export a `process` function.', ); } - if (!isTransformFunction) { + if (typeof transformFunctionOrPath !== 'function') { this._transformCache.set(transformFunctionOrPath, transform); } } From 40f39f7a9628737c4299aae6184436f100666f1a Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 14:07:44 -0500 Subject: [PATCH 06/24] add unit test for preprocessor transform as a function vs filepath --- .../src/__tests__/script_transformer.test.js | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/jest-runtime/src/__tests__/script_transformer.test.js b/packages/jest-runtime/src/__tests__/script_transformer.test.js index 60ae65c665ff..71c2671030c9 100644 --- a/packages/jest-runtime/src/__tests__/script_transformer.test.js +++ b/packages/jest-runtime/src/__tests__/script_transformer.test.js @@ -316,7 +316,7 @@ describe('ScriptTransformer', () => { ).not.toThrow(); }); - it('uses the supplied preprocessor', () => { + it('uses the supplied preprocessor if it is a file path', () => { config = Object.assign(config, { transform: [['^.+\\.js$', 'test_preprocessor']], }); @@ -332,6 +332,36 @@ describe('ScriptTransformer', () => { expect(vm.Script.mock.calls[1][0]).toMatchSnapshot(); }); + it('uses the supplied preprocessor if it is a function', () => { + const escapeStrings = str => str.replace(/'/, `'`); + + const process = jest.fn( + (content, filename, config) => ` + const TRANSFORMED = { + filename: '${escapeStrings(filename)}', + script: '${escapeStrings(content)}', + config: '${escapeStrings(JSON.stringify(config))}', + }; + `, + ); + + const preprocessor = jest.fn(() => ({process})); + config = Object.assign(config, { + transform: [['^.+\\.js$', preprocessor]], + }); + const scriptTransformer = new ScriptTransformer(config); + scriptTransformer.transform('/fruits/banana.js', {}); + + expect(vm.Script.mock.calls[0][0]).toMatchSnapshot(); + expect(process.mock.calls[0][0]).toMatchSnapshot(); + + scriptTransformer.transform('/node_modules/react.js', {}); + + expect(process.mock.calls.length).toBe(1); + // ignores preprocessor + expect(vm.Script.mock.calls[1][0]).toMatchSnapshot(); + }); + it('uses multiple preprocessors', () => { config = Object.assign(config, { transform: [ From 848585f6000a204fe9bc3ba25d45d1660ee05e3a Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 18:27:10 -0500 Subject: [PATCH 07/24] guard against customJSTransformer.includes is not a function error --- packages/jest-config/src/normalize.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 97b04b9b0619..142c7f0d78ce 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -144,7 +144,7 @@ const setupBabelJest = (options: InitialOptions) => { if (customJSTransformer === 'babel-jest') { babelJest = require.resolve('babel-jest'); transform[customJSPattern] = babelJest; - } else if (customJSTransformer.includes('babel-jest')) { + } else if (customJSTransformer && customJSTransformer.includes && customJSTransformer.includes('babel-jest')) { babelJest = customJSTransformer; } } From 3bb6d6eae95997244bac88595c8f963d9ffa574e Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 18:40:42 -0500 Subject: [PATCH 08/24] add unit test for runCLI function with standalone config argument --- .../__tests__/__fixtures__/runCLI/aFile.js | 3 ++ .../__fixtures__/runCLI/aFile_spec.js | 8 +++ .../jest-cli/src/__tests__/cli/runCLI.test.js | 51 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js create mode 100644 packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js create mode 100644 packages/jest-cli/src/__tests__/cli/runCLI.test.js diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js new file mode 100644 index 000000000000..fbd438792af1 --- /dev/null +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js @@ -0,0 +1,3 @@ +module.exports = { + toReplace: 1 +}; \ No newline at end of file diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js new file mode 100644 index 000000000000..e046527102fb --- /dev/null +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js @@ -0,0 +1,8 @@ +const expect = require('expect'); +const aFile = require('./aFile'); + +describe('aFile test', () => { + it('should have transformed aFile', () => { + expect(JSON.stringify(aFile)).toEqual(JSON.stringify({ 'replaced2': 1 })); + }) +}); \ No newline at end of file diff --git a/packages/jest-cli/src/__tests__/cli/runCLI.test.js b/packages/jest-cli/src/__tests__/cli/runCLI.test.js new file mode 100644 index 000000000000..817d29f1df22 --- /dev/null +++ b/packages/jest-cli/src/__tests__/cli/runCLI.test.js @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +import path from 'path'; +import { runCLI } from '../../cli'; + +const project = path.join(__dirname, '../__fixtures__/runCLI'); +const projects = [project]; + +const argv = { + config: { + testMatch: [ + "/*_spec.js" + ], + transform: { + "^.+\\.jsx?$": () => { + return { + process(src, filename) { + return src.replace('toReplace', 'replaced'); + }, + }; + } + } + }, +}; + +describe('runCLI', () => { + describe('config as object', () => { + it('runs jest with a standalone config object', async () => { + let runResult = null; + let error = null; + try { + runResult = await runCLI(argv, projects); + } + catch (ex) { + error = ex; + } + let numPassedTests = runResult ? runResult.results.numPassedTests : -1; + expect(error).toBe(null); + expect(numPassedTests).toBe(1); + }) + }) +}); + From bbf105e593ba2b2a28258f3a442f6f92faad7ca2 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 18:42:43 -0500 Subject: [PATCH 09/24] remove typo added to make a passing test fail --- .../jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js index e046527102fb..1da6e8f9a5c3 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js @@ -3,6 +3,6 @@ const aFile = require('./aFile'); describe('aFile test', () => { it('should have transformed aFile', () => { - expect(JSON.stringify(aFile)).toEqual(JSON.stringify({ 'replaced2': 1 })); + expect(JSON.stringify(aFile)).toEqual(JSON.stringify({ 'replaced': 1 })); }) }); \ No newline at end of file From 6f9d9cfaebf2fc257a36ff7dfd87037e39113351 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Wed, 21 Nov 2018 19:12:17 -0500 Subject: [PATCH 10/24] cosmetic - prettier formatting --- .../__tests__/__fixtures__/runCLI/aFile.js | 4 +-- .../__fixtures__/runCLI/aFile_spec.js | 6 ++-- .../jest-cli/src/__tests__/cli/runCLI.test.js | 30 ++++++++----------- packages/jest-config/src/normalize.js | 6 +++- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js index fbd438792af1..a1d3e3566f57 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js @@ -1,3 +1,3 @@ module.exports = { - toReplace: 1 -}; \ No newline at end of file + toReplace: 1, +}; diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js index 1da6e8f9a5c3..b29dcad55cfe 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js @@ -3,6 +3,6 @@ const aFile = require('./aFile'); describe('aFile test', () => { it('should have transformed aFile', () => { - expect(JSON.stringify(aFile)).toEqual(JSON.stringify({ 'replaced': 1 })); - }) -}); \ No newline at end of file + expect(JSON.stringify(aFile)).toEqual(JSON.stringify({replaced: 1})); + }); +}); diff --git a/packages/jest-cli/src/__tests__/cli/runCLI.test.js b/packages/jest-cli/src/__tests__/cli/runCLI.test.js index 817d29f1df22..b91461447658 100644 --- a/packages/jest-cli/src/__tests__/cli/runCLI.test.js +++ b/packages/jest-cli/src/__tests__/cli/runCLI.test.js @@ -9,25 +9,21 @@ 'use strict'; import path from 'path'; -import { runCLI } from '../../cli'; +import {runCLI} from '../../cli'; const project = path.join(__dirname, '../__fixtures__/runCLI'); const projects = [project]; const argv = { config: { - testMatch: [ - "/*_spec.js" - ], + testMatch: ['/*_spec.js'], transform: { - "^.+\\.jsx?$": () => { - return { - process(src, filename) { - return src.replace('toReplace', 'replaced'); - }, - }; - } - } + '^.+\\.jsx?$': () => ({ + process(src, filename) { + return src.replace('toReplace', 'replaced'); + }, + }), + }, }, }; @@ -38,14 +34,12 @@ describe('runCLI', () => { let error = null; try { runResult = await runCLI(argv, projects); - } - catch (ex) { + } catch (ex) { error = ex; } - let numPassedTests = runResult ? runResult.results.numPassedTests : -1; + const numPassedTests = runResult ? runResult.results.numPassedTests : -1; expect(error).toBe(null); expect(numPassedTests).toBe(1); - }) - }) + }); + }); }); - diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 142c7f0d78ce..8efbf6f1d4cc 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -144,7 +144,11 @@ const setupBabelJest = (options: InitialOptions) => { if (customJSTransformer === 'babel-jest') { babelJest = require.resolve('babel-jest'); transform[customJSPattern] = babelJest; - } else if (customJSTransformer && customJSTransformer.includes && customJSTransformer.includes('babel-jest')) { + } else if ( + customJSTransformer && + customJSTransformer.includes && + customJSTransformer.includes('babel-jest') + ) { babelJest = customJSTransformer; } } From 829d5da91c60ca6af09a37ff2b30218ab3c9242c Mon Sep 17 00:00:00 2001 From: jharris4 Date: Thu, 22 Nov 2018 11:57:16 -0500 Subject: [PATCH 11/24] add required facebook headers for text fixture files --- .../src/__tests__/__fixtures__/runCLI/aFile.js | 10 ++++++++++ .../src/__tests__/__fixtures__/runCLI/aFile_spec.js | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js index a1d3e3566f57..fcdaa7a88d0a 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js @@ -1,3 +1,13 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + module.exports = { toReplace: 1, }; diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js index b29dcad55cfe..16b66861fbe9 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js @@ -1,3 +1,13 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + const expect = require('expect'); const aFile = require('./aFile'); From 565c83b992ebb61e151f71b50aad00b2bc67c66b Mon Sep 17 00:00:00 2001 From: jharris4 Date: Thu, 22 Nov 2018 12:05:36 -0500 Subject: [PATCH 12/24] fix test assertion by sorting keys before comparing equality --- packages/jest-config/src/__tests__/normalize.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 2cbfd3be4689..95334daf3005 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1119,7 +1119,8 @@ describe('preset', () => { '/node_modules/a', '/node_modules/b', ]); - expect(options.transform).toEqual([ + const sorter = (a, b) => a[0].localeCompare(b[0]); + expect(options.transform.sort(sorter)).toEqual([ ['a', '/node_modules/a'], ['b', '/node_modules/b'], ['c', aFunction], From e30e7b7f8cd5eebd10a5cdad60a162534ba091ee Mon Sep 17 00:00:00 2001 From: jharris4 Date: Thu, 22 Nov 2018 14:04:11 -0500 Subject: [PATCH 13/24] update snapshot for script_transformer_test --- .../script_transformer.test.js.snap | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap b/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap index 68522bebc832..5390b9cbc237 100644 --- a/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap +++ b/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap @@ -195,3 +195,37 @@ exports[`ScriptTransformer uses the supplied preprocessor 2`] = ` "({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){module.exports = \\"react\\"; }});" `; + +exports[`ScriptTransformer uses the supplied preprocessor if it is a file path 1`] = ` +"({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){ + const TRANSFORMED = { + filename: '/fruits/banana.js', + script: 'module.exports = \\"banana\\";', + config: '{\\"cache\\":true,\\"cacheDirectory\\":\\"/cache/\\",\\"name\\":\\"test\\",\\"rootDir\\":\\"/\\",\\"transformIgnorePatterns\\":[\\"/node_modules/\\"],\\"transform\\":[[\\"^.+\\\\\\\\.js$\\",\\"test_preprocessor\\"]]}', + }; + +}});" +`; + +exports[`ScriptTransformer uses the supplied preprocessor if it is a file path 2`] = ` +"({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){module.exports = \\"react\\"; +}});" +`; + +exports[`ScriptTransformer uses the supplied preprocessor if it is a function 1`] = ` +"({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){ + const TRANSFORMED = { + filename: '/fruits/banana.js', + script: 'module.exports = \\"banana\\";', + config: '{\\"cache\\":true,\\"cacheDirectory\\":\\"/cache/\\",\\"name\\":\\"test\\",\\"rootDir\\":\\"/\\",\\"transformIgnorePatterns\\":[\\"/node_modules/\\"],\\"transform\\":[[\\"^.+\\\\\\\\.js$\\",null]]}', + }; + +}});" +`; + +exports[`ScriptTransformer uses the supplied preprocessor if it is a function 2`] = `"module.exports = \\"banana\\";"`; + +exports[`ScriptTransformer uses the supplied preprocessor if it is a function 3`] = ` +"({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){module.exports = \\"react\\"; +}});" +`; From 46671632134f09d7723428644d27414be3bc1cd9 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Thu, 22 Nov 2018 14:15:32 -0500 Subject: [PATCH 14/24] remove outdated snapshot for script_transformer_test --- .../script_transformer.test.js.snap | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap b/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap index 5390b9cbc237..ddde78369fd1 100644 --- a/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap +++ b/packages/jest-runtime/src/__tests__/__snapshots__/script_transformer.test.js.snap @@ -180,22 +180,6 @@ exports[`ScriptTransformer uses multiple preprocessors 3`] = ` }});" `; -exports[`ScriptTransformer uses the supplied preprocessor 1`] = ` -"({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){ - const TRANSFORMED = { - filename: '/fruits/banana.js', - script: 'module.exports = \\"banana\\";', - config: '{\\"cache\\":true,\\"cacheDirectory\\":\\"/cache/\\",\\"name\\":\\"test\\",\\"rootDir\\":\\"/\\",\\"transformIgnorePatterns\\":[\\"/node_modules/\\"],\\"transform\\":[[\\"^.+\\\\\\\\.js$\\",\\"test_preprocessor\\"]]}', - }; - -}});" -`; - -exports[`ScriptTransformer uses the supplied preprocessor 2`] = ` -"({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){module.exports = \\"react\\"; -}});" -`; - exports[`ScriptTransformer uses the supplied preprocessor if it is a file path 1`] = ` "({\\"Object.\\":function(module,exports,require,__dirname,__filename,global,jest){ const TRANSFORMED = { From 4ae1e9532929d19d0394d35bd6be7e3ee8eb9469 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Fri, 23 Nov 2018 19:03:59 -0500 Subject: [PATCH 15/24] cli/run function - add param for optional exit and return results This makes it easier to test the function --- packages/jest-cli/src/cli/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 889a35f77808..bc1a69e3ff5d 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -33,7 +33,7 @@ import {sync as realpath} from 'realpath-native'; import init from '../lib/init'; import logDebugMessages from '../lib/log_debug_messages'; -export async function run(maybeArgv?: Argv, project?: Path) { +export async function run(maybeArgv?: Argv, project?: Path, logAndExitOnError?: Boolean = true) { try { const argv: Argv = buildArgv(maybeArgv, project); @@ -46,11 +46,14 @@ export async function run(maybeArgv?: Argv, project?: Path) { const {results, globalConfig} = await runCLI(argv, projects); readResultsAndExit(results, globalConfig); + return results; } catch (error) { - clearLine(process.stderr); - clearLine(process.stdout); - console.error(chalk.red(error.stack)); - exit(1); + if (logAndExitOnError) { + clearLine(process.stderr); + clearLine(process.stdout); + console.error(chalk.red(error.stack)); + exit(1); + } throw error; } } From 42e1cd27b137a7fee48d5981131e2a7ae45821e7 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Fri, 23 Nov 2018 19:05:04 -0500 Subject: [PATCH 16/24] add tests calling jest-cli run() with string vs object parameters --- .../src/__tests__/__fixtures__/run/aFile.js | 13 ++++ .../__tests__/__fixtures__/run/aFile_spec.js | 18 +++++ .../__fixtures__/run/transform-module.js | 3 + .../jest-cli/src/__tests__/cli/runCLI.test.js | 77 ++++++++++++++++--- 4 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 packages/jest-cli/src/__tests__/__fixtures__/run/aFile.js create mode 100644 packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js create mode 100644 packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js diff --git a/packages/jest-cli/src/__tests__/__fixtures__/run/aFile.js b/packages/jest-cli/src/__tests__/__fixtures__/run/aFile.js new file mode 100644 index 000000000000..fcdaa7a88d0a --- /dev/null +++ b/packages/jest-cli/src/__tests__/__fixtures__/run/aFile.js @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +module.exports = { + toReplace: 1, +}; diff --git a/packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js new file mode 100644 index 000000000000..35c435b70d85 --- /dev/null +++ b/packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +const expect = require('expect'); +const aFile = require('./aFile'); + +describe('aFile test', () => { + it('should have transformed aFile', () => { + expect(JSON.stringify(aFile)).toEqual(JSON.stringify({transformModuleReplaced: 1})); + }); +}); diff --git a/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js b/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js new file mode 100644 index 000000000000..5e4b1bda45ed --- /dev/null +++ b/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js @@ -0,0 +1,3 @@ +module.exports = { + process: (src, filename) => src.replace('toReplace', 'transformModuleReplaced') +}; \ No newline at end of file diff --git a/packages/jest-cli/src/__tests__/cli/runCLI.test.js b/packages/jest-cli/src/__tests__/cli/runCLI.test.js index b91461447658..7cc37565d5ca 100644 --- a/packages/jest-cli/src/__tests__/cli/runCLI.test.js +++ b/packages/jest-cli/src/__tests__/cli/runCLI.test.js @@ -9,31 +9,54 @@ 'use strict'; import path from 'path'; -import {runCLI} from '../../cli'; +import {run, runCLI} from '../../cli'; -const project = path.join(__dirname, '../__fixtures__/runCLI'); -const projects = [project]; +const runProject = path.join(__dirname, '../__fixtures__/run'); +const runCLIProject = path.join(__dirname, '../__fixtures__/runCLI'); +const runCLIProjects = [runCLIProject]; -const argv = { +const processor = { + process: (src, filename) => src.replace('toReplace', 'replaced') +}; + +const runCLIArgv = { config: { testMatch: ['/*_spec.js'], transform: { - '^.+\\.jsx?$': () => ({ - process(src, filename) { - return src.replace('toReplace', 'replaced'); - }, - }), + '^.+\\.jsx?$': () => processor }, }, }; +const runArgvString = [ + '--config', + JSON.stringify({ + "rootDir": runProject, + "testMatch": ["/*_spec.js"], + "transform": { + "^.+\\.jsx?$": "./transform-module" + } + }) +]; + +const runArgvObject = [ + '--config', + { + "rootDir": runProject, + "testMatch": ["/*_spec.js"], + "transform": { + "^.+\\.jsx?$": "./transform-module" + } + } +]; + describe('runCLI', () => { describe('config as object', () => { - it('runs jest with a standalone config object', async () => { + it('passes the test when the config has a transform function', async () => { let runResult = null; let error = null; try { - runResult = await runCLI(argv, projects); + runResult = await runCLI(runCLIArgv, runCLIProjects); } catch (ex) { error = ex; } @@ -43,3 +66,35 @@ describe('runCLI', () => { }); }); }); + +describe('run', () => { + describe('config as string', () => { + it('passes the test when the config has a transform module path', async () => { + let runResult = null; + let error = null; + try { + runResult = await run(runArgvString, runProject); + } catch (ex) { + error = ex; + } + const numPassedTests = runResult ? runResult.numPassedTests : -1; + expect(error).toBe(null); + expect(numPassedTests).toBe(1); + }); + }); + + describe('config as object', () => { + it('throws running the test when the config is an object', async () => { + let runResult = null; + let error = null; + try { + runResult = await run(runArgvObject, runProject, false); + } catch (ex) { + error = ex; + } + const numPassedTests = runResult ? runResult.numPassedTests : -1; + expect(error).not.toBe(null); + expect(numPassedTests).toBe(-1); + }); + }); +}); From bd032a6304de7567fc74c44af82fcae005a4dfba Mon Sep 17 00:00:00 2001 From: jharris4 Date: Sat, 24 Nov 2018 13:28:41 -0500 Subject: [PATCH 17/24] remove run exit parameter and add better error message when yargs throws --- packages/jest-cli/src/cli/index.js | 43 +++++++++++++++++------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index bc1a69e3ff5d..5a4cd3f9a778 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -33,7 +33,7 @@ import {sync as realpath} from 'realpath-native'; import init from '../lib/init'; import logDebugMessages from '../lib/log_debug_messages'; -export async function run(maybeArgv?: Argv, project?: Path, logAndExitOnError?: Boolean = true) { +export async function run(maybeArgv?: Argv, project?: Path) { try { const argv: Argv = buildArgv(maybeArgv, project); @@ -48,12 +48,10 @@ export async function run(maybeArgv?: Argv, project?: Path, logAndExitOnError?: readResultsAndExit(results, globalConfig); return results; } catch (error) { - if (logAndExitOnError) { - clearLine(process.stderr); - clearLine(process.stdout); - console.error(chalk.red(error.stack)); - exit(1); - } + clearLine(process.stderr); + clearLine(process.stdout); + console.error(chalk.red(error.stack)); + exit(1); throw error; } } @@ -173,19 +171,26 @@ const readResultsAndExit = ( }; const buildArgv = (maybeArgv: ?Argv, project: ?Path) => { - const argv: Argv = yargs(maybeArgv || process.argv.slice(2)) - .usage(args.usage) - .alias('help', 'h') - .options(args.options) - .epilogue(args.docs) - .check(args.check).argv; - - validateCLIOptions( - argv, - Object.assign({}, args.options, {deprecationEntries}), - ); + try { + const argv: Argv = yargs(maybeArgv || process.argv.slice(2)) + .usage(args.usage) + .alias('help', 'h') + .options(args.options) + .epilogue(args.docs) + .check(args.check).argv; + + validateCLIOptions( + argv, + Object.assign({}, args.options, {deprecationEntries}), + ); - return argv; + return argv; + } catch (err) { + if (maybeArgv) { + throw new Error('Command line arguments should be an array of strings'); + } + throw err; + } }; const getProjectListFromCLIArgs = (argv, project: ?Path) => { From 3aa417a049b8aaba3859cdcb2ac59f74508ea9bc Mon Sep 17 00:00:00 2001 From: jharris4 Date: Sat, 24 Nov 2018 13:31:08 -0500 Subject: [PATCH 18/24] split run & runCLI tests and cleanup console output for nested tests --- .../__tests__/__fixtures__/run/aFile_spec.js | 2 +- .../__fixtures__/run/transform-module.js | 2 +- .../__fixtures__/runCLI/aFile_spec.js | 2 +- .../__fixtures__/runCLI/transform-module.js | 3 + .../jest-cli/src/__tests__/cli/run.test.js | 109 ++++++++++++++++++ .../jest-cli/src/__tests__/cli/runCLI.test.js | 79 ++++++------- 6 files changed, 150 insertions(+), 47 deletions(-) create mode 100644 packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js create mode 100644 packages/jest-cli/src/__tests__/cli/run.test.js diff --git a/packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js index 35c435b70d85..a3fc4f8165a8 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js @@ -13,6 +13,6 @@ const aFile = require('./aFile'); describe('aFile test', () => { it('should have transformed aFile', () => { - expect(JSON.stringify(aFile)).toEqual(JSON.stringify({transformModuleReplaced: 1})); + expect(JSON.stringify(aFile)).toEqual(JSON.stringify({runReplaced: 1})); }); }); diff --git a/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js b/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js index 5e4b1bda45ed..c7066fd4a08e 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js @@ -1,3 +1,3 @@ module.exports = { - process: (src, filename) => src.replace('toReplace', 'transformModuleReplaced') + process: (src, filename) => src.replace('toReplace', 'runReplaced') }; \ No newline at end of file diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js index 16b66861fbe9..88340c199a7d 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js @@ -13,6 +13,6 @@ const aFile = require('./aFile'); describe('aFile test', () => { it('should have transformed aFile', () => { - expect(JSON.stringify(aFile)).toEqual(JSON.stringify({replaced: 1})); + expect(JSON.stringify(aFile)).toEqual(JSON.stringify({ runCLIReplaced: 1})); }); }); diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js new file mode 100644 index 000000000000..d25611c47c2d --- /dev/null +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js @@ -0,0 +1,3 @@ +module.exports = { + process: (src, filename) => src.replace('toReplace', 'runCLIReplaced') +}; \ No newline at end of file diff --git a/packages/jest-cli/src/__tests__/cli/run.test.js b/packages/jest-cli/src/__tests__/cli/run.test.js new file mode 100644 index 000000000000..fd6cced59fd6 --- /dev/null +++ b/packages/jest-cli/src/__tests__/cli/run.test.js @@ -0,0 +1,109 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +import path from 'path'; +import {run} from '../../cli'; + +const runProject = path.join(__dirname, '../__fixtures__/run'); + +jest.mock('exit'); + +const runArgvString = [ + '--config', + JSON.stringify({ + "rootDir": runProject, + "testMatch": ["/*_spec.js"], + "transform": { + "^.+\\.jsx?$": "./transform-module" + } + }) +]; + +const runArgvObject = [ + '--config', + { + "rootDir": runProject, + "testMatch": ["/*_spec.js"], + "transform": { + "^.+\\.jsx?$": "./transform-module" + } + } +]; + +const mockArgv = ['arg1', 'arg2']; + +const processOnFn = process.on; +const processExitFn = process.exit; +const processErrWriteFn = process.stderr.write; +const consoleErrorFn = console.error; + +const noSubTestLogs = true; + +describe('run', () => { + let processOnSpy; + let processExitSpy; + let stderrSpy; + let consoleErrorSpy; + beforeEach(() => { + if (noSubTestLogs) { + process.on = jest.fn(); + process.on.mockReset(); + processOnSpy = jest.spyOn(process, 'on'); + process.exit = jest.fn(); + process.exit.mockReset(); + processExitSpy = jest.spyOn(process, 'exit'); + process.stderr.write = jest.fn(); + process.stderr.write.mockReset(); + stderrSpy = jest.spyOn(process.stderr, 'write'); + console.error = jest.fn(); + console.error.mockReset(); + consoleErrorSpy = jest.spyOn(console, 'error'); + } + }); + + afterEach(() => { + if (noSubTestLogs) { + process.on = processOnFn; + process.exit = processExitFn; + process.stderr.write = processErrWriteFn; + console.error = consoleErrorFn; + } + }); + + describe('config as string', () => { + it('passes the test when the config has a transform module path', async () => { + let runResult = null; + let error = null; + try { + runResult = await run(runArgvString, runProject); + } catch (ex) { + error = ex; + } + const numPassedTests = runResult ? runResult.numPassedTests : -1; + expect(error).toBe(null); + expect(numPassedTests).toBe(1); + }); + }); + + describe('config as object', () => { + it('throws running the test when the config is an object', async () => { + let runResult = null; + let error = null; + try { + runResult = await run(runArgvObject, runProject); + } catch (ex) { + error = ex; + } + const numPassedTests = runResult ? runResult.numPassedTests : -1; + expect(error).not.toBe(null); + expect(numPassedTests).toBe(-1); + }); + }); +}); diff --git a/packages/jest-cli/src/__tests__/cli/runCLI.test.js b/packages/jest-cli/src/__tests__/cli/runCLI.test.js index 7cc37565d5ca..9af6d1211b97 100644 --- a/packages/jest-cli/src/__tests__/cli/runCLI.test.js +++ b/packages/jest-cli/src/__tests__/cli/runCLI.test.js @@ -9,54 +9,61 @@ 'use strict'; import path from 'path'; -import {run, runCLI} from '../../cli'; +import {runCLI} from '../../cli'; +import transformModule from '../__fixtures__/runCLI/transform-module'; -const runProject = path.join(__dirname, '../__fixtures__/run'); -const runCLIProject = path.join(__dirname, '../__fixtures__/runCLI'); -const runCLIProjects = [runCLIProject]; +const project = path.join(__dirname, '../__fixtures__/runCLI'); +const projects = [project]; const processor = { - process: (src, filename) => src.replace('toReplace', 'replaced') + process: (src, filename) => transformModule.process(src, filename) }; -const runCLIArgv = { +const argvObject = { config: { testMatch: ['/*_spec.js'], transform: { - '^.+\\.jsx?$': () => processor + '^.+\\.jsx?$': () => transformModule }, }, }; -const runArgvString = [ - '--config', - JSON.stringify({ - "rootDir": runProject, +const argvString = { + config: JSON.stringify({ + "rootDir": project, "testMatch": ["/*_spec.js"], "transform": { "^.+\\.jsx?$": "./transform-module" } - }) -]; + }), +} -const runArgvObject = [ - '--config', - { - "rootDir": runProject, - "testMatch": ["/*_spec.js"], - "transform": { - "^.+\\.jsx?$": "./transform-module" - } - } -]; +const processErrWriteFn = process.stderr.write; + +const noLogs = true; describe('runCLI', () => { + let stderrSpy; + beforeEach(() => { + if (noLogs) { + process.stderr.write = jest.fn(); + process.stderr.write.mockReset(); + stderrSpy = jest.spyOn(process.stderr, 'write'); + } + }); + + afterEach(() => { + if (noLogs) { + process.stderr.write = processErrWriteFn; + } + }); + describe('config as object', () => { it('passes the test when the config has a transform function', async () => { let runResult = null; let error = null; try { - runResult = await runCLI(runCLIArgv, runCLIProjects); + runResult = await runCLI(argvObject, projects); } catch (ex) { error = ex; } @@ -65,36 +72,20 @@ describe('runCLI', () => { expect(numPassedTests).toBe(1); }); }); -}); -describe('run', () => { describe('config as string', () => { - it('passes the test when the config has a transform module path', async () => { + it('passes the test when the config is a string', async () => { let runResult = null; let error = null; try { - runResult = await run(runArgvString, runProject); + runResult = await runCLI(argvString, projects); } catch (ex) { error = ex; } - const numPassedTests = runResult ? runResult.numPassedTests : -1; + const numPassedTests = runResult ? runResult.results.numPassedTests : -1; expect(error).toBe(null); expect(numPassedTests).toBe(1); }); }); - - describe('config as object', () => { - it('throws running the test when the config is an object', async () => { - let runResult = null; - let error = null; - try { - runResult = await run(runArgvObject, runProject, false); - } catch (ex) { - error = ex; - } - const numPassedTests = runResult ? runResult.numPassedTests : -1; - expect(error).not.toBe(null); - expect(numPassedTests).toBe(-1); - }); - }); }); + From 458bb67953c01b953588a2e0cdb68aa4fcde213a Mon Sep 17 00:00:00 2001 From: jharris4 Date: Sat, 24 Nov 2018 13:39:54 -0500 Subject: [PATCH 19/24] misc lint fixes --- .../__fixtures__/run/transform-module.js | 14 ++++++-- .../__fixtures__/runCLI/aFile_spec.js | 2 +- .../__fixtures__/runCLI/transform-module.js | 14 ++++++-- .../jest-cli/src/__tests__/cli/run.test.js | 34 +++++++------------ .../jest-cli/src/__tests__/cli/runCLI.test.js | 21 ++++-------- 5 files changed, 44 insertions(+), 41 deletions(-) diff --git a/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js b/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js index c7066fd4a08e..4b6d60e96ba9 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/run/transform-module.js @@ -1,3 +1,13 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + module.exports = { - process: (src, filename) => src.replace('toReplace', 'runReplaced') -}; \ No newline at end of file + process: (src, filename) => src.replace('toReplace', 'runReplaced'), +}; diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js index 88340c199a7d..b992de8e8899 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js @@ -13,6 +13,6 @@ const aFile = require('./aFile'); describe('aFile test', () => { it('should have transformed aFile', () => { - expect(JSON.stringify(aFile)).toEqual(JSON.stringify({ runCLIReplaced: 1})); + expect(JSON.stringify(aFile)).toEqual(JSON.stringify({runCLIReplaced: 1})); }); }); diff --git a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js index d25611c47c2d..66370bd9efb1 100644 --- a/packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js +++ b/packages/jest-cli/src/__tests__/__fixtures__/runCLI/transform-module.js @@ -1,3 +1,13 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + module.exports = { - process: (src, filename) => src.replace('toReplace', 'runCLIReplaced') -}; \ No newline at end of file + process: (src, filename) => src.replace('toReplace', 'runCLIReplaced'), +}; diff --git a/packages/jest-cli/src/__tests__/cli/run.test.js b/packages/jest-cli/src/__tests__/cli/run.test.js index fd6cced59fd6..b7563ec68276 100644 --- a/packages/jest-cli/src/__tests__/cli/run.test.js +++ b/packages/jest-cli/src/__tests__/cli/run.test.js @@ -18,27 +18,25 @@ jest.mock('exit'); const runArgvString = [ '--config', JSON.stringify({ - "rootDir": runProject, - "testMatch": ["/*_spec.js"], - "transform": { - "^.+\\.jsx?$": "./transform-module" - } - }) + rootDir: runProject, + testMatch: ['/*_spec.js'], + transform: { + '^.+\\.jsx?$': './transform-module', + }, + }), ]; const runArgvObject = [ '--config', { - "rootDir": runProject, - "testMatch": ["/*_spec.js"], - "transform": { - "^.+\\.jsx?$": "./transform-module" - } - } + rootDir: runProject, + testMatch: ['/*_spec.js'], + transform: { + '^.+\\.jsx?$': './transform-module', + }, + }, ]; -const mockArgv = ['arg1', 'arg2']; - const processOnFn = process.on; const processExitFn = process.exit; const processErrWriteFn = process.stderr.write; @@ -47,24 +45,16 @@ const consoleErrorFn = console.error; const noSubTestLogs = true; describe('run', () => { - let processOnSpy; - let processExitSpy; - let stderrSpy; - let consoleErrorSpy; beforeEach(() => { if (noSubTestLogs) { process.on = jest.fn(); process.on.mockReset(); - processOnSpy = jest.spyOn(process, 'on'); process.exit = jest.fn(); process.exit.mockReset(); - processExitSpy = jest.spyOn(process, 'exit'); process.stderr.write = jest.fn(); process.stderr.write.mockReset(); - stderrSpy = jest.spyOn(process.stderr, 'write'); console.error = jest.fn(); console.error.mockReset(); - consoleErrorSpy = jest.spyOn(console, 'error'); } }); diff --git a/packages/jest-cli/src/__tests__/cli/runCLI.test.js b/packages/jest-cli/src/__tests__/cli/runCLI.test.js index 9af6d1211b97..f9508c5c158b 100644 --- a/packages/jest-cli/src/__tests__/cli/runCLI.test.js +++ b/packages/jest-cli/src/__tests__/cli/runCLI.test.js @@ -15,40 +15,34 @@ import transformModule from '../__fixtures__/runCLI/transform-module'; const project = path.join(__dirname, '../__fixtures__/runCLI'); const projects = [project]; -const processor = { - process: (src, filename) => transformModule.process(src, filename) -}; - const argvObject = { config: { testMatch: ['/*_spec.js'], transform: { - '^.+\\.jsx?$': () => transformModule + '^.+\\.jsx?$': () => transformModule, }, }, }; const argvString = { config: JSON.stringify({ - "rootDir": project, - "testMatch": ["/*_spec.js"], - "transform": { - "^.+\\.jsx?$": "./transform-module" - } + rootDir: project, + testMatch: ['/*_spec.js'], + transform: { + '^.+\\.jsx?$': './transform-module', + }, }), -} +}; const processErrWriteFn = process.stderr.write; const noLogs = true; describe('runCLI', () => { - let stderrSpy; beforeEach(() => { if (noLogs) { process.stderr.write = jest.fn(); process.stderr.write.mockReset(); - stderrSpy = jest.spyOn(process.stderr, 'write'); } }); @@ -88,4 +82,3 @@ describe('runCLI', () => { }); }); }); - From f7fccaaa88e056fc069ec05d4e34292b652e69a5 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Sat, 24 Nov 2018 13:41:45 -0500 Subject: [PATCH 20/24] rename variable --- packages/jest-cli/src/__tests__/cli/runCLI.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-cli/src/__tests__/cli/runCLI.test.js b/packages/jest-cli/src/__tests__/cli/runCLI.test.js index f9508c5c158b..d7d7371ed425 100644 --- a/packages/jest-cli/src/__tests__/cli/runCLI.test.js +++ b/packages/jest-cli/src/__tests__/cli/runCLI.test.js @@ -36,18 +36,18 @@ const argvString = { const processErrWriteFn = process.stderr.write; -const noLogs = true; +const noSubTestLogs = true; describe('runCLI', () => { beforeEach(() => { - if (noLogs) { + if (noSubTestLogs) { process.stderr.write = jest.fn(); process.stderr.write.mockReset(); } }); afterEach(() => { - if (noLogs) { + if (noSubTestLogs) { process.stderr.write = processErrWriteFn; } }); From 4aa187eb0c08ba69a06be93d7abcddce3d184504 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Sat, 24 Nov 2018 14:58:46 -0500 Subject: [PATCH 21/24] lint fixes for run function returning results --- packages/jest-cli/src/cli/index.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 5a4cd3f9a778..46c51b8197e3 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -33,20 +33,23 @@ import {sync as realpath} from 'realpath-native'; import init from '../lib/init'; import logDebugMessages from '../lib/log_debug_messages'; -export async function run(maybeArgv?: Argv, project?: Path) { +export const run = async ( + maybeArgv?: Argv, + project?: Path, +): Promise => { + let results, globalConfig; try { const argv: Argv = buildArgv(maybeArgv, project); if (argv.init) { await init(); - return; + return Promise.resolve(results); } const projects = getProjectListFromCLIArgs(argv, project); - const {results, globalConfig} = await runCLI(argv, projects); + ({results, globalConfig} = await runCLI(argv, projects)); readResultsAndExit(results, globalConfig); - return results; } catch (error) { clearLine(process.stderr); clearLine(process.stdout); @@ -54,7 +57,8 @@ export async function run(maybeArgv?: Argv, project?: Path) { exit(1); throw error; } -} + return Promise.resolve(results); +}; export const runCLI = async ( argv: Argv, From df6adfd79d4cf5082b61bcba0f740afe7e4ad370 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Sat, 24 Nov 2018 15:11:05 -0500 Subject: [PATCH 22/24] fix flow error on run function --- packages/jest-cli/src/cli/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 46c51b8197e3..7dddf8987121 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -36,7 +36,7 @@ import logDebugMessages from '../lib/log_debug_messages'; export const run = async ( maybeArgv?: Argv, project?: Path, -): Promise => { +): Promise => { let results, globalConfig; try { const argv: Argv = buildArgv(maybeArgv, project); From e48fd77c55c9cff64eb349e6a367ab3d897a8c15 Mon Sep 17 00:00:00 2001 From: jharris4 Date: Sat, 24 Nov 2018 15:57:05 -0500 Subject: [PATCH 23/24] always mock process.exit for cli run function tests --- packages/jest-cli/src/__tests__/cli/run.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/jest-cli/src/__tests__/cli/run.test.js b/packages/jest-cli/src/__tests__/cli/run.test.js index b7563ec68276..79f7b8f09221 100644 --- a/packages/jest-cli/src/__tests__/cli/run.test.js +++ b/packages/jest-cli/src/__tests__/cli/run.test.js @@ -46,11 +46,11 @@ const noSubTestLogs = true; describe('run', () => { beforeEach(() => { + process.on = jest.fn(); + process.on.mockReset(); + process.exit = jest.fn(); + process.exit.mockReset(); if (noSubTestLogs) { - process.on = jest.fn(); - process.on.mockReset(); - process.exit = jest.fn(); - process.exit.mockReset(); process.stderr.write = jest.fn(); process.stderr.write.mockReset(); console.error = jest.fn(); @@ -59,9 +59,9 @@ describe('run', () => { }); afterEach(() => { + process.on = processOnFn; + process.exit = processExitFn; if (noSubTestLogs) { - process.on = processOnFn; - process.exit = processExitFn; process.stderr.write = processErrWriteFn; console.error = consoleErrorFn; } From 3791c47257d76732d37686f40dc31684bae687c4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 25 Dec 2018 18:37:42 -0500 Subject: [PATCH 24/24] Update packages/jest-cli/src/cli/index.js Co-Authored-By: jharris4 --- packages/jest-cli/src/cli/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 7dddf8987121..fb6a16c91278 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -43,7 +43,7 @@ export const run = async ( if (argv.init) { await init(); - return Promise.resolve(results); + return results; } const projects = getProjectListFromCLIArgs(argv, project);