Skip to content

Commit

Permalink
enhance test pattern matching, support testRegex
Browse files Browse the repository at this point in the history
Also use all files in __tests__ by default, as jest
  • Loading branch information
ChALkeR committed Jul 14, 2024
1 parent fc57c24 commit 3b906dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
29 changes: 24 additions & 5 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import glob from 'fast-glob'

const bindir = dirname(fileURLToPath(import.meta.url))

const DEFAULT_PATTERNS = ['**/?(*.)+(spec|test).?([cm])[jt]s?(x)']
const EXTS = `.?([cm])[jt]s?(x)` // we differt from jest, allowing [cm] before everything
const DEFAULT_PATTERNS = [`**/__tests__/**/*${EXTS}`, `**/?(*.)+(spec|test)${EXTS}`]

function versionCheck() {
const [major, minor, patch] = process.versions.node.split('.').map(Number)
Expand Down Expand Up @@ -162,6 +163,7 @@ if (options.babel) {
}

const ignore = ['**/node_modules']
let filter
if (process.env.EXODUS_TEST_IGNORE) {
// fast-glob treats negative ignore patterns exactly the same as positive, let's not cause a confusion
assert(!process.env.EXODUS_TEST_IGNORE.startsWith('!'), 'Ignore pattern should not be negative')
Expand All @@ -172,7 +174,6 @@ if (process.env.EXODUS_TEST_IGNORE) {
if (options.jest) {
const { loadJestConfig } = await import('../src/jest.config.js')
const config = await loadJestConfig(process.cwd())
ignore.push(...config.testPathIgnorePatterns)
if (major >= 20 || (major === 18 && minor >= 18)) {
args.push('--import', resolve(bindir, 'jest.js'))
} else {
Expand All @@ -189,12 +190,30 @@ if (options.jest) {
})
}

const jestPatterns = Array.isArray(config.testMatch) ? config.testMatch : [config.testMatch]
if (patterns.length === 0) patterns.push(...jestPatterns) // no patterns specified via argv
if (patterns.length > 0) {
// skip, we already have patterns via argv
} else if (config.testRegex) {
assert(typeof config.testRegex === 'string', `config.testRegex should be a string`)
assert(!config.testMatch, 'config.testRegex can not be used together with config.testMatch')
patterns.push('**/*')
} else if (config.testMatch) {
patterns.push(...(Array.isArray(config.testMatch) ? config.testMatch : [config.testMatch]))
}

const testRegex = config.testRegex ? new RegExp(config.testRegex, 'u') : null
const ignoreRegexes = config.testPathIgnorePatterns.map((x) => new RegExp(x, 'u'))
if (testRegex || ignoreRegexes.length > 0) {
filter = (x) => {
const resolved = `<rootDir>/${x}` // don't actually include cwd, that should be irrelevant
if (testRegex && !testRegex.test(resolved)) return false
return !ignoreRegexes.some((r) => r.test(resolved))
}
}
}

if (patterns.length === 0) patterns.push(...DEFAULT_PATTERNS) // defaults
const allfiles = await glob(patterns, { ignore })
const globbed = await glob(patterns, { ignore })
const allfiles = filter ? globbed.filter(filter) : globbed

if (allfiles.length === 0) {
if (options.passWithNoTests) {
Expand Down
3 changes: 1 addition & 2 deletions src/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ async function getJestConfig(dir) {
const normalizeJestConfig = (config) => ({
testEnvironment: 'node',
testTimeout: 5000,
testMatch: ['**/__tests__/**/*.?([cm])[jt]s?(x)', '**/?(*.)+(spec|test).?([cm])[jt]s?(x)'],
testPathIgnorePatterns: [],
snapshotSerializers: [],
injectGlobals: true,
Expand Down Expand Up @@ -93,7 +92,7 @@ function verifyJestConfig(c) {
assert(!c.preset, 'Jest config.preset is not supported')

// TODO
const TODO = ['globalSetup', 'globalTeardown', 'randomize', 'projects', 'roots', 'testRegex']
const TODO = ['globalSetup', 'globalTeardown', 'randomize', 'projects', 'roots']
TODO.push('resolver', 'unmockedModulePathPatterns', 'watchPathIgnorePatterns', 'snapshotResolver')
for (const key of TODO) assert.equal(c[key], undefined, `Jest config.${key} is not supported yet`)
}
Expand Down

0 comments on commit 3b906dd

Please sign in to comment.