From cf2906ab44c09d9cf41f4252f27a13db0787711f Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Tue, 20 Jun 2017 13:02:31 +0400 Subject: [PATCH 1/4] Allow to pass the exact test filenames --- packages/jest-cli/src/search_source.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/jest-cli/src/search_source.js b/packages/jest-cli/src/search_source.js index 0011498c2504..b4b8b37fd02a 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'; @@ -62,6 +63,15 @@ const toTests = (context, tests) => path, })); +const fileExists = (filePath: string) => { + try { + fs.accessSync(filePath, fs.F_OK); + return true; + } catch (e) { + return false; + } +}; + export default class SearchSource { _context: Context; _rootPattern: RegExp; @@ -207,12 +217,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(fileExists); + + 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: []}); + } } } } From 3c9e2a046ad68fd68fba39122a4d550945b67e61 Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Sat, 24 Jun 2017 18:17:55 +0400 Subject: [PATCH 2/4] Replaced custom check with fs.existsSync(), added tests --- .../cli-accepts-exact-filenames.test.js.snap | 29 ++++++++++++ .../cli-accepts-exact-filenames.test.js | 46 +++++++++++++++++++ packages/jest-cli/src/search_source.js | 11 +---- 3 files changed, 76 insertions(+), 10 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..724c786dc22c --- /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/. +" +`; + +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..eddf44fc5df5 --- /dev/null +++ b/integration_tests/__tests__/cli-accepts-exact-filenames.test.js @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +const path = require('path'); +const skipOnWindows = require('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', + ]); + const {rest, summary} = extractSummary(stderr); + expect(status).toBe(0); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); + expect(stdout).toMatchSnapshot(); +}); diff --git a/packages/jest-cli/src/search_source.js b/packages/jest-cli/src/search_source.js index b4b8b37fd02a..d27d3b83a603 100644 --- a/packages/jest-cli/src/search_source.js +++ b/packages/jest-cli/src/search_source.js @@ -63,15 +63,6 @@ const toTests = (context, tests) => path, })); -const fileExists = (filePath: string) => { - try { - fs.accessSync(filePath, fs.F_OK); - return true; - } catch (e) { - return false; - } -}; - export default class SearchSource { _context: Context; _rootPattern: RegExp; @@ -218,7 +209,7 @@ export default class SearchSource { } else if (globalConfig.findRelatedTests && paths && paths.length) { return Promise.resolve(this.findRelatedTestsFromPattern(paths)); } else { - const validTestPaths = paths && paths.filter(fileExists); + const validTestPaths = paths && paths.filter(fs.existsSync); if (validTestPaths && validTestPaths.length) { return Promise.resolve({tests: toTests(this._context, validTestPaths)}); From 2a360b2d9a2960474424d825a894b1a218323d48 Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Fri, 15 Dec 2017 22:17:08 +0400 Subject: [PATCH 3/4] Changed licence to MIT --- .../__tests__/cli-accepts-exact-filenames.test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/integration_tests/__tests__/cli-accepts-exact-filenames.test.js b/integration_tests/__tests__/cli-accepts-exact-filenames.test.js index eddf44fc5df5..f20d73b39c9f 100644 --- a/integration_tests/__tests__/cli-accepts-exact-filenames.test.js +++ b/integration_tests/__tests__/cli-accepts-exact-filenames.test.js @@ -1,9 +1,8 @@ /** * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. */ 'use strict'; From 04a5ba0b45cd765de6a80a83ce733a66e29db939 Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Sat, 16 Dec 2017 13:25:35 +0400 Subject: [PATCH 4/4] Fixed tests --- .../__snapshots__/cli-accepts-exact-filenames.test.js.snap | 6 +++--- .../__tests__/cli-accepts-exact-filenames.test.js | 4 +++- integration_tests/__tests__/find_related_files.test.js | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) 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 index 724c786dc22c..6c75b05c0a40 100644 --- a/integration_tests/__tests__/__snapshots__/cli-accepts-exact-filenames.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/cli-accepts-exact-filenames.test.js.snap @@ -1,13 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`CLI accepts exact filenames 1`] = ` -" PASS ./bar.js +"PASS ./bar.js ● Console console.log bar.js:2 Hey - PASS foo/baz.js +PASS foo/baz.js ● Console console.log foo/baz.js:2 @@ -22,7 +22,7 @@ exports[`CLI accepts exact filenames 2`] = ` Tests: 2 passed, 2 total Snapshots: 0 total Time: <> -Ran all test suites matching /./bar.js|./foo/baz.js/. +Ran all test suites matching /.\\\\/bar.js|.\\\\/foo\\\\/baz.js/i. " `; diff --git a/integration_tests/__tests__/cli-accepts-exact-filenames.test.js b/integration_tests/__tests__/cli-accepts-exact-filenames.test.js index f20d73b39c9f..9b5aee992b1a 100644 --- a/integration_tests/__tests__/cli-accepts-exact-filenames.test.js +++ b/integration_tests/__tests__/cli-accepts-exact-filenames.test.js @@ -3,12 +3,14 @@ * * 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('skipOnWindows'); +const skipOnWindows = require('../../scripts/skip_on_windows'); const {extractSummary, cleanup, writeFiles} = require('../utils'); const runJest = require('../runJest'); 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');