diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ab5e414fcb2..83bc8e369195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Fixes +* `[jest-cli]` Check if the file belongs to the checked project before adding it + to the list ([#5335](https://github.com/facebook/jest/pull/5335)) * `[jest-cli]` Fix `EISDIR` when a directory is passed as an argument to `jest`. ([#5317](https://github.com/facebook/jest/pull/5317)) * `[jest-config]` Added restoreMocks config option. diff --git a/integration-tests/__tests__/__snapshots__/execute-tests-once-in-mpr.js.snap b/integration-tests/__tests__/__snapshots__/execute-tests-once-in-mpr.js.snap new file mode 100644 index 000000000000..90f90fe3c30b --- /dev/null +++ b/integration-tests/__tests__/__snapshots__/execute-tests-once-in-mpr.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Tests are executed only once even in an MPR 1`] = ` +"PASS foo/folder/my-test-bar.js + ✓ bar + +" +`; diff --git a/integration-tests/__tests__/cli-accepts-exact-filenames.test.js b/integration-tests/__tests__/cli-accepts-exact-filenames.test.js index 0ef90d7da4fd..be774a889848 100644 --- a/integration-tests/__tests__/cli-accepts-exact-filenames.test.js +++ b/integration-tests/__tests__/cli-accepts-exact-filenames.test.js @@ -40,8 +40,11 @@ test('CLI accepts exact filenames', () => { './foo/baz.js', './foo', ]); - const {rest, summary} = extractSummary(stderr); + expect(status).toBe(0); + + const {rest, summary} = extractSummary(stderr); + expect(rest).toMatchSnapshot(); expect(summary).toMatchSnapshot(); expect(stdout).toMatchSnapshot(); diff --git a/integration-tests/__tests__/execute-tests-once-in-mpr.js b/integration-tests/__tests__/execute-tests-once-in-mpr.js new file mode 100644 index 000000000000..dd66aeb412ac --- /dev/null +++ b/integration-tests/__tests__/execute-tests-once-in-mpr.js @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2017-present, Facebook, Inc. 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. + * + * @flow + */ + +'use strict'; + +const path = require('path'); +const skipOnWindows = require('../../scripts/skip_on_windows'); +const {extractSummary, cleanup, writeFiles} = require('../utils'); +const runJest = require('../runJest'); + +const DIR = path.resolve(__dirname, '../execute-tests-once-in-mpr'); + +skipOnWindows.suite(); + +beforeEach(() => cleanup(DIR)); +afterAll(() => cleanup(DIR)); + +test('Tests are executed only once even in an MPR', () => { + // Make a global config that ignores all sub-projects. + const config = { + jest: { + projects: ['/foo/*/'], + testPathIgnorePatterns: ['/foo/'], + }, + }; + + // Make a child config with a special regexp to ensure we execute the tests. + const childConfig = { + jest: { + testRegex: /my-test-.*\.js/.source, + }, + }; + + /* eslint-disable sort-keys */ + writeFiles(DIR, { + 'foo/folder/my-test-bar.js': `test('bar', () => console.log('Bar!'));`, + 'foo/folder/package.json': JSON.stringify(childConfig, null, 2), + + 'foo/directory/my-test-baz.js': `test('baz', () => console.log('Baz!'));`, + 'foo/directory/package.json': JSON.stringify(childConfig, null, 2), + + 'foo/whatever/my-test-qux.js': `test('qux', () => console.log('Qux!'));`, + 'foo/whatever/package.json': JSON.stringify(childConfig, null, 2), + + 'package.json': JSON.stringify(config, null, 2), + }); + /* eslint-enable sort-keys */ + + const {stderr, status} = runJest(DIR, ['foo/folder/my-test-bar.js']); + + expect(status).toBe(0); + + const {rest, summary} = extractSummary(stderr); + + // We have only one test passed, so total should equal to one, despite we have + // three projects. + expect(rest).toMatchSnapshot(); + expect(summary).toMatch(/1 total/); +}); diff --git a/packages/jest-cli/src/search_source.js b/packages/jest-cli/src/search_source.js index b49fbae44ee1..f56eebc5c583 100644 --- a/packages/jest-cli/src/search_source.js +++ b/packages/jest-cli/src/search_source.js @@ -209,14 +209,22 @@ export default class SearchSource { } else if (globalConfig.findRelatedTests && paths && paths.length) { return Promise.resolve(this.findRelatedTestsFromPattern(paths)); } else { + const allFiles = new Set(this._context.hasteFS.getAllFiles()); const validTestPaths = paths && paths.filter(name => { try { - return fs.lstatSync(name).isFile(); + if (!fs.lstatSync(name).isFile()) { + // It exists, but it is not a file; return false. + return false; + } } catch (e) { + // It does not exist; return false. return false; } + + // It exists and it is a file; return true if it's in the project. + return allFiles.has(path.resolve(name)); }); if (validTestPaths && validTestPaths.length) {