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

Add testNamePattern CLI argument #1733

Merged
merged 3 commits into from
Sep 20, 2016
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
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: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we give this an alias? Maybe p or t?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe t? I would reserve p for filtering by path, because it means that in the watch mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added:

  --testNamePattern, -t           Run only tests with a name that matches the
                                  regex pattern.                        [string]

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