From 7f85bab6fc13cdfaf6eef711e094776f98bf150b Mon Sep 17 00:00:00 2001 From: Ahn Date: Mon, 27 Apr 2020 16:23:03 +0200 Subject: [PATCH] fix(config): verify `testMatchPatterns` contain RegExp instance or string type values (#1569) --- .../__snapshots__/config-set.spec.ts.snap | 28 +++++++++++++++- src/config/config-set.spec.ts | 33 ++++++++++++++++++- src/config/config-set.ts | 14 +++++++- src/constants.ts | 21 +++++++++++- 4 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/config/__snapshots__/config-set.spec.ts.snap b/src/config/__snapshots__/config-set.spec.ts.snap index 49374247b4..55469f1a77 100644 --- a/src/config/__snapshots__/config-set.spec.ts.snap +++ b/src/config/__snapshots__/config-set.spec.ts.snap @@ -18,7 +18,7 @@ Object { } `; -exports[`jest should returns correct config and go thru backports 1`] = ` +exports[`jest should return correct config and go thru backports 1`] = ` Object { "__backported": true, "globals": Object {}, @@ -126,6 +126,32 @@ Array [ ] `; +exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 1`] = ` +Array [ + "**/__tests__/**/*.[jt]s?(x)", + "**/?(*.)+(spec|test).[jt]s?(x)", +] +`; + +exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 2`] = ` +Array [ + /\\.\\*\\\\\\.\\(spec\\|test\\)\\\\\\.\\[jt\\]sx\\?\\$/, +] +`; + +exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 3`] = ` +Array [ + "**/?(*.)+(spec|test).[tj]s?(x)", +] +`; + +exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 4`] = ` +Array [ + "**/?(*.)+(spec|test).[tj]s?(x)", + "**/?(*.)+(foo|bar).[tj]s?(x)", +] +`; + exports[`tsJest should return correct defaults 1`] = ` Object { "babelConfig": undefined, diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index a1c6a61fd5..887627d33f 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -122,7 +122,7 @@ describe('projectPackageJson', () => { }) describe('jest', () => { - it('should returns correct config and go thru backports', () => { + it('should return correct config and go thru backports', () => { expect(createConfigSet().jest).toMatchSnapshot() expect(backports.backportJestConfig).toHaveBeenCalledTimes(1) }) @@ -150,6 +150,37 @@ describe('jest', () => { }) }) +describe('testMatchPatterns', () => { + it.each([ + { + jestConfig: { + testRegex: [{}], + testMatch: [], + } as any, + }, + { + jestConfig: { + testMatch: [], + testRegex: [/.*\.(spec|test)\.[jt]sx?$/], + } as any, + }, + { + jestConfig: { + testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'], + testRegex: [], + } as any, + }, + { + jestConfig: { + testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'], + testRegex: ['**/?(*.)+(foo|bar).[tj]s?(x)'], + } as any, + }, + ])('should return an array of patterns based on testRegex and testMatch from jestConfig', config => { + expect(createConfigSet(config).testMatchPatterns).toMatchSnapshot() + }) +}) + describe('tsJest', () => { const getConfigSet = (tsJest?: TsJestGlobalOptions) => createConfigSet({ tsJestConfig: tsJest }) const getTsJest = (tsJest?: TsJestGlobalOptions) => getConfigSet(tsJest).tsJest diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 8fa1564bea..1892cf9413 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -25,6 +25,7 @@ import { import { digest as MY_DIGEST, version as MY_VERSION } from '..' import { createCompilerInstance } from '../compiler/instance' +import { DEFAULT_JEST_TEST_MATCH } from '../constants' import { internals as internalAstTransformers } from '../transformers' import { AstTransformerDesc, @@ -197,7 +198,18 @@ export class ConfigSet { */ @Memoize() get testMatchPatterns(): (string | RegExp)[] { - return [...this.jest.testMatch, ...this.jest.testRegex] + const matchablePatterns = [...this.jest.testMatch, ...this.jest.testRegex].filter(pattern => { + /** + * jest config testRegex doesn't always deliver the correct RegExp object + * See https://github.com/facebook/jest/issues/9778 + */ + return pattern instanceof RegExp || typeof pattern === 'string' + }) + if (!matchablePatterns.length) { + matchablePatterns.push(...DEFAULT_JEST_TEST_MATCH) + } + + return matchablePatterns } /** diff --git a/src/constants.ts b/src/constants.ts index 646fdc027d..10c4fcf39e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,6 +1,25 @@ +/** + * @internal + */ export const LINE_FEED = '\n' - +/** + * @internal + */ export const EXTENSION_REGEX = /\.[^.]+$/ +/** + * @internal + */ export const TS_TSX_REGEX = /\.tsx?$/ +/** + * @internal + */ export const JS_JSX_REGEX = /\.jsx?$/ +/** + * @internal + */ export const JSON_REGEX = /\.json$/i +/** + * @internal + * See https://jestjs.io/docs/en/configuration#testmatch-arraystring + */ +export const DEFAULT_JEST_TEST_MATCH = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']