Skip to content

Commit

Permalink
Allow unmatched test paths from CLI (#3882)
Browse files Browse the repository at this point in the history
* Allow to pass the exact test filenames

* Replaced custom check with fs.existsSync(), added tests

* Changed licence to MIT

* Fixed tests
  • Loading branch information
valerybugakov authored and cpojer committed Jan 6, 2018
1 parent ffec104 commit 9cd00e6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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: <<REPLACED>>
Ran all test suites matching /.\\\\/bar.js|.\\\\/foo\\\\/baz.js/i.
"
`;
exports[`CLI accepts exact filenames 3`] = `""`;
47 changes: 47 additions & 0 deletions integration_tests/__tests__/cli-accepts-exact-filenames.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
2 changes: 1 addition & 1 deletion integration_tests/__tests__/find_related_files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 12 additions & 5 deletions packages/jest-cli/src/search_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: []});
}
}
}
}

0 comments on commit 9cd00e6

Please sign in to comment.