Skip to content

Commit

Permalink
Add testNamePattern CLI argument (#1733)
Browse files Browse the repository at this point in the history
* Add testNamePattern CLI argument

testNamePattern allows filtering tests by name with a regex

* Add -t as an alias for --testNamePattern

* Skip testNamePattern-test integration test on Windows
  • Loading branch information
fson authored and cpojer committed Sep 20, 2016
1 parent 73e513c commit f8a02cc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions integration_tests/__tests__/testNamePattern-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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 runJest = require('../runJest');
const skipOnWindows = require('skipOnWindows');

skipOnWindows.suite();

test('testNamePattern', () => {
const result = runJest.json('testNamePattern', [
'--testNamePattern', 'should match',
]);

expect(result.status).toBe(0);
expect(result.json.numTotalTests).toBe(4);
expect(result.json.numPassedTests).toBe(2);
expect(result.json.numFailedTests).toBe(0);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* 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';

test('should match 1', () => expect(1).toBe(1));
test('should match 2', () => expect(1).toBe(1));
test('should not match 1', () => expect(1).toBe(1));
test('should not match 2', () => expect(1).toBe(1));
5 changes: 5 additions & 0 deletions integration_tests/testNamePattern/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
6 changes: 6 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ const options = {
'rare.',
type: 'boolean',
},
testNamePattern: {
alias: 't',
description:
'Run only tests with a name that matches the regex pattern.',
type: 'string',
},
testPathPattern: {
description:
'A regexp pattern string that is matched against all tests ' +
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-config/src/setFromArgv.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ function setFromArgv(config, argv) {
config.setupTestFrameworkScriptFile = argv.setupTestFrameworkScriptFile;
}

if (argv.testNamePattern) {
config.testNamePattern = argv.testNamePattern;
}

if (argv.updateSnapshot) {
config.updateSnapshot = argv.updateSnapshot;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ function jasmine2(
environment.fakeTimers.useFakeTimers();
}

if (config.testNamePattern) {
const testNameRegex = new RegExp(config.testNamePattern);
env.specFilter = spec => testNameRegex.test(spec.getFullName());
}

runtime.requireModule(testPath);
env.execute();
return reporter.getResults().then(results => {
Expand Down
1 change: 1 addition & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type Config = BaseConfig & {
setupTestFrameworkScriptFile: Path,
silent: boolean,
testcheckOptions: {},
testNamePattern: string,
unmockedModulePathPatterns: Array<string>,
updateSnapshot: boolean,
usesBabelJest: boolean,
Expand Down

0 comments on commit f8a02cc

Please sign in to comment.