diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c380204a2f7..11e6c5e964a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -121,6 +121,7 @@ - `[jest-environment-node]` Fix buffer property is not ArrayBuffer issue. ([#7626](https://github.com/facebook/jest/pull/7626)) - `[babel-plugin-jest-hoist]` Ignore TS type annotations when looking for out-of-scope references ([#7641](https://github.com/facebook/jest/pull/7641)) - `[jest-config]` Add name to project if one does not exist to pick correct resolver ([#5862](https://github.com/facebook/jest/pull/5862)) +- `[jest-runtime]` Pass `watchPathIgnorePatterns` to Haste instance ([#7585](https://github.com/facebook/jest/pull/7585)) ### Chore & Maintenance diff --git a/packages/jest-cli/src/lib/__tests__/is_valid_path.test.js b/packages/jest-cli/src/lib/__tests__/is_valid_path.test.js index 6a51280f796d..2dbf4aaade1a 100644 --- a/packages/jest-cli/src/lib/__tests__/is_valid_path.test.js +++ b/packages/jest-cli/src/lib/__tests__/is_valid_path.test.js @@ -8,40 +8,24 @@ */ import isValidPath from '../is_valid_path'; - -const path = require('path'); -const { - makeGlobalConfig, - makeProjectConfig, -} = require('../../../../../TestUtils'); +import path from 'path'; +import {makeGlobalConfig} from '../../../../../TestUtils'; const rootDir = path.resolve(path.sep, 'root'); -const config = makeProjectConfig({ - rootDir, - roots: [path.resolve(rootDir, 'src'), path.resolve(rootDir, 'lib')], - watchPathIgnorePatterns: ['pacts'], -}); - it('is valid when it is a file inside roots', () => { expect( - isValidPath( - makeGlobalConfig(), - config, - path.resolve(rootDir, 'src', 'index.js'), - ), + isValidPath(makeGlobalConfig(), path.resolve(rootDir, 'src', 'index.js')), ).toBe(true); expect( isValidPath( makeGlobalConfig(), - config, path.resolve(rootDir, 'src', 'components', 'Link.js'), ), ).toBe(true); expect( isValidPath( makeGlobalConfig(), - config, path.resolve(rootDir, 'src', 'lib', 'something.js'), ), ).toBe(true); @@ -51,21 +35,18 @@ it('is not valid when it is a snapshot file', () => { expect( isValidPath( makeGlobalConfig(), - config, path.resolve(rootDir, 'src', 'index.js.snap'), ), ).toBe(false); expect( isValidPath( makeGlobalConfig(), - config, path.resolve(rootDir, 'src', 'components', 'Link.js.snap'), ), ).toBe(false); expect( isValidPath( makeGlobalConfig(), - config, path.resolve(rootDir, 'src', 'lib', 'something.js.snap'), ), ).toBe(false); @@ -75,7 +56,6 @@ it('is not valid when it is a file in the coverage dir', () => { expect( isValidPath( makeGlobalConfig({rootDir}), - config, path.resolve(rootDir, 'coverage', 'lib', 'index.js'), ), ).toBe(false); @@ -83,18 +63,7 @@ it('is not valid when it is a file in the coverage dir', () => { expect( isValidPath( makeGlobalConfig({coverageDirectory: 'cov-dir'}), - config, path.resolve(rootDir, 'src', 'cov-dir', 'lib', 'index.js'), ), ).toBe(false); }); - -it('is not valid when it is a file match one of the watchPathIgnorePatterns', () => { - expect( - isValidPath( - makeGlobalConfig({rootDir}), - config, - path.resolve(rootDir, 'pacts', 'todoapp-todoservice.json'), - ), - ).toBe(false); -}); diff --git a/packages/jest-cli/src/lib/is_valid_path.js b/packages/jest-cli/src/lib/is_valid_path.js index 1d2620b674d5..ffd14efb8fff 100644 --- a/packages/jest-cli/src/lib/is_valid_path.js +++ b/packages/jest-cli/src/lib/is_valid_path.js @@ -7,17 +7,15 @@ * @flow */ -import type {GlobalConfig, ProjectConfig} from 'types/Config'; +import type {GlobalConfig} from 'types/Config'; import {isSnapshotPath} from 'jest-snapshot'; export default function isValidPath( globalConfig: GlobalConfig, - config: ProjectConfig, filePath: string, ) { return ( !filePath.includes(globalConfig.coverageDirectory) && - !config.watchPathIgnorePatterns.some(pattern => filePath.match(pattern)) && !isSnapshotPath(filePath) ); } diff --git a/packages/jest-cli/src/watch.js b/packages/jest-cli/src/watch.js index 412279a40a9a..2ea55110d318 100644 --- a/packages/jest-cli/src/watch.js +++ b/packages/jest-cli/src/watch.js @@ -194,7 +194,7 @@ export default function watch( hasteMapInstances.forEach((hasteMapInstance, index) => { hasteMapInstance.on('change', ({eventsQueue, hasteFS, moduleMap}) => { const validPaths = eventsQueue.filter(({filePath}) => - isValidPath(globalConfig, contexts[index].config, filePath), + isValidPath(globalConfig, filePath), ); if (validPaths.length) { diff --git a/packages/jest-runtime/src/__tests__/Runtime-statics.test.js b/packages/jest-runtime/src/__tests__/Runtime-statics.test.js new file mode 100644 index 000000000000..a4fc811dbeb1 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/Runtime-statics.test.js @@ -0,0 +1,45 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. 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. + * + */ + +import HasteMap from 'jest-haste-map'; +const Runtime = require('../'); + +jest.mock('jest-haste-map'); + +describe('Runtime statics', () => { + const projectConfig = { + cacheDirectory: '/tmp', + haste: {}, + modulePathIgnorePatterns: ['/root/ignore-1', '/root/ignore-2'], + watchPathIgnorePatterns: ['/watch-root/ignore-1'], + }; + const options = {}; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + test('Runtime.createHasteMap passes correct ignore files to HasteMap', () => { + Runtime.createHasteMap(projectConfig, options); + expect(HasteMap).toBeCalledWith( + expect.objectContaining({ + ignorePattern: /\/root\/ignore-1|\/root\/ignore-2/, + }), + ); + }); + + test('Runtime.createHasteMap passes correct ignore files to HasteMap in watch mode', () => { + Runtime.createHasteMap(projectConfig, {...options, watch: true}); + expect(HasteMap).toBeCalledWith( + expect.objectContaining({ + ignorePattern: /\/root\/ignore-1|\/root\/ignore-2|\/watch-root\/ignore-1/, + watch: true, + }), + ); + }); +}); diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 05dc03a7a35b..e09902fb1a67 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -229,6 +229,7 @@ class Runtime { ): HasteMap { const ignorePatternParts = [ ...config.modulePathIgnorePatterns, + ...(options && options.watch ? config.watchPathIgnorePatterns : []), config.cacheDirectory.startsWith(config.rootDir + path.sep) && config.cacheDirectory, ].filter(Boolean);