Skip to content

Commit

Permalink
fix jest config resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Jul 14, 2024
1 parent a21001e commit c876ec6
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import path from 'node:path'
import { createRequire } from 'node:module'
import { specialEnvironments } from './jest.environment.js'
Expand All @@ -10,36 +11,39 @@ const baseDir = files.length === 1 ? path.dirname(path.resolve(files[0])) : unde
async function getJestConfig(dir) {
if (!dir) return

try {
const pkg = JSON.parse(await readFile(path.resolve(dir, 'package.json'), 'utf8'))
const configPath = (ext) => path.resolve(dir, `jest.config.${ext}`)

// Only if package.json is found
let dynamic
for (const type of ['mjs', 'cjs', 'js']) {
try {
const { default: config } = await import(path.resolve(dir, `jest.config.${type}`))
dynamic = config
break
} catch (e) {
if (e.code !== 'ERR_MODULE_NOT_FOUND') throw e
}
}
assert(!existsSync(configPath('ts')), 'jest.config.ts is not supported yet with .ts extension')

if (dynamic === undefined) {
try {
dynamic = JSON.parse(await readFile(path.resolve(dir, 'jest.config.json'), 'utf8'))
} catch (e) {
if (e.code !== 'ENOENT') throw e
const configs = []
for (const type of ['js', 'ts', 'mjs', 'cjs', 'json']) {
try {
if (type === 'json') {
configs.push(JSON.parse(await readFile(configPath('json'), 'utf8')))
} else {
const { default: config } = await import(configPath(type))
configs.push(config)
}
} catch (e) {
if (!['ERR_MODULE_NOT_FOUND', 'ENOENT'].includes(e.code)) throw e
}
}

try {
const pkg = JSON.parse(await readFile(path.resolve(dir, 'package.json'), 'utf8'))
assert(typeof pkg.jest !== 'string', 'String package.json["jest"] values are not supported yet')
if (pkg.jest) configs.push(pkg.jest)
} catch (e) {
if (e.code !== 'ENOENT') throw e
}

// We don't deep merge (yet?)
const conf = { ...pkg.jest, ...dynamic }
assert(configs.length < 2, `Multiple jest configs found in ${dir} dir, use only a single one`)

if (configs.length > 0) {
const conf = { ...configs[0] }
assert(!conf.rootDir, 'Jest config.rootDir is not supported yet')
conf.rootDir = dir
return conf
} catch (e) {
if (e.code !== 'ENOENT') throw e
}

const parent = path.dirname(dir)
Expand Down Expand Up @@ -89,7 +93,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 c876ec6

Please sign in to comment.