From 68782ca2da6dff0ddedf2b662d6f39a309888cfd Mon Sep 17 00:00:00 2001 From: Kevin Groat Date: Thu, 19 Apr 2018 13:51:11 -0400 Subject: [PATCH 1/7] Adding noDuplicateMockWarn cli option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This option disables the “jest-haste-map: duplicate manual mock found” warning --- TestUtils.js | 1 + docs/CLI.md | 4 +++ packages/jest-cli/src/cli/args.js | 5 ++++ packages/jest-config/src/defaults.js | 1 + packages/jest-config/src/index.js | 1 + packages/jest-config/src/valid_config.js | 1 + .../src/__tests__/index.test.js | 25 +++++++++++++++++++ packages/jest-haste-map/src/index.js | 5 +++- packages/jest-runtime/src/index.js | 1 + .../src/__tests__/fixtures/jest_config.js | 2 ++ types/Argv.js | 1 + types/Config.js | 3 +++ 12 files changed, 49 insertions(+), 1 deletion(-) diff --git a/TestUtils.js b/TestUtils.js index 0050dd123c77..dc6b3c07ee26 100644 --- a/TestUtils.js +++ b/TestUtils.js @@ -83,6 +83,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = { modulePathIgnorePatterns: [], modulePaths: [], name: 'test_name', + noDuplicateMockWarn: false, resetMocks: false, resetModules: false, resolver: null, diff --git a/docs/CLI.md b/docs/CLI.md index 071ecba4d5e7..1a93f7f29eb2 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -234,6 +234,10 @@ for running tests. This defaults to the number of the cores available on your machine. It may be useful to adjust this in resource limited environments like CIs but the default should be adequate for most use-cases. +### `--noDuplicateMockWarn` + +Disables duplicate mock warnings in output + ### `--noStackTrace` Disables stack trace in test results output. diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 87e50f9b0d49..cfb03291cf88 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -353,6 +353,11 @@ export const options = { 'search when resolving modules.', type: 'array', }, + noDuplicateMockWarn: { + default: false, + description: 'Disables duplicate mock warnings in output', + type: 'boolean', + }, noStackTrace: { default: undefined, description: 'Disables stack trace in test results output', diff --git a/packages/jest-config/src/defaults.js b/packages/jest-config/src/defaults.js index ee576e1aeef4..f00c7c2e9d6c 100644 --- a/packages/jest-config/src/defaults.js +++ b/packages/jest-config/src/defaults.js @@ -50,6 +50,7 @@ export default ({ moduleFileExtensions: ['js', 'json', 'jsx', 'node'], moduleNameMapper: {}, modulePathIgnorePatterns: [], + noDuplicateMockWarn: false, noStackTrace: false, notify: false, notifyMode: 'always', diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 2d61877849c7..dacdec05f59b 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -163,6 +163,7 @@ const getConfigs = ( modulePathIgnorePatterns: options.modulePathIgnorePatterns, modulePaths: options.modulePaths, name: options.name, + noDuplicateMockWarn: options.noDuplicateMockWarn, resetMocks: options.resetMocks, resetModules: options.resetModules, resolver: options.resolver, diff --git a/packages/jest-config/src/valid_config.js b/packages/jest-config/src/valid_config.js index 9684ce69263a..508043201f52 100644 --- a/packages/jest-config/src/valid_config.js +++ b/packages/jest-config/src/valid_config.js @@ -58,6 +58,7 @@ export default ({ modulePathIgnorePatterns: ['/build/'], modulePaths: ['/shared/vendor/modules'], name: 'string', + noDuplicateMockWarn: false, noStackTrace: false, notify: false, notifyMode: 'always', diff --git a/packages/jest-haste-map/src/__tests__/index.test.js b/packages/jest-haste-map/src/__tests__/index.test.js index 5920d0ae7905..4c4b5939daa9 100644 --- a/packages/jest-haste-map/src/__tests__/index.test.js +++ b/packages/jest-haste-map/src/__tests__/index.test.js @@ -480,6 +480,31 @@ describe('HasteMap', () => { }); }); + it('does not warn on duplicate mock files if noDuplicateMockWarn is true', () => { + // Duplicate mock files for blueberry + mockFs['/fruits1/__mocks__/subdir/blueberry.js'] = [ + '/**', + ' * @providesModule Blueberry1', + ' */', + ].join('\n'); + mockFs['/fruits2/__mocks__/subdir/blueberry.js'] = [ + '/**', + ' * @providesModule Blueberry2', + ' */', + ].join('\n'); + + return new HasteMap( + Object.assign( + {mocksPattern: '__mocks__', noDuplicateMockWarn: true}, + defaultConfig, + ), + ) + .build() + .then(({__hasteMapForTest: data}) => { + expect(console.warn).not.toHaveBeenCalled(); + }); + }); + it('warns on duplicate module ids', () => { // Raspberry thinks it is a Strawberry mockFs['/fruits/raspberry.js'] = [ diff --git a/packages/jest-haste-map/src/index.js b/packages/jest-haste-map/src/index.js index 8637caa1956a..acfed426510f 100644 --- a/packages/jest-haste-map/src/index.js +++ b/packages/jest-haste-map/src/index.js @@ -53,6 +53,7 @@ type Options = { maxWorkers: number, mocksPattern?: string, name: string, + noDuplicateMockWarn?: boolean, platforms: Array, providesModuleNodeModules?: Array, resetCache?: boolean, @@ -73,6 +74,7 @@ type InternalOptions = { maxWorkers: number, mocksPattern: ?RegExp, name: string, + noDuplicateMockWarn: ?boolean, platforms: Array, resetCache: ?boolean, retainAllFiles: boolean, @@ -223,6 +225,7 @@ class HasteMap extends EventEmitter { ? new RegExp(options.mocksPattern) : null, name: options.name, + noDuplicateMockWarn: options.noDuplicateMockWarn, platforms: options.platforms, resetCache: options.resetCache, retainAllFiles: options.retainAllFiles, @@ -418,7 +421,7 @@ class HasteMap extends EventEmitter { this._options.mocksPattern.test(filePath) ) { const mockPath = getMockName(filePath); - if (mocks[mockPath]) { + if (mocks[mockPath] && !this._options.noDuplicateMockWarn) { this._console.warn( `jest-haste-map: duplicate manual mock found:\n` + ` Module name: ${mockPath}\n` + diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index b21208815648..40f0ae8cc875 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -235,6 +235,7 @@ class Runtime { maxWorkers: (options && options.maxWorkers) || 1, mocksPattern: escapePathForRegex(path.sep + '__mocks__' + path.sep), name: config.name, + noDuplicateMockWarn: config.noDuplicateMockWarn, platforms: config.haste.platforms || ['ios', 'android'], providesModuleNodeModules: config.haste.providesModuleNodeModules, resetCache: options && options.resetCache, diff --git a/packages/jest-validate/src/__tests__/fixtures/jest_config.js b/packages/jest-validate/src/__tests__/fixtures/jest_config.js index 263ec29aa871..7722e6a50450 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jest_config.js +++ b/packages/jest-validate/src/__tests__/fixtures/jest_config.js @@ -37,6 +37,7 @@ const defaultConfig = { moduleFileExtensions: ['js', 'json', 'jsx', 'node'], moduleNameMapper: {}, modulePathIgnorePatterns: [], + noDuplicateMockWarn: false, noStackTrace: false, notify: false, notifyMode: 'always', @@ -95,6 +96,7 @@ const validConfig = { modulePathIgnorePatterns: ['/build/'], modulePaths: ['/shared/vendor/modules'], name: 'string', + noDuplicateMockWarn: false, noStackTrace: false, notify: false, notifyMode: 'always', diff --git a/types/Argv.js b/types/Argv.js index 81926b868cc7..4bd89887ffd1 100644 --- a/types/Argv.js +++ b/types/Argv.js @@ -53,6 +53,7 @@ export type Argv = {| modulePathIgnorePatterns: Array, modulePaths: Array, name: string, + noDuplicateMockWarn: boolean, noSCM: boolean, noStackTrace: boolean, notify: boolean, diff --git a/types/Config.js b/types/Config.js index f509ad6fec4b..1f4c2675fcbd 100644 --- a/types/Config.js +++ b/types/Config.js @@ -42,6 +42,7 @@ export type DefaultOptions = {| moduleFileExtensions: Array, moduleNameMapper: {[key: string]: string}, modulePathIgnorePatterns: Array, + noDuplicateMockWarn: boolean, noStackTrace: boolean, notify: boolean, notifyMode: string, @@ -109,6 +110,7 @@ export type InitialOptions = { modulePathIgnorePatterns?: Array, modulePaths?: Array, name?: string, + noDuplicateMockWarn?: boolean, noStackTrace?: boolean, notify?: boolean, notifyMode?: string, @@ -231,6 +233,7 @@ export type ProjectConfig = {| modulePathIgnorePatterns: Array, modulePaths: Array, name: string, + noDuplicateMockWarn: boolean, resetMocks: boolean, resetModules: boolean, resolver: ?Path, From 28919f7e16178db9c2cb0d203971890ecd6e4c36 Mon Sep 17 00:00:00 2001 From: Kevin Groat Date: Thu, 19 Apr 2018 14:05:12 -0400 Subject: [PATCH 2/7] Updated changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b117fe5e892..d76677ffc9b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ### Features +* ([#6037](https://github.com/facebook/jest/pull/6037)) + `[jest-cli]` Added `--noDuplicateMockWarn` to disable HasteMap warning of + duplicate mocks. * `[jest-snapshot]` [**BREAKING**] Concatenate name of test, optional snapshot name and count ([#6015](https://github.com/facebook/jest/pull/6015)) * `[jest-runtime]` Allow for transform plugins to skip the definition process From 4321b56c0cfb41187637144479f4171f6c152e53 Mon Sep 17 00:00:00 2001 From: Kevin Groat Date: Thu, 19 Apr 2018 16:35:05 -0400 Subject: [PATCH 3/7] Fixed issue of the arg always being false if from config --- packages/jest-cli/src/cli/args.js | 2 +- packages/jest-config/src/normalize.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index cfb03291cf88..ac5764375cb6 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -354,7 +354,7 @@ export const options = { type: 'array', }, noDuplicateMockWarn: { - default: false, + default: undefined, description: 'Disables duplicate mock warnings in output', type: 'boolean', }, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 75501d305e9b..b1177858d1da 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -512,6 +512,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { case 'mapCoverage': case 'moduleFileExtensions': case 'name': + case 'noDuplicateMockWarn': case 'noStackTrace': case 'notify': case 'notifyMode': From 905ee4d530403010761064d7afca1b54f3a78e55 Mon Sep 17 00:00:00 2001 From: Kevin Groat Date: Thu, 19 Apr 2018 16:36:40 -0400 Subject: [PATCH 4/7] Added to changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d76677ffc9b2..33e7f3d528c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,8 @@ ### Features * ([#6037](https://github.com/facebook/jest/pull/6037)) - `[jest-cli]` Added `--noDuplicateMockWarn` to disable HasteMap warning of - duplicate mocks. + `[jest-cli]`, `[jest-haste-map]` Added `--noDuplicateMockWarn` to disable + HasteMap warning of duplicate mocks. * `[jest-snapshot]` [**BREAKING**] Concatenate name of test, optional snapshot name and count ([#6015](https://github.com/facebook/jest/pull/6015)) * `[jest-runtime]` Allow for transform plugins to skip the definition process From 2fcef6e0b33517f6287f1ea1c584aa705772539a Mon Sep 17 00:00:00 2001 From: Kevin Groat Date: Thu, 19 Apr 2018 16:41:25 -0400 Subject: [PATCH 5/7] Fixed changelog formatting --- CHANGELOG.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33e7f3d528c5..d629105bc065 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,12 @@ ### Features -* ([#6037](https://github.com/facebook/jest/pull/6037)) - `[jest-cli]`, `[jest-haste-map]` Added `--noDuplicateMockWarn` to disable - HasteMap warning of duplicate mocks. +* `[jest-cli]` Added `--noDuplicateMockWarn` to disable HasteMap warning of + duplicate mocks. + ([#6037](https://github.com/facebook/jest/pull/6037)) +* `[jest-haste-map]` Added `noDuplicateMockWarn` option to disable warning of + duplicate mocks. + ([#6037](https://github.com/facebook/jest/pull/6037)) * `[jest-snapshot]` [**BREAKING**] Concatenate name of test, optional snapshot name and count ([#6015](https://github.com/facebook/jest/pull/6015)) * `[jest-runtime]` Allow for transform plugins to skip the definition process From 79abb80b403a81ac0c985a9e2aa4865ac0f01872 Mon Sep 17 00:00:00 2001 From: Kevin Groat Date: Thu, 19 Apr 2018 17:51:47 -0400 Subject: [PATCH 6/7] Fixed test --- .../__tests__/__snapshots__/show_config.test.js.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/integration-tests/__tests__/__snapshots__/show_config.test.js.snap b/integration-tests/__tests__/__snapshots__/show_config.test.js.snap index 22911ad113be..f1045e088c20 100644 --- a/integration-tests/__tests__/__snapshots__/show_config.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/show_config.test.js.snap @@ -30,6 +30,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` \\"moduleNameMapper\\": {}, \\"modulePathIgnorePatterns\\": [], \\"name\\": \\"[md5 hash]\\", + \\"noDuplicateMockWarn\\": false, \\"resetMocks\\": false, \\"resetModules\\": false, \\"restoreMocks\\": false, From 4c0163572a484a98bf38de7bd8b1b277c1991c12 Mon Sep 17 00:00:00 2001 From: Kevin Groat Date: Fri, 20 Apr 2018 12:46:38 -0400 Subject: [PATCH 7/7] FIxed prettylint errors --- CHANGELOG.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d629105bc065..1b5087496f5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,9 @@ ### Features * `[jest-cli]` Added `--noDuplicateMockWarn` to disable HasteMap warning of - duplicate mocks. - ([#6037](https://github.com/facebook/jest/pull/6037)) + duplicate mocks. ([#6037](https://github.com/facebook/jest/pull/6037)) * `[jest-haste-map]` Added `noDuplicateMockWarn` option to disable warning of - duplicate mocks. - ([#6037](https://github.com/facebook/jest/pull/6037)) + duplicate mocks. ([#6037](https://github.com/facebook/jest/pull/6037)) * `[jest-snapshot]` [**BREAKING**] Concatenate name of test, optional snapshot name and count ([#6015](https://github.com/facebook/jest/pull/6015)) * `[jest-runtime]` Allow for transform plugins to skip the definition process