From 03199e5971236f5172835b3b9cc55d02a6333f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 15 Oct 2018 17:28:24 +0100 Subject: [PATCH] Make ignorePattern optional in HasteMap --- CHANGELOG.md | 2 ++ .../jest-haste-map/src/__tests__/index.test.js | 14 ++++++++++---- packages/jest-haste-map/src/index.js | 10 +++++----- packages/jest-runtime/src/index.js | 12 +++++++++--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9696b0bc75d3..a25fa16d4ae8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ - `[jest-haste-map]` [**BREAKING**] Remove support for `@providesModule` ([#6104](https://github.com/facebook/jest/pull/6104)) - `[pretty-format]` Support HTMLCollection and NodeList in DOMCollection plugin ([#7125](https://github.com/facebook/jest/pull/7125)) - `[jest-runtime]` Pass the normalized configuration to script transformers ([#7148](https://github.com/facebook/jest/pull/7148)) +- `[jest-haste-map]` Make `ignorePattern` optional ([#7166](https://github.com/facebook/jest/pull/7166)) +- `[jest-runtime]` Remove `cacheDirectory` from `ignorePattern` for `HasteMap` if not necessary ([#7166](https://github.com/facebook/jest/pull/7166)) ### Fixes diff --git a/packages/jest-haste-map/src/__tests__/index.test.js b/packages/jest-haste-map/src/__tests__/index.test.js index b67f5ec3dbaa..efd8a10aa6ed 100644 --- a/packages/jest-haste-map/src/__tests__/index.test.js +++ b/packages/jest-haste-map/src/__tests__/index.test.js @@ -144,9 +144,6 @@ describe('HasteMap', () => { '/project/fruits/Banana.js': ` const Strawberry = require("Strawberry"); `, - '/project/fruits/Kiwi.js': ` - // Kiwi! - `, '/project/fruits/Pear.js': ` const Banana = require("Banana"); const Strawberry = require("Strawberry"); @@ -189,7 +186,6 @@ describe('HasteMap', () => { defaultConfig = { extensions: ['js', 'json'], hasteImplModulePath: require.resolve('./haste_impl.js'), - ignorePattern: /Kiwi/, maxWorkers: 1, name: 'haste-map-test', platforms: ['ios', 'android'], @@ -237,6 +233,16 @@ describe('HasteMap', () => { ]); })); + it('ignores files given a pattern', () => { + const config = Object.assign({}, defaultConfig, {ignorePattern: /Kiwi/}); + mockFs['/project/fruits/Kiwi.js'] = ` + // Kiwi! + `; + return new HasteMap(config).build().then(({hasteFS}) => { + expect(hasteFS.matchFiles(/Kiwi/)).toEqual([]); + }); + }); + it('builds a haste map on a fresh cache', () => { // Include these files in the map mockFs['/project/fruits/node_modules/react/React.js'] = ` diff --git a/packages/jest-haste-map/src/index.js b/packages/jest-haste-map/src/index.js index 77d7fb17ef99..5ea1cac2386a 100644 --- a/packages/jest-haste-map/src/index.js +++ b/packages/jest-haste-map/src/index.js @@ -54,7 +54,7 @@ type Options = { extensions: Array, forceNodeFilesystemAPI?: boolean, hasteImplModulePath?: string, - ignorePattern: HasteRegExp, + ignorePattern?: ?HasteRegExp, maxWorkers: number, mocksPattern?: string, name: string, @@ -76,7 +76,7 @@ type InternalOptions = { extensions: Array, forceNodeFilesystemAPI: boolean, hasteImplModulePath?: string, - ignorePattern: HasteRegExp, + ignorePattern: ?HasteRegExp, maxWorkers: number, mocksPattern: ?RegExp, name: string, @@ -249,7 +249,7 @@ class HasteMap extends EventEmitter { watch: !!options.watch, }; this._console = options.console || global.console; - if (!(options.ignorePattern instanceof RegExp)) { + if (options.ignorePattern && !(options.ignorePattern instanceof RegExp)) { this._console.warn( 'jest-haste-map: the `ignorePattern` options as a function is being ' + 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.', @@ -270,7 +270,7 @@ class HasteMap extends EventEmitter { this._options.platforms.join(':'), this._options.computeSha1.toString(), options.mocksPattern || '', - options.ignorePattern.toString(), + (options.ignorePattern || '').toString(), ); this._whitelist = getWhiteList(options.providesModuleNodeModules); this._buildPromise = null; @@ -966,7 +966,7 @@ class HasteMap extends EventEmitter { const ignoreMatched = ignorePattern instanceof RegExp ? ignorePattern.test(filePath) - : ignorePattern(filePath); + : ignorePattern && ignorePattern(filePath); return ( ignoreMatched || diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index bea9ef463f98..ae0a5a9ab94f 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -223,9 +223,15 @@ class Runtime { config: ProjectConfig, options?: HasteMapOptions, ): HasteMap { - const ignorePattern = new RegExp( - [config.cacheDirectory].concat(config.modulePathIgnorePatterns).join('|'), - ); + const ignorePatternParts = [ + ...config.modulePathIgnorePatterns, + config.cacheDirectory.startsWith(config.rootDir + path.sep) && + config.cacheDirectory, + ].filter(Boolean); + const ignorePattern = + ignorePatternParts.length > 0 + ? new RegExp(ignorePatternParts.join('|')) + : null; return new HasteMap({ cacheDirectory: config.cacheDirectory,