Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): verify testMatchPatterns contain RegExp instance or string #1569

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/config/__snapshots__/config-set.spec.ts.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)']