From 9cd00e6dca5ecfae6222b93cf04ccfd8b68626ab Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Sat, 6 Jan 2018 14:41:34 +0400 Subject: [PATCH] Allow unmatched test paths from CLI (#3882) * Allow to pass the exact test filenames * Replaced custom check with fs.existsSync(), added tests * Changed licence to MIT * Fixed tests --- .../cli-accepts-exact-filenames.test.js.snap | 29 ++++++++++++ .../cli-accepts-exact-filenames.test.js | 47 +++++++++++++++++++ .../__tests__/find_related_files.test.js | 2 +- packages/jest-cli/src/search_source.js | 17 +++++-- 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 integration_tests/__tests__/__snapshots__/cli-accepts-exact-filenames.test.js.snap create mode 100644 integration_tests/__tests__/cli-accepts-exact-filenames.test.js 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 new file mode 100644 index 000000000000..6c75b05c0a40 --- /dev/null +++ b/integration_tests/__tests__/__snapshots__/cli-accepts-exact-filenames.test.js.snap @@ -0,0 +1,29 @@ +// 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/i. +" +`; + +exports[`CLI accepts exact filenames 3`] = `""`; diff --git a/integration_tests/__tests__/cli-accepts-exact-filenames.test.js b/integration_tests/__tests__/cli-accepts-exact-filenames.test.js new file mode 100644 index 000000000000..9b5aee992b1a --- /dev/null +++ b/integration_tests/__tests__/cli-accepts-exact-filenames.test.js @@ -0,0 +1,47 @@ +/** + * 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/skip_on_windows'); +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', + ]); + const {rest, summary} = extractSummary(stderr); + expect(status).toBe(0); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); + expect(stdout).toMatchSnapshot(); +}); diff --git a/integration_tests/__tests__/find_related_files.test.js b/integration_tests/__tests__/find_related_files.test.js index 565a8c0c2e4c..bfac9bbad938 100644 --- a/integration_tests/__tests__/find_related_files.test.js +++ b/integration_tests/__tests__/find_related_files.test.js @@ -35,7 +35,7 @@ test('runs tests related to filename', () => { }); const {stdout} = runJest(DIR, ['a.js']); - expect(stdout).toMatch(/no tests found/i); + expect(stdout).toMatch(''); const {stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']); expect(stderr).toMatch('PASS __tests__/test.test.js'); diff --git a/packages/jest-cli/src/search_source.js b/packages/jest-cli/src/search_source.js index 0011498c2504..d27d3b83a603 100644 --- a/packages/jest-cli/src/search_source.js +++ b/packages/jest-cli/src/search_source.js @@ -12,6 +12,7 @@ import type {Glob, GlobalConfig, Path} from 'types/Config'; import type {Test} from 'types/TestRunner'; import type {ChangedFilesPromise} from 'types/ChangedFiles'; +import fs from 'fs'; import path from 'path'; import micromatch from 'micromatch'; import DependencyResolver from 'jest-resolve-dependencies'; @@ -207,12 +208,18 @@ 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 { - return Promise.resolve({tests: []}); + const validTestPaths = paths && paths.filter(fs.existsSync); + + 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: []}); + } } } }