Skip to content
Permalink
Browse files

fix(config): verify `testMatchPatterns` contain RegExp instance or st…

…ring type values (#1569)
  • Loading branch information
ahnpnl committed Apr 27, 2020
1 parent 78744a5 commit 7f85bab6fc13cdfaf6eef711e094776f98bf150b
Showing with 92 additions and 4 deletions.
  1. +27 −1 src/config/__snapshots__/config-set.spec.ts.snap
  2. +32 −1 src/config/config-set.spec.ts
  3. +13 −1 src/config/config-set.ts
  4. +20 −1 src/constants.ts
@@ -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,
@@ -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
@@ -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
}

/**
@@ -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)']

0 comments on commit 7f85bab

Please sign in to comment.
You can’t perform that action at this time.