Skip to content

Commit

Permalink
fix(config): verify testMatchPatterns contain RegExp instance or st…
Browse files Browse the repository at this point in the history
…ring type values (#1569)
  • Loading branch information
ahnpnl committed Apr 27, 2020
1 parent 78744a5 commit 7f85bab
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/config/__snapshots__/config-set.spec.ts.snap
Expand Up @@ -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 {},
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 32 additions & 1 deletion src/config/config-set.spec.ts
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/config/config-set.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

/**
Expand Down
21 changes: 20 additions & 1 deletion 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)']

0 comments on commit 7f85bab

Please sign in to comment.