Skip to content

Commit 7f85bab

Browse files
authoredApr 27, 2020
fix(config): verify testMatchPatterns contain RegExp instance or string type values (#1569)
1 parent 78744a5 commit 7f85bab

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed
 

‎src/config/__snapshots__/config-set.spec.ts.snap

+27-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Object {
1818
}
1919
`;
2020

21-
exports[`jest should returns correct config and go thru backports 1`] = `
21+
exports[`jest should return correct config and go thru backports 1`] = `
2222
Object {
2323
"__backported": true,
2424
"globals": Object {},
@@ -126,6 +126,32 @@ Array [
126126
]
127127
`;
128128

129+
exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 1`] = `
130+
Array [
131+
"**/__tests__/**/*.[jt]s?(x)",
132+
"**/?(*.)+(spec|test).[jt]s?(x)",
133+
]
134+
`;
135+
136+
exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 2`] = `
137+
Array [
138+
/\\.\\*\\\\\\.\\(spec\\|test\\)\\\\\\.\\[jt\\]sx\\?\\$/,
139+
]
140+
`;
141+
142+
exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 3`] = `
143+
Array [
144+
"**/?(*.)+(spec|test).[tj]s?(x)",
145+
]
146+
`;
147+
148+
exports[`testMatchPatterns should return an array of patterns based on testRegex and testMatch from jestConfig 4`] = `
149+
Array [
150+
"**/?(*.)+(spec|test).[tj]s?(x)",
151+
"**/?(*.)+(foo|bar).[tj]s?(x)",
152+
]
153+
`;
154+
129155
exports[`tsJest should return correct defaults 1`] = `
130156
Object {
131157
"babelConfig": undefined,

‎src/config/config-set.spec.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('projectPackageJson', () => {
122122
})
123123

124124
describe('jest', () => {
125-
it('should returns correct config and go thru backports', () => {
125+
it('should return correct config and go thru backports', () => {
126126
expect(createConfigSet().jest).toMatchSnapshot()
127127
expect(backports.backportJestConfig).toHaveBeenCalledTimes(1)
128128
})
@@ -150,6 +150,37 @@ describe('jest', () => {
150150
})
151151
})
152152

153+
describe('testMatchPatterns', () => {
154+
it.each([
155+
{
156+
jestConfig: {
157+
testRegex: [{}],
158+
testMatch: [],
159+
} as any,
160+
},
161+
{
162+
jestConfig: {
163+
testMatch: [],
164+
testRegex: [/.*\.(spec|test)\.[jt]sx?$/],
165+
} as any,
166+
},
167+
{
168+
jestConfig: {
169+
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'],
170+
testRegex: [],
171+
} as any,
172+
},
173+
{
174+
jestConfig: {
175+
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'],
176+
testRegex: ['**/?(*.)+(foo|bar).[tj]s?(x)'],
177+
} as any,
178+
},
179+
])('should return an array of patterns based on testRegex and testMatch from jestConfig', config => {
180+
expect(createConfigSet(config).testMatchPatterns).toMatchSnapshot()
181+
})
182+
})
183+
153184
describe('tsJest', () => {
154185
const getConfigSet = (tsJest?: TsJestGlobalOptions) => createConfigSet({ tsJestConfig: tsJest })
155186
const getTsJest = (tsJest?: TsJestGlobalOptions) => getConfigSet(tsJest).tsJest

‎src/config/config-set.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525

2626
import { digest as MY_DIGEST, version as MY_VERSION } from '..'
2727
import { createCompilerInstance } from '../compiler/instance'
28+
import { DEFAULT_JEST_TEST_MATCH } from '../constants'
2829
import { internals as internalAstTransformers } from '../transformers'
2930
import {
3031
AstTransformerDesc,
@@ -197,7 +198,18 @@ export class ConfigSet {
197198
*/
198199
@Memoize()
199200
get testMatchPatterns(): (string | RegExp)[] {
200-
return [...this.jest.testMatch, ...this.jest.testRegex]
201+
const matchablePatterns = [...this.jest.testMatch, ...this.jest.testRegex].filter(pattern => {
202+
/**
203+
* jest config testRegex doesn't always deliver the correct RegExp object
204+
* See https://github.com/facebook/jest/issues/9778
205+
*/
206+
return pattern instanceof RegExp || typeof pattern === 'string'
207+
})
208+
if (!matchablePatterns.length) {
209+
matchablePatterns.push(...DEFAULT_JEST_TEST_MATCH)
210+
}
211+
212+
return matchablePatterns
201213
}
202214

203215
/**

‎src/constants.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
/**
2+
* @internal
3+
*/
14
export const LINE_FEED = '\n'
2-
5+
/**
6+
* @internal
7+
*/
38
export const EXTENSION_REGEX = /\.[^.]+$/
9+
/**
10+
* @internal
11+
*/
412
export const TS_TSX_REGEX = /\.tsx?$/
13+
/**
14+
* @internal
15+
*/
516
export const JS_JSX_REGEX = /\.jsx?$/
17+
/**
18+
* @internal
19+
*/
620
export const JSON_REGEX = /\.json$/i
21+
/**
22+
* @internal
23+
* See https://jestjs.io/docs/en/configuration#testmatch-arraystring
24+
*/
25+
export const DEFAULT_JEST_TEST_MATCH = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']

0 commit comments

Comments
 (0)
Please sign in to comment.