Skip to content

Commit 21d79eb

Browse files
committed
fix(config): verify testMatchPatterns contain RegExp instance or string type values (#1569)
1 parent 9bc3f0d commit 21d79eb

4 files changed

Lines changed: 92 additions & 4 deletions

File tree

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

Lines changed: 27 additions & 1 deletion
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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('projectPackageJson', () => {
123123
})
124124

125125
describe('jest', () => {
126-
it('should returns correct config and go thru backports', () => {
126+
it('should return correct config and go thru backports', () => {
127127
expect(createConfigSet().jest).toMatchSnapshot()
128128
expect(backports.backportJestConfig).toHaveBeenCalledTimes(1)
129129
})
@@ -151,6 +151,37 @@ describe('jest', () => {
151151
})
152152
})
153153

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

src/config/config-set.ts

Lines changed: 13 additions & 1 deletion
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

Lines changed: 20 additions & 1 deletion
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)