From 81a0a8bf3abcf43f6547fd7184b8a5ef8e6e9d90 Mon Sep 17 00:00:00 2001 From: Ryan Parker Date: Mon, 20 Jul 2020 21:30:41 -0500 Subject: [PATCH 1/4] Working implementation --- e2e/async-regenerator/yarn.lock | 2 +- e2e/babel-plugin-jest-hoist/yarn.lock | 2 +- e2e/chai-assertion-library-errors/yarn.lock | 2 +- e2e/console-winston/yarn.lock | 2 +- e2e/coverage-handlebars/yarn.lock | 2 +- e2e/coverage-remapping/yarn.lock | 4 ++-- e2e/coverage-report/yarn.lock | 2 +- e2e/coverage-transform-instrumented/yarn.lock | 2 +- e2e/expect-async-matcher/yarn.lock | 2 +- e2e/failures/yarn.lock | 2 +- e2e/global-setup/yarn.lock | 2 +- e2e/global-teardown/yarn.lock | 2 +- e2e/stack-trace-source-maps-with-coverage/yarn.lock | 4 ++-- e2e/stack-trace-source-maps/yarn.lock | 4 ++-- e2e/transform/babel-jest-manual/yarn.lock | 2 +- e2e/transform/babel-jest/yarn.lock | 2 +- e2e/transform/multiple-transformers/yarn.lock | 2 +- e2e/transform/transformer-config/yarn.lock | 2 +- e2e/typescript-coverage/yarn.lock | 4 ++-- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 4 ++-- packages/jest-jasmine2/src/index.ts | 8 ++++++-- packages/jest-runner/src/index.ts | 8 ++++++++ packages/jest-types/src/Config.ts | 1 + yarn.lock | 2 +- 24 files changed, 41 insertions(+), 28 deletions(-) diff --git a/e2e/async-regenerator/yarn.lock b/e2e/async-regenerator/yarn.lock index 236d2fa2ed51..f980026cd0d8 100644 --- a/e2e/async-regenerator/yarn.lock +++ b/e2e/async-regenerator/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/code-frame@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/babel-plugin-jest-hoist/yarn.lock b/e2e/babel-plugin-jest-hoist/yarn.lock index 4889de3d0508..03581ee192fc 100644 --- a/e2e/babel-plugin-jest-hoist/yarn.lock +++ b/e2e/babel-plugin-jest-hoist/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/code-frame@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/chai-assertion-library-errors/yarn.lock b/e2e/chai-assertion-library-errors/yarn.lock index 4080194c353c..7fb782725f06 100644 --- a/e2e/chai-assertion-library-errors/yarn.lock +++ b/e2e/chai-assertion-library-errors/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "assertion-error@npm:^1.1.0": version: 1.1.0 diff --git a/e2e/console-winston/yarn.lock b/e2e/console-winston/yarn.lock index c3f0cef31f7b..a812d12623c9 100644 --- a/e2e/console-winston/yarn.lock +++ b/e2e/console-winston/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "async@npm:^2.6.1": version: 2.6.3 diff --git a/e2e/coverage-handlebars/yarn.lock b/e2e/coverage-handlebars/yarn.lock index 58ffd42c552d..72e6260d30b5 100644 --- a/e2e/coverage-handlebars/yarn.lock +++ b/e2e/coverage-handlebars/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "commander@npm:~2.20.3": version: 2.20.3 diff --git a/e2e/coverage-remapping/yarn.lock b/e2e/coverage-remapping/yarn.lock index a81055e41cba..4d6b494151c8 100644 --- a/e2e/coverage-remapping/yarn.lock +++ b/e2e/coverage-remapping/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "root-workspace-0b6124@workspace:.": version: 0.0.0-use.local @@ -25,7 +25,7 @@ typescript@^3.7.4: "typescript@patch:typescript@^3.7.4#builtin": version: 3.8.3 - resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=64df9d" + resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=5b02a2" bin: tsc: bin/tsc tsserver: bin/tsserver diff --git a/e2e/coverage-report/yarn.lock b/e2e/coverage-report/yarn.lock index 98d14aaac0ad..68e8b9d238ef 100644 --- a/e2e/coverage-report/yarn.lock +++ b/e2e/coverage-report/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/helper-plugin-utils@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/coverage-transform-instrumented/yarn.lock b/e2e/coverage-transform-instrumented/yarn.lock index cdcd34252ff7..08e0b5c4bf16 100644 --- a/e2e/coverage-transform-instrumented/yarn.lock +++ b/e2e/coverage-transform-instrumented/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/code-frame@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/expect-async-matcher/yarn.lock b/e2e/expect-async-matcher/yarn.lock index 98d14aaac0ad..68e8b9d238ef 100644 --- a/e2e/expect-async-matcher/yarn.lock +++ b/e2e/expect-async-matcher/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/helper-plugin-utils@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/failures/yarn.lock b/e2e/failures/yarn.lock index 98d14aaac0ad..68e8b9d238ef 100644 --- a/e2e/failures/yarn.lock +++ b/e2e/failures/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/helper-plugin-utils@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/global-setup/yarn.lock b/e2e/global-setup/yarn.lock index 0a5f76b9dc1f..d8771ec6bbdc 100644 --- a/e2e/global-setup/yarn.lock +++ b/e2e/global-setup/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/code-frame@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/global-teardown/yarn.lock b/e2e/global-teardown/yarn.lock index 0a5f76b9dc1f..d8771ec6bbdc 100644 --- a/e2e/global-teardown/yarn.lock +++ b/e2e/global-teardown/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/code-frame@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/stack-trace-source-maps-with-coverage/yarn.lock b/e2e/stack-trace-source-maps-with-coverage/yarn.lock index a81055e41cba..4d6b494151c8 100644 --- a/e2e/stack-trace-source-maps-with-coverage/yarn.lock +++ b/e2e/stack-trace-source-maps-with-coverage/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "root-workspace-0b6124@workspace:.": version: 0.0.0-use.local @@ -25,7 +25,7 @@ typescript@^3.7.4: "typescript@patch:typescript@^3.7.4#builtin": version: 3.8.3 - resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=64df9d" + resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=5b02a2" bin: tsc: bin/tsc tsserver: bin/tsserver diff --git a/e2e/stack-trace-source-maps/yarn.lock b/e2e/stack-trace-source-maps/yarn.lock index a81055e41cba..4d6b494151c8 100644 --- a/e2e/stack-trace-source-maps/yarn.lock +++ b/e2e/stack-trace-source-maps/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "root-workspace-0b6124@workspace:.": version: 0.0.0-use.local @@ -25,7 +25,7 @@ typescript@^3.7.4: "typescript@patch:typescript@^3.7.4#builtin": version: 3.8.3 - resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=64df9d" + resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=5b02a2" bin: tsc: bin/tsc tsserver: bin/tsserver diff --git a/e2e/transform/babel-jest-manual/yarn.lock b/e2e/transform/babel-jest-manual/yarn.lock index 98d14aaac0ad..68e8b9d238ef 100644 --- a/e2e/transform/babel-jest-manual/yarn.lock +++ b/e2e/transform/babel-jest-manual/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/helper-plugin-utils@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/transform/babel-jest/yarn.lock b/e2e/transform/babel-jest/yarn.lock index 98d14aaac0ad..68e8b9d238ef 100644 --- a/e2e/transform/babel-jest/yarn.lock +++ b/e2e/transform/babel-jest/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/helper-plugin-utils@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/transform/multiple-transformers/yarn.lock b/e2e/transform/multiple-transformers/yarn.lock index 4d43e9f5bb86..c94e93cfc1d9 100644 --- a/e2e/transform/multiple-transformers/yarn.lock +++ b/e2e/transform/multiple-transformers/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/code-frame@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/transform/transformer-config/yarn.lock b/e2e/transform/transformer-config/yarn.lock index 6ee10e7837ca..dd41475c1994 100644 --- a/e2e/transform/transformer-config/yarn.lock +++ b/e2e/transform/transformer-config/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "@babel/helper-plugin-utils@npm:^7.0.0, @babel/helper-plugin-utils@npm:^7.8.3": version: 7.8.3 diff --git a/e2e/typescript-coverage/yarn.lock b/e2e/typescript-coverage/yarn.lock index b1e167d2dcf2..29c836d1eb1b 100644 --- a/e2e/typescript-coverage/yarn.lock +++ b/e2e/typescript-coverage/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 4 + cacheKey: 6 "root-workspace-0b6124@workspace:.": version: 0.0.0-use.local @@ -25,7 +25,7 @@ typescript@^3.3.1: "typescript@patch:typescript@^3.3.1#builtin": version: 3.8.3 - resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=64df9d" + resolution: "typescript@patch:typescript@npm%3A3.8.3#builtin::version=3.8.3&hash=5b02a2" bin: tsc: bin/tsc tsserver: bin/tsserver diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index eb0595455c4e..ba19f8615b83 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -77,7 +77,7 @@ export const initialize = async ({ timeout?: number, ) => { // For concurrent tests we first run the function that returns promise, and then register a - // nomral test that will be waiting on the returned promise (when we start the test, the promise + // normal test that will be waiting on the returned promise (when we start the test, the promise // will already be in the process of execution). // Unfortunately at this stage there's no way to know if there are any `.only` tests in the suite // that will result in this test to be skipped, so we'll be executing the promise function anyway, @@ -110,7 +110,7 @@ export const initialize = async ({ await dispatch({ name: 'setup', parentProcess, - testNamePattern: globalConfig.testNamePattern, + testNamePattern: config.testNamePattern ?? globalConfig.testNamePattern, }); if (config.testLocationInResults) { diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 7c081b7846b1..5520dbb7b8b1 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -175,8 +175,12 @@ async function jasmine2( globalConfig.enabledTestsMap[spec.result.testPath]; return (suiteMap && suiteMap[spec.result.fullName]) || false; }; - } else if (globalConfig.testNamePattern) { - const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i'); + } + + const testNamePattern = config.testNamePattern ?? globalConfig.testNamePattern + + if (testNamePattern) { + const testNameRegex = new RegExp(testNamePattern, 'i'); env.specFilter = (spec: Spec) => testNameRegex.test(spec.getFullName()); } diff --git a/packages/jest-runner/src/index.ts b/packages/jest-runner/src/index.ts index 0f1d069c6af0..d6d03a91df66 100644 --- a/packages/jest-runner/src/index.ts +++ b/packages/jest-runner/src/index.ts @@ -54,6 +54,14 @@ class TestRunner { this._context = context || {}; } + get testNamePattern() { + return this._globalConfig.testNamePattern; + } + + set testNamePattern(pattern: string | undefined) { + this._globalConfig = { ...this._globalConfig, testNamePattern: pattern}; + } + async runTests( tests: Array, watcher: JestTestWatcher, diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 76a7b33a8f77..97fb640e5f01 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -337,6 +337,7 @@ export type ProjectConfig = { testEnvironment: string; testEnvironmentOptions: Record; testMatch: Array; + testNamePattern?: string; testLocationInResults: boolean; testPathIgnorePatterns: Array; testRegex: Array; diff --git a/yarn.lock b/yarn.lock index 42b35e08e785..25e98d817b6d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 5 + cacheKey: 6 "@angular/common@npm:^10.0.2": version: 10.0.2 From e268d0922ba43c7b6183fbc81f611d821ec48930 Mon Sep 17 00:00:00 2001 From: Ryan Parker Date: Tue, 21 Jul 2020 00:09:19 -0500 Subject: [PATCH 2/4] Updated documentation --- CHANGELOG.md | 1 + packages/jest-runner/README.md | 79 +++++++++++++++++++ .../src/__tests__/testRunner.test.ts | 2 +- .../version-26.0/Configuration.md | 8 ++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 packages/jest-runner/README.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bc2c8d7bb91..8b6b957c90a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-runner, jest-circus, jest-jasmine2, jest-types]` Added support for `testNamePattern` in the `Project.Config`. This enables custom Jest runners to specify which test cases are ran in each test file (by setting the `testNamePattern` config on each `JestTest.context.config`). The `GlobalConfig.testNamePattern` was also exposed to custom Jest runners. - `[pretty-format]` Added support for serializing custom elements (web components) ([#10217](https://github.com/facebook/jest/pull/10237)) ### Fixes diff --git a/packages/jest-runner/README.md b/packages/jest-runner/README.md new file mode 100644 index 000000000000..5ff9b680cfea --- /dev/null +++ b/packages/jest-runner/README.md @@ -0,0 +1,79 @@ +# jest-runner + +`jest-runner` is a package that can be used to create custom Jest runners. Runners are used to control which tests run. + +```js +const TestRunner = require('jest-runner'); + +class myCustomJestRunner extends TestRunner { + constructor(globalConfig, context) { + super(globalConfig, context); + } + + async runTests(tests, watcher, onStart, onResult, onFailure, options) { + const filteredTests = tests.filter(test => test.path.includes('e2e')); + + super(filteredTests, watcher, onStart, onResult, onFailure, options); + } +} +``` + +`jest-runner` can: + +- Intercept test collections before they are ran. +- Modify the Jest-Config per test file. This includes controlling the `testNamePattern` of each test file. + +## Installation + +```sh +# with yarn +$ yarn add jest-runner +# with npm +$ npm install jest-runner +``` + + diff --git a/packages/jest-runner/src/__tests__/testRunner.test.ts b/packages/jest-runner/src/__tests__/testRunner.test.ts index d602238a1a3d..3b57f38c0bb9 100644 --- a/packages/jest-runner/src/__tests__/testRunner.test.ts +++ b/packages/jest-runner/src/__tests__/testRunner.test.ts @@ -1,4 +1,4 @@ -/** + /** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * * This source code is licensed under the MIT license found in the diff --git a/website/versioned_docs/version-26.0/Configuration.md b/website/versioned_docs/version-26.0/Configuration.md index ea03c5b909aa..d28c38ed2e28 100644 --- a/website/versioned_docs/version-26.0/Configuration.md +++ b/website/versioned_docs/version-26.0/Configuration.md @@ -978,6 +978,14 @@ See the [micromatch](https://github.com/jonschlinkert/micromatch) package for de See also [`testRegex` [string | array\]](#testregex-string--arraystring), but note that you cannot specify both options. +### `testNamePattern` [string] + +Default: `undefined` + +Run only tests with a name that matches the regex. For example, suppose you want to run only tests related to authorization which will have names like "GET /api/posts with auth", then you can use jest -t=auth. + +Note: The regex is matched against the full name, which is a combination of the test name and all its surrounding describe blocks. + ### `testPathIgnorePatterns` [array\] Default: `["/node_modules/"]` From b162e38c4522f391fdcb4f8727a0748c03d0135e Mon Sep 17 00:00:00 2001 From: Ryan Parker Date: Tue, 21 Jul 2020 01:14:50 -0500 Subject: [PATCH 3/4] Ran prettier on changed files --- packages/jest-jasmine2/src/index.ts | 3 ++- packages/jest-runner/src/__tests__/testRunner.test.ts | 2 +- packages/jest-runner/src/index.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 5520dbb7b8b1..9a1bd811fec3 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -177,7 +177,8 @@ async function jasmine2( }; } - const testNamePattern = config.testNamePattern ?? globalConfig.testNamePattern + const testNamePattern = + config.testNamePattern ?? globalConfig.testNamePattern; if (testNamePattern) { const testNameRegex = new RegExp(testNamePattern, 'i'); diff --git a/packages/jest-runner/src/__tests__/testRunner.test.ts b/packages/jest-runner/src/__tests__/testRunner.test.ts index 3b57f38c0bb9..d602238a1a3d 100644 --- a/packages/jest-runner/src/__tests__/testRunner.test.ts +++ b/packages/jest-runner/src/__tests__/testRunner.test.ts @@ -1,4 +1,4 @@ - /** +/** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * * This source code is licensed under the MIT license found in the diff --git a/packages/jest-runner/src/index.ts b/packages/jest-runner/src/index.ts index d6d03a91df66..6d87e3f1fdb6 100644 --- a/packages/jest-runner/src/index.ts +++ b/packages/jest-runner/src/index.ts @@ -59,7 +59,7 @@ class TestRunner { } set testNamePattern(pattern: string | undefined) { - this._globalConfig = { ...this._globalConfig, testNamePattern: pattern}; + this._globalConfig = {...this._globalConfig, testNamePattern: pattern}; } async runTests( From ef5c49fca3f4deb2bbb12df37399503a4b7852a9 Mon Sep 17 00:00:00 2001 From: Ryan Parker Date: Mon, 3 Aug 2020 11:36:41 -0500 Subject: [PATCH 4/4] removed testNamePattern get/set from jest runner --- packages/jest-runner/src/index.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/jest-runner/src/index.ts b/packages/jest-runner/src/index.ts index c7b73c0150e2..abdba02c39aa 100644 --- a/packages/jest-runner/src/index.ts +++ b/packages/jest-runner/src/index.ts @@ -62,14 +62,6 @@ class TestRunner { this._context = context || {}; } - get testNamePattern() { - return this._globalConfig.testNamePattern; - } - - set testNamePattern(pattern: string | undefined) { - this._globalConfig = {...this._globalConfig, testNamePattern: pattern}; - } - async runTests( tests: Array, watcher: JestTestWatcher,