Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unmatched test paths from CLI #3882

Merged
merged 4 commits into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: []});
}
}
}
}