diff --git a/CHANGELOG.md b/CHANGELOG.md index 23742b4601ee..2f9bc1036e5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Fixes +* `[jest-cli]` Don't skip matchers for exact files ([#5582](https://github.com/facebook/jest/pull/5582)) * `[docs]` Update discord links ([#5586](https://github.com/facebook/jest/pull/5586)) * `[jest-runtime]` Align handling of testRegex on Windows between searching for tests and instrumentation checks diff --git a/integration-tests/__tests__/__snapshots__/cli-accepts-exact-filenames.test.js.snap b/integration-tests/__tests__/__snapshots__/cli-accepts-exact-filenames.test.js.snap deleted file mode 100644 index 14f0f7d5b8bc..000000000000 --- a/integration-tests/__tests__/__snapshots__/cli-accepts-exact-filenames.test.js.snap +++ /dev/null @@ -1,29 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`CLI accepts exact filenames 1`] = ` -"PASS ./bar.js - ● Console - - console.log bar.js:2 - Hey - -PASS foo/baz.js - ● Console - - console.log foo/baz.js:2 - Hey - - -" -`; - -exports[`CLI accepts exact filenames 2`] = ` -"Test Suites: 2 passed, 2 total -Tests: 2 passed, 2 total -Snapshots: 0 total -Time: <> -Ran all test suites matching /.\\\\/bar.js|.\\\\/foo\\\\/baz.js|.\\\\/foo/i. -" -`; - -exports[`CLI accepts exact filenames 3`] = `""`; diff --git a/integration-tests/__tests__/__snapshots__/cli-handles-exact-filenames.test.js.snap b/integration-tests/__tests__/__snapshots__/cli-handles-exact-filenames.test.js.snap new file mode 100644 index 000000000000..288a38cb312a --- /dev/null +++ b/integration-tests/__tests__/__snapshots__/cli-handles-exact-filenames.test.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`CLI accepts exact file names if matchers matched 1`] = ` +"PASS foo/bar.spec.js + ✓ foo + +" +`; + +exports[`CLI accepts exact file names if matchers matched 2`] = ` +"Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites matching /.\\\\/foo\\\\/bar.spec.js/i. +" +`; + +exports[`CLI accepts exact file names if matchers matched 3`] = `""`; diff --git a/integration-tests/__tests__/cli-accepts-exact-filenames.test.js b/integration-tests/__tests__/cli-accepts-exact-filenames.test.js deleted file mode 100644 index 083c6cc1b278..000000000000 --- a/integration-tests/__tests__/cli-accepts-exact-filenames.test.js +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (c) 2014-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/SkipOnWindows'); -const {extractSummary, cleanup, writeFiles} = require('../Utils'); -const runJest = require('../runJest'); - -const DIR = path.resolve(__dirname, '../cli_accepts_exact_filenames'); - -SkipOnWindows.suite(); - -beforeEach(() => cleanup(DIR)); -afterAll(() => cleanup(DIR)); - -test('CLI accepts exact filenames', () => { - writeFiles(DIR, { - 'bar.js': ` - test('bar', () => console.log('Hey')); - `, - 'foo/baz.js': ` - test('baz', () => console.log('Hey')); - `, - 'package.json': '{}', - }); - - const {stderr, stdout, status} = runJest(DIR, [ - '-i', - '--ci=false', - '--forceExit', - './bar.js', - './foo/baz.js', - './foo', - ]); - - expect(status).toBe(0); - - const {rest, summary} = extractSummary(stderr); - - expect(rest).toMatchSnapshot(); - expect(summary).toMatchSnapshot(); - expect(stdout).toMatchSnapshot(); -}); diff --git a/integration-tests/__tests__/cli-handles-exact-filenames.test.js b/integration-tests/__tests__/cli-handles-exact-filenames.test.js new file mode 100644 index 000000000000..cc50d571f41b --- /dev/null +++ b/integration-tests/__tests__/cli-handles-exact-filenames.test.js @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2014-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/SkipOnWindows'); +const {extractSummary, cleanup, writeFiles} = require('../Utils'); +const runJest = require('../runJest'); + +const DIR = path.resolve(__dirname, '../cli_accepts_exact_filenames'); + +SkipOnWindows.suite(); + +beforeEach(() => cleanup(DIR)); +afterAll(() => cleanup(DIR)); + +test('CLI accepts exact file names if matchers matched', () => { + writeFiles(DIR, { + 'foo/bar.spec.js': ` + test('foo', () => {}); + `, + 'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}), + }); + + const result = runJest(DIR, ['-i', '--forceExit', './foo/bar.spec.js']); + + expect(result.status).toBe(0); + + const {rest, summary} = extractSummary(result.stderr); + + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); + expect(result.stdout).toMatchSnapshot(); +}); + +test('CLI skips exact file names if no matchers matched', () => { + writeFiles(DIR, { + 'foo/bar.js': ` + test('foo', () => {); + `, + 'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}), + }); + + const result = runJest(DIR, ['-i', '--forceExit', './foo/bar.js']); + + expect(result.status).toBe(1); + expect(result.stdout).toMatch(/No tests found([\S\s]*)2 files checked./); + expect(result.stderr).toEqual(''); +}); diff --git a/packages/jest-cli/src/search_source.js b/packages/jest-cli/src/search_source.js index efb39ba054a0..51fff5854b67 100644 --- a/packages/jest-cli/src/search_source.js +++ b/packages/jest-cli/src/search_source.js @@ -206,41 +206,12 @@ export default class SearchSource { return Promise.resolve(this.findTestsByPaths(paths)); } else if (globalConfig.findRelatedTests && paths && paths.length) { return Promise.resolve(this.findRelatedTestsFromPattern(paths)); + } else if (globalConfig.testPathPattern != null) { + return Promise.resolve( + this.findMatchingTests(globalConfig.testPathPattern), + ); } else { - const allFiles = new Set(this._context.hasteFS.getAllFiles()); - const validTestPaths = - paths && - paths.filter(name => { - const fullName = path.resolve(name); - - try { - if (!fs.lstatSync(fullName).isFile()) { - // It exists, but it is not a file. - return false; - } - } catch (e) { - // It does not exist. - return false; - } - - // The file exists, but it is explicitly blacklisted. - if (!this._testPathCases.testPathIgnorePatterns(fullName)) { - return false; - } - - // It exists and it is a file; return true if it's in the project. - return allFiles.has(fullName); - }); - - if (validTestPaths && validTestPaths.length) { - return Promise.resolve({tests: toTests(this._context, validTestPaths)}); - } else if (globalConfig.testPathPattern != null) { - return Promise.resolve( - this.findMatchingTests(globalConfig.testPathPattern), - ); - } else { - return Promise.resolve({tests: []}); - } + return Promise.resolve({tests: []}); } } }