Skip to content

Commit

Permalink
fix: Pass watchPathIgnorePatterns to Haste instance (jestjs#7585)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and captain-yossarian committed Jul 18, 2019
1 parent dfbc36c commit 33fba2c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 3 additions & 34 deletions packages/jest-cli/src/lib/__tests__/is_valid_path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -75,26 +56,14 @@ 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);

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);
});
4 changes: 1 addition & 3 deletions packages/jest-cli/src/lib/is_valid_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
2 changes: 1 addition & 1 deletion packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions packages/jest-runtime/src/__tests__/Runtime-statics.test.js
Original file line number Diff line number Diff line change
@@ -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,
}),
);
});
});
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 33fba2c

Please sign in to comment.