From 5756129c9e1e28b1afdeccc13b4146c20141961f Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Thu, 22 Mar 2018 23:13:15 -0400 Subject: [PATCH 01/18] sets project name if empty fixes #5597 --- .../src/__tests__/normalize.test.js | 58 +++++++++++++++++++ packages/jest-config/src/normalize.js | 14 +++++ types/Config.js | 4 +- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 94003c410899..704c3a0d6070 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -64,6 +64,64 @@ it('picks a name based on the rootDir', () => { ).toBe(expected); }); +it('picks a name for projects based on the main rootDir', () => { + const rootDir = '/root/path/foo'; + const firstExpected = crypto + .createHash('md5') + .update('/root/path/foo0') + .digest('hex'); + const secondExpected = crypto + .createHash('md5') + .update('/root/path/foo1') + .digest('hex'); + + const options = normalize( + { + projects: [{}, {}], + rootDir, + }, + {}, + ); + + expect(options.projects[0].name).toBe(firstExpected); + expect(options.projects[1].name).toBe(secondExpected); +}); + +it('picks a name for projects based on the projects rootDir', () => { + const firstRootDir = '/root/path/foo'; + const secondRootDir = '/root/path/bar'; + const firstExpected = crypto + .createHash('md5') + .update('/root/path/foo') + .digest('hex'); + const secondExpected = crypto + .createHash('md5') + .update('/root/path/bar') + .digest('hex'); + + const options = normalize( + { + projects: [{rootDir: firstRootDir}, {rootDir: secondRootDir}], + }, + {}, + ); + + expect(options.projects[0].name).toBe(firstExpected); + expect(options.projects[1].name).toBe(secondExpected); +}); + +it('keeps custom project name based on the projects rootDir', () => { + const name = 'test'; + const options = normalize( + { + projects: [{name, rootDir: '/path/to/foo'}], + }, + {}, + ); + + expect(options.projects[0].name).toBe(name); +}); + it('keeps custom names based on the rootDir', () => { expect( normalize( diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index b53185643f3f..a3540c95b607 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -39,6 +39,7 @@ import DEFAULT_CONFIG from './defaults'; import DEPRECATED_CONFIG from './deprecated'; import setFromArgv from './set_from_argv'; import VALID_CONFIG from './valid_config'; +import {ProjectConfig} from '../../../types/Config'; const ERROR = `${BULLET}Validation Error`; const JSON_EXTENSION = '.json'; const PRESET_NAME = 'jest-preset' + JSON_EXTENSION; @@ -237,6 +238,19 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { .digest('hex'); } + options.projects = (options.projects || []).map( + (project: string | ProjectConfig, index) => { + if (!typeof project !== 'string' && !project.hasOwnProperty('name')) { + project.name = crypto + .createHash('md5') + .update(project.rootDir || `${options.rootDir}/${index}`) + .digest('hex'); + } + + return project; + }, + ); + if (!options.setupFiles) { options.setupFiles = []; } diff --git a/types/Config.js b/types/Config.js index f509ad6fec4b..17619d999f7a 100644 --- a/types/Config.js +++ b/types/Config.js @@ -117,7 +117,7 @@ export type InitialOptions = { passWithNoTests?: boolean, preprocessorIgnorePatterns?: Array, preset?: ?string, - projects?: Array, + projects?: Array, replname?: ?string, resetMocks?: boolean, resetModules?: boolean, @@ -192,7 +192,7 @@ export type GlobalConfig = {| onlyChanged: boolean, onlyFailures: boolean, passWithNoTests: boolean, - projects: Array, + projects: Array, replname: ?string, reporters: Array, runTestsByPath: boolean, From 50d9933d1091496f0d1b00dc8a60f74ab44d83bc Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Thu, 22 Mar 2018 23:26:34 -0400 Subject: [PATCH 02/18] added to changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7b5520066a1..563ec82adef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ * `[jest-cli]` Add `isSerial` property that runners can expose to specify that they can not run in parallel [#5706](https://github.com/facebook/jest/pull/5706) +* `[expect]` Add name to project if one does not exist to pick correct resolver + ([#5862](https://github.com/facebook/jest/pull/5862)) ### Fixes From 6036dfa46ebc55267c3c6cf509ca70013d8aeaba Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Fri, 23 Mar 2018 10:15:44 -0400 Subject: [PATCH 03/18] cr comments --- CHANGELOG.md | 4 ++-- packages/jest-config/src/normalize.js | 14 +++++++------- types/Config.js | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 563ec82adef8..c783165f5976 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,8 +34,8 @@ * `[jest-cli]` Add `isSerial` property that runners can expose to specify that they can not run in parallel [#5706](https://github.com/facebook/jest/pull/5706) -* `[expect]` Add name to project if one does not exist to pick correct resolver - ([#5862](https://github.com/facebook/jest/pull/5862)) +* `[jest-config]` Add name to project if one does not exist to pick correct + resolver ([#5862](https://github.com/facebook/jest/pull/5862)) ### Fixes diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index a3540c95b607..39ed24d87925 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -8,7 +8,7 @@ */ import type {Argv} from 'types/Argv'; -import type {InitialOptions, ReporterConfig} from 'types/Config'; +import type {InitialOptions, ReporterConfig, ProjectConfig} from 'types/Config'; import crypto from 'crypto'; import glob from 'glob'; @@ -39,7 +39,7 @@ import DEFAULT_CONFIG from './defaults'; import DEPRECATED_CONFIG from './deprecated'; import setFromArgv from './set_from_argv'; import VALID_CONFIG from './valid_config'; -import {ProjectConfig} from '../../../types/Config'; + const ERROR = `${BULLET}Validation Error`; const JSON_EXTENSION = '.json'; const PRESET_NAME = 'jest-preset' + JSON_EXTENSION; @@ -238,9 +238,9 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { .digest('hex'); } - options.projects = (options.projects || []).map( - (project: string | ProjectConfig, index) => { - if (!typeof project !== 'string' && !project.hasOwnProperty('name')) { + if (Array.isArray(options.projects)) { + options.projects = options.projects.map((project, index) => { + if (typeof project !== 'string' && !project.hasOwnProperty('name')) { project.name = crypto .createHash('md5') .update(project.rootDir || `${options.rootDir}/${index}`) @@ -248,8 +248,8 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { } return project; - }, - ); + }); + } if (!options.setupFiles) { options.setupFiles = []; diff --git a/types/Config.js b/types/Config.js index 17619d999f7a..573e8e007f71 100644 --- a/types/Config.js +++ b/types/Config.js @@ -117,7 +117,7 @@ export type InitialOptions = { passWithNoTests?: boolean, preprocessorIgnorePatterns?: Array, preset?: ?string, - projects?: Array, + projects?: Array, replname?: ?string, resetMocks?: boolean, resetModules?: boolean, @@ -192,7 +192,7 @@ export type GlobalConfig = {| onlyChanged: boolean, onlyFailures: boolean, passWithNoTests: boolean, - projects: Array, + projects: Array, replname: ?string, reporters: Array, runTestsByPath: boolean, From be1cc18238cbab86ebb0d9a57de2336701a93e3e Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Fri, 23 Mar 2018 10:30:39 -0400 Subject: [PATCH 04/18] removed unused import --- 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 39ed24d87925..615033ec76a5 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -8,7 +8,7 @@ */ import type {Argv} from 'types/Argv'; -import type {InitialOptions, ReporterConfig, ProjectConfig} from 'types/Config'; +import type {InitialOptions, ReporterConfig} from 'types/Config'; import crypto from 'crypto'; import glob from 'glob'; From 6054f0bd2e65f2d42ba1bc0aea7f720ed544a44b Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Fri, 23 Mar 2018 14:45:47 -0400 Subject: [PATCH 05/18] fixed tests --- .../jest-config/src/__tests__/normalize.test.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 704c3a0d6070..7a8f83c546be 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -68,11 +68,11 @@ it('picks a name for projects based on the main rootDir', () => { const rootDir = '/root/path/foo'; const firstExpected = crypto .createHash('md5') - .update('/root/path/foo0') + .update('/root/path/foo/0') .digest('hex'); const secondExpected = crypto .createHash('md5') - .update('/root/path/foo1') + .update('/root/path/foo/1') .digest('hex'); const options = normalize( @@ -83,8 +83,8 @@ it('picks a name for projects based on the main rootDir', () => { {}, ); - expect(options.projects[0].name).toBe(firstExpected); - expect(options.projects[1].name).toBe(secondExpected); + expect(options.options.projects[0].name).toBe(firstExpected); + expect(options.options.projects[1].name).toBe(secondExpected); }); it('picks a name for projects based on the projects rootDir', () => { @@ -102,12 +102,13 @@ it('picks a name for projects based on the projects rootDir', () => { const options = normalize( { projects: [{rootDir: firstRootDir}, {rootDir: secondRootDir}], + rootDir: '/root/path/baz', }, {}, ); - expect(options.projects[0].name).toBe(firstExpected); - expect(options.projects[1].name).toBe(secondExpected); + expect(options.options.projects[0].name).toBe(firstExpected); + expect(options.options.projects[1].name).toBe(secondExpected); }); it('keeps custom project name based on the projects rootDir', () => { @@ -115,11 +116,12 @@ it('keeps custom project name based on the projects rootDir', () => { const options = normalize( { projects: [{name, rootDir: '/path/to/foo'}], + rootDir: '/root/path/baz', }, {}, ); - expect(options.projects[0].name).toBe(name); + expect(options.options.projects[0].name).toBe(name); }); it('keeps custom names based on the rootDir', () => { From feb9751d6338a9ddf99c5cb0a93d8e72fe4202a9 Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Fri, 23 Mar 2018 15:16:57 -0400 Subject: [PATCH 06/18] use inital options instead of project config --- integration-tests/timeouts/__tests__/a-banana.js | 9 +++++++++ integration-tests/timeouts/package.json | 1 + packages/jest-cli/src/cli/index.js | 1 + types/Config.js | 4 ++-- 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 integration-tests/timeouts/__tests__/a-banana.js create mode 100644 integration-tests/timeouts/package.json diff --git a/integration-tests/timeouts/__tests__/a-banana.js b/integration-tests/timeouts/__tests__/a-banana.js new file mode 100644 index 000000000000..703c84733ebc --- /dev/null +++ b/integration-tests/timeouts/__tests__/a-banana.js @@ -0,0 +1,9 @@ + + jest.setTimeout(20); + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 100); + }); + }); + \ No newline at end of file diff --git a/integration-tests/timeouts/package.json b/integration-tests/timeouts/package.json new file mode 100644 index 000000000000..9e26dfeeb6e6 --- /dev/null +++ b/integration-tests/timeouts/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 116a07d5989d..28aac1a0202f 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -250,6 +250,7 @@ const getConfigs = ( .filter(root => { // Ignore globbed files that cannot be `require`d. if ( + typeof root === 'string' && fs.existsSync(root) && !fs.lstatSync(root).isDirectory() && !root.endsWith('.js') && diff --git a/types/Config.js b/types/Config.js index 573e8e007f71..41ed6b03ae8b 100644 --- a/types/Config.js +++ b/types/Config.js @@ -117,7 +117,7 @@ export type InitialOptions = { passWithNoTests?: boolean, preprocessorIgnorePatterns?: Array, preset?: ?string, - projects?: Array, + projects?: Array, replname?: ?string, resetMocks?: boolean, resetModules?: boolean, @@ -192,7 +192,7 @@ export type GlobalConfig = {| onlyChanged: boolean, onlyFailures: boolean, passWithNoTests: boolean, - projects: Array, + projects: Array, replname: ?string, reporters: Array, runTestsByPath: boolean, From 0bf6d8ed6de66e883e78f16dc2b7878424276f7f Mon Sep 17 00:00:00 2001 From: Paul Armstrong Date: Fri, 23 Mar 2018 14:35:26 -0700 Subject: [PATCH 07/18] fix unintentional type change --- types/Config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/Config.js b/types/Config.js index 41ed6b03ae8b..f509ad6fec4b 100644 --- a/types/Config.js +++ b/types/Config.js @@ -117,7 +117,7 @@ export type InitialOptions = { passWithNoTests?: boolean, preprocessorIgnorePatterns?: Array, preset?: ?string, - projects?: Array, + projects?: Array, replname?: ?string, resetMocks?: boolean, resetModules?: boolean, @@ -192,7 +192,7 @@ export type GlobalConfig = {| onlyChanged: boolean, onlyFailures: boolean, passWithNoTests: boolean, - projects: Array, + projects: Array, replname: ?string, reporters: Array, runTestsByPath: boolean, From 4f72619ffa2b6fb538fd9e8b0e313f4f21da09ca Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Mon, 26 Mar 2018 09:51:59 -0400 Subject: [PATCH 08/18] fixed path string and deleted unneeded test files --- integration-tests/timeouts/__tests__/a-banana.js | 9 --------- integration-tests/timeouts/package.json | 1 - packages/jest-config/src/__tests__/normalize.test.js | 4 ++-- packages/jest-config/src/normalize.js | 4 ++-- 4 files changed, 4 insertions(+), 14 deletions(-) delete mode 100644 integration-tests/timeouts/__tests__/a-banana.js delete mode 100644 integration-tests/timeouts/package.json diff --git a/integration-tests/timeouts/__tests__/a-banana.js b/integration-tests/timeouts/__tests__/a-banana.js deleted file mode 100644 index 703c84733ebc..000000000000 --- a/integration-tests/timeouts/__tests__/a-banana.js +++ /dev/null @@ -1,9 +0,0 @@ - - jest.setTimeout(20); - - test('banana', () => { - return new Promise(resolve => { - setTimeout(resolve, 100); - }); - }); - \ No newline at end of file diff --git a/integration-tests/timeouts/package.json b/integration-tests/timeouts/package.json deleted file mode 100644 index 9e26dfeeb6e6..000000000000 --- a/integration-tests/timeouts/package.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 7a8f83c546be..6d7f4801b5a6 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -68,11 +68,11 @@ it('picks a name for projects based on the main rootDir', () => { const rootDir = '/root/path/foo'; const firstExpected = crypto .createHash('md5') - .update('/root/path/foo/0') + .update('/root/path/foo:0') .digest('hex'); const secondExpected = crypto .createHash('md5') - .update('/root/path/foo/1') + .update('/root/path/foo:gst1') .digest('hex'); const options = normalize( diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 615033ec76a5..1323f3be022a 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -240,10 +240,10 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { if (Array.isArray(options.projects)) { options.projects = options.projects.map((project, index) => { - if (typeof project !== 'string' && !project.hasOwnProperty('name')) { + if (typeof project !== 'string' && !project.name) { project.name = crypto .createHash('md5') - .update(project.rootDir || `${options.rootDir}/${index}`) + .update(project.rootDir || `${options.rootDir}:${index}`) .digest('hex'); } From 65c64d98d5597d4eae83a598e70bfde8d1464255 Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Mon, 26 Mar 2018 10:29:15 -0400 Subject: [PATCH 09/18] updated snapshots and tests --- .../src/__tests__/__snapshots__/matchers.test.js.snap | 6 +++--- packages/jest-config/src/__tests__/normalize.test.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index cc7fc3b606e9..1a759e202655 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -290,7 +290,7 @@ Received: [] Difference: -Compared values have no visual difference. Looks like you wanted to test for object/array equality with the stricter \`toBe\` matcher. You probably need to use \`toEqual\` instead." +Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` @@ -301,7 +301,7 @@ Received: {\\"a\\": 1} Difference: -Compared values have no visual difference. Looks like you wanted to test for object/array equality with the stricter \`toBe\` matcher. You probably need to use \`toEqual\` instead." +Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` @@ -329,7 +329,7 @@ Received: {} Difference: -Compared values have no visual difference. Looks like you wanted to test for object/array equality with the stricter \`toBe\` matcher. You probably need to use \`toEqual\` instead." +Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: -0 and 0 1`] = ` diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 6d7f4801b5a6..b9c387935b04 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -72,7 +72,7 @@ it('picks a name for projects based on the main rootDir', () => { .digest('hex'); const secondExpected = crypto .createHash('md5') - .update('/root/path/foo:gst1') + .update('/root/path/foo:1') .digest('hex'); const options = normalize( From a856fb102de5cd9089d188d25965fdca2e2616df Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Mon, 26 Mar 2018 10:41:15 -0400 Subject: [PATCH 10/18] removed updated snapshots put change under fixes --- CHANGELOG.md | 4 ++-- .../src/__tests__/__snapshots__/matchers.test.js.snap | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 892dc204c478..9cf41c539035 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,8 +36,6 @@ ([#5706](https://github.com/facebook/jest/pull/5706)) * `[jest-cli]` Interactive Snapshot Mode improvements ([#5864](https://github.com/facebook/jest/pull/5864)) -* `[jest-config]` Add name to project if one does not exist to pick correct - resolver ([#5862](https://github.com/facebook/jest/pull/5862)) ### Fixes @@ -67,6 +65,8 @@ ([#5720](https://github.com/facebook/jest/pull/5720)) * `[pretty-format]` Handle React fragments better ([#5816](https://github.com/facebook/jest/pull/5816)) +* `[jest-config]` Add name to project if one does not exist to pick correct + resolver ([#5862](https://github.com/facebook/jest/pull/5862)) ### Chore & Maintenance diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 1a759e202655..cc7fc3b606e9 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -290,7 +290,7 @@ Received: [] Difference: -Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." +Compared values have no visual difference. Looks like you wanted to test for object/array equality with the stricter \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` @@ -301,7 +301,7 @@ Received: {\\"a\\": 1} Difference: -Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." +Compared values have no visual difference. Looks like you wanted to test for object/array equality with the stricter \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` @@ -329,7 +329,7 @@ Received: {} Difference: -Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." +Compared values have no visual difference. Looks like you wanted to test for object/array equality with the stricter \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: -0 and 0 1`] = ` From 8c8761d4d3b19e87c37911d8cd25e686f3919404 Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Sat, 29 Dec 2018 15:14:12 -0500 Subject: [PATCH 11/18] unique names across config and projects --- .../src/__tests__/normalize.test.js | 19 +++++++++++++++++-- packages/jest-config/src/normalize.js | 9 ++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 977afed63b28..2c6d14c3ccc3 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -67,7 +67,7 @@ it('picks a name for projects based on the main rootDir', () => { const rootDir = '/root/path/foo'; const firstExpected = crypto .createHash('md5') - .update('/root/path/foo:0') + .update('/root/path/foo') .digest('hex'); const secondExpected = crypto .createHash('md5') @@ -94,13 +94,26 @@ it('picks a name for projects based on the projects rootDir', () => { .update('/root/path/foo') .digest('hex'); const secondExpected = crypto + .createHash('md5') + .update('/root/path/foo:1') + .digest('hex'); + const thirdExpected = crypto .createHash('md5') .update('/root/path/bar') .digest('hex'); + const fourthExpected = crypto + .createHash('md5') + .update('/root/path/baz') + .digest('hex'); const options = normalize( { - projects: [{rootDir: firstRootDir}, {rootDir: secondRootDir}], + projects: [ + {rootDir: firstRootDir}, + {rootDir: firstRootDir}, + {rootDir: secondRootDir}, + {}, + ], rootDir: '/root/path/baz', }, {}, @@ -108,6 +121,8 @@ it('picks a name for projects based on the projects rootDir', () => { expect(options.options.projects[0].name).toBe(firstExpected); expect(options.options.projects[1].name).toBe(secondExpected); + expect(options.options.projects[2].name).toBe(thirdExpected); + expect(options.options.projects[3].name).toBe(fourthExpected); }); it('keeps custom project name based on the projects rootDir', () => { diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index f67f76a38271..23812519a7f8 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -266,6 +266,7 @@ const normalizePreprocessor = (options: InitialOptions): InitialOptions => { }; const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { + const knownRootDirs = {}; if (!options.name) { options.name = crypto .createHash('md5') @@ -276,9 +277,15 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { if (Array.isArray(options.projects)) { options.projects = options.projects.map((project, index) => { if (typeof project !== 'string' && !project.name) { + let rootDir = project.rootDir || options.rootDir; + if (knownRootDirs[rootDir]) { + rootDir = `${rootDir}:${index}`; + } + + knownRootDirs[rootDir] = true; project.name = crypto .createHash('md5') - .update(project.rootDir || `${options.rootDir}:${index}`) + .update(rootDir) .digest('hex'); } From 78801aad18a5026061ac6e7082cb5d901dffe87a Mon Sep 17 00:00:00 2001 From: Chris Belsole Date: Sat, 29 Dec 2018 15:15:06 -0500 Subject: [PATCH 12/18] fixed changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cc05a2f992c..3b9c54698725 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -200,7 +200,7 @@ - `[jest-snapshot]` Add error messages for invalid property matchers. ([#6528](https://github.com/facebook/jest/pull/6528)) - `[jest-cli]` Show open handles from inside test files as well ([#6263](https://github.com/facebook/jest/pull/6263)) - `[jest-haste-map]` Fix a problem where creating folders ending with `.js` could cause a crash ([#6818](https://github.com/facebook/jest/pull/6818)) -- `[jest-config]` Add name to project if one does not exist to pick correct resolver ([#5862](https://github.com/facebook/jest/pull/5862)) +- `[master]` Add name to project if one does not exist to pick correct resolver ([#5862](https://github.com/facebook/jest/pull/5862)) ### Chore & Maintenance From 9bbbd61dbdb4ce774b73d427127826deb9d842f1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 29 Dec 2018 21:19:10 +0100 Subject: [PATCH 13/18] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b9c54698725..b32b3b156f1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,6 +102,7 @@ - `[jest-cli]` print info about passWithNoTests flag ([#7309](https://github.com/facebook/jest/pull/7309)) - `[pretty-format]` Omit unnecessary symbol filter for object keys ([#7457](https://github.com/facebook/jest/pull/7457)) - `[jest-runtime]` Fix `requireActual` on node_modules with mock present ([#7404](https://github.com/facebook/jest/pull/7404)) +- `[jest-config]` Add name to project if one does not exist to pick correct resolver ([#5862](https://github.com/facebook/jest/pull/5862)) ### Chore & Maintenance @@ -200,7 +201,6 @@ - `[jest-snapshot]` Add error messages for invalid property matchers. ([#6528](https://github.com/facebook/jest/pull/6528)) - `[jest-cli]` Show open handles from inside test files as well ([#6263](https://github.com/facebook/jest/pull/6263)) - `[jest-haste-map]` Fix a problem where creating folders ending with `.js` could cause a crash ([#6818](https://github.com/facebook/jest/pull/6818)) -- `[master]` Add name to project if one does not exist to pick correct resolver ([#5862](https://github.com/facebook/jest/pull/5862)) ### Chore & Maintenance From 44aa91d545e71bba9e2301b89c01e06d8365a35a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 20 Jan 2019 17:11:58 +0100 Subject: [PATCH 14/18] use Set --- packages/jest-config/src/normalize.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 7c35137fbb71..76e7833aece2 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -264,7 +264,7 @@ const normalizePreprocessor = (options: InitialOptions): InitialOptions => { }; const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { - const knownRootDirs = {}; + const knownRootDirs = new Set(); if (!options.name) { options.name = crypto .createHash('md5') @@ -276,11 +276,11 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { options.projects = options.projects.map((project, index) => { if (typeof project !== 'string' && !project.name) { let rootDir = project.rootDir || options.rootDir; - if (knownRootDirs[rootDir]) { + if (knownRootDirs.has(rootDir)) { rootDir = `${rootDir}:${index}`; } - knownRootDirs[rootDir] = true; + knownRootDirs.add(rootDir); project.name = crypto .createHash('md5') .update(rootDir) From a370c21539aad54f79d577b3b95203351b1fc169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 20 Jan 2019 19:13:20 +0100 Subject: [PATCH 15/18] use random uuid for assigning project name --- packages/jest-config/package.json | 3 +- .../src/__tests__/normalize.test.js | 88 +++---------------- packages/jest-config/src/normalize.js | 22 +---- 3 files changed, 15 insertions(+), 98 deletions(-) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index f41992144802..63d03d525a13 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -23,7 +23,8 @@ "jest-validate": "^23.6.0", "micromatch": "^3.1.10", "pretty-format": "^23.6.0", - "realpath-native": "^1.0.2" + "realpath-native": "^1.0.2", + "uuid": "^3.3.2" }, "engines": { "node": ">= 6" diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 271b7153b0fb..a4df3af60e81 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -47,87 +47,21 @@ beforeEach(() => { require('jest-resolve').findNodeModule = findNodeModule; }); -it('picks a name based on the rootDir', () => { +it('assigns a random 32-byte hash as a name to avoid clashes', () => { const rootDir = '/root/path/foo'; - const expected = crypto - .createHash('md5') - .update('/root/path/foo') - .digest('hex'); - expect( - normalize( - { - rootDir, - }, - {}, - ).options.name, - ).toBe(expected); -}); - -it('picks a name for projects based on the main rootDir', () => { - const rootDir = '/root/path/foo'; - const firstExpected = crypto - .createHash('md5') - .update('/root/path/foo') - .digest('hex'); - const secondExpected = crypto - .createHash('md5') - .update('/root/path/foo:1') - .digest('hex'); - - const options = normalize( - { - projects: [{}, {}], - rootDir, - }, - {}, - ); - - expect(options.options.projects[0].name).toBe(firstExpected); - expect(options.options.projects[1].name).toBe(secondExpected); -}); - -it('picks a name for projects based on the projects rootDir', () => { - const firstRootDir = '/root/path/foo'; - const secondRootDir = '/root/path/bar'; - const firstExpected = crypto - .createHash('md5') - .update('/root/path/foo') - .digest('hex'); - const secondExpected = crypto - .createHash('md5') - .update('/root/path/foo:1') - .digest('hex'); - const thirdExpected = crypto - .createHash('md5') - .update('/root/path/bar') - .digest('hex'); - const fourthExpected = crypto - .createHash('md5') - .update('/root/path/baz') - .digest('hex'); - - const options = normalize( - { - projects: [ - {rootDir: firstRootDir}, - {rootDir: firstRootDir}, - {rootDir: secondRootDir}, - {}, - ], - rootDir: '/root/path/baz', - }, - {}, - ); - - expect(options.options.projects[0].name).toBe(firstExpected); - expect(options.options.projects[1].name).toBe(secondExpected); - expect(options.options.projects[2].name).toBe(thirdExpected); - expect(options.options.projects[3].name).toBe(fourthExpected); + const {name: name1} = normalize({rootDir}, {}).options; + const {name: name2} = normalize({rootDir}, {}).options; + + expect(name1).toEqual(expect.any(String)); + expect(name1).toHaveLength(32); + expect(name2).toEqual(expect.any(String)); + expect(name2).toHaveLength(32); + expect(name1).not.toBe(name2); }); it('keeps custom project name based on the projects rootDir', () => { const name = 'test'; - const options = normalize( + const {options} = normalize( { projects: [{name, rootDir: '/path/to/foo'}], rootDir: '/root/path/baz', @@ -135,7 +69,7 @@ it('keeps custom project name based on the projects rootDir', () => { {}, ); - expect(options.options.projects[0].name).toBe(name); + expect(options.projects[0].name).toBe(name); }); it('keeps custom names based on the rootDir', () => { diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 76e7833aece2..f63714494aca 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -17,6 +17,7 @@ import type { } from 'types/Config'; import crypto from 'crypto'; +import uuid from 'uuid/v4'; import glob from 'glob'; import path from 'path'; import {ValidationError, validate} from 'jest-validate'; @@ -264,33 +265,14 @@ const normalizePreprocessor = (options: InitialOptions): InitialOptions => { }; const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { - const knownRootDirs = new Set(); if (!options.name) { options.name = crypto .createHash('md5') .update(options.rootDir) + .update(uuid()) .digest('hex'); } - if (Array.isArray(options.projects)) { - options.projects = options.projects.map((project, index) => { - if (typeof project !== 'string' && !project.name) { - let rootDir = project.rootDir || options.rootDir; - if (knownRootDirs.has(rootDir)) { - rootDir = `${rootDir}:${index}`; - } - - knownRootDirs.add(rootDir); - project.name = crypto - .createHash('md5') - .update(rootDir) - .digest('hex'); - } - - return project; - }); - } - if (!options.setupFiles) { options.setupFiles = []; } From 1ab0bacef44b5d97b657180b09e6fc7babe00dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 20 Jan 2019 19:19:53 +0100 Subject: [PATCH 16/18] remove unnecessary test --- .../jest-config/src/__tests__/normalize.test.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index a4df3af60e81..b850c428b0c9 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -59,19 +59,6 @@ it('assigns a random 32-byte hash as a name to avoid clashes', () => { expect(name1).not.toBe(name2); }); -it('keeps custom project name based on the projects rootDir', () => { - const name = 'test'; - const {options} = normalize( - { - projects: [{name, rootDir: '/path/to/foo'}], - rootDir: '/root/path/baz', - }, - {}, - ); - - expect(options.projects[0].name).toBe(name); -}); - it('keeps custom names based on the rootDir', () => { expect( normalize( From 691339f7dccbc9a1e095844174bc1bbd1f9844dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 20 Jan 2019 20:20:31 +0100 Subject: [PATCH 17/18] fix lint --- packages/jest-config/src/__tests__/normalize.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index b850c428b0c9..ffebc661caa9 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -12,7 +12,6 @@ import normalize from '../normalize'; jest.mock('jest-resolve'); jest.mock('path', () => jest.requireActual('path').posix); -const crypto = require('crypto'); const path = require('path'); const DEFAULT_JS_PATTERN = require('../constants').DEFAULT_JS_PATTERN; const DEFAULT_CSS_PATTERN = '^.+\\.(css)$'; From 6fcab164b78b538c4780582abad8bbfc6dc535b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 20 Jan 2019 20:20:48 +0100 Subject: [PATCH 18/18] add e2e test --- e2e/__tests__/multiProjectRunner.test.js | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/e2e/__tests__/multiProjectRunner.test.js b/e2e/__tests__/multiProjectRunner.test.js index b423df611b51..b3438cb90905 100644 --- a/e2e/__tests__/multiProjectRunner.test.js +++ b/e2e/__tests__/multiProjectRunner.test.js @@ -399,3 +399,41 @@ test('Does transform files with the corresponding project transformer', () => { expect(stderr).toMatch('PASS project1/__tests__/project1.test.js'); expect(stderr).toMatch('PASS project2/__tests__/project2.test.js'); }); + +test("doesn't bleed module file extensions resolution with multiple workers", () => { + writeFiles(DIR, { + '.watchmanconfig': '', + 'file.js': 'module.exports = "file1"', + 'file.p2.js': 'module.exports = "file2"', + 'package.json': '{}', + 'project1/__tests__/project1.test.js': ` + const file = require('../../file'); + test('file 1', () => expect(file).toBe('file1')); + `, + 'project1/jest.config.js': ` + module.exports = { + rootDir: '..', + };`, + 'project2/__tests__/project2.test.js': ` + const file = require('../../file'); + test('file 2', () => expect(file).toBe('file2')); + `, + 'project2/jest.config.js': ` + module.exports = { + rootDir: '..', + moduleFileExtensions: ['p2.js', 'js'] + };`, + }); + + const {stderr} = runJest(DIR, [ + '--no-watchman', + '-w=2', + '--projects', + 'project1', + 'project2', + ]); + + expect(stderr).toMatch('Ran all test suites in 2 projects.'); + expect(stderr).toMatch('PASS project1/__tests__/project1.test.js'); + expect(stderr).toMatch('PASS project2/__tests__/project2.test.js'); +});