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 e3cfc26
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 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 @@ -124,7 +128,9 @@ export async function installJestEnvironment(jestGlobals) {
if (c.restoreMocks) beforeEach(() => jest.restoreAllMocks())
if (c.resetModules) beforeEach(() => jest.resetModules())

const require = createRequire(config.rootDir)
const require = config.rootDir
? createRequire(path.resolve(config.rootDir, 'package.json'))
: () => assert.fail('Unreachable: requiring plugins without a rootDir')

if (Object.hasOwn(specialEnvironments, c.testEnvironment)) {
specialEnvironments[c.testEnvironment](require, jestGlobals, c.testEnvironmentOptions)
Expand Down

0 comments on commit e3cfc26

Please sign in to comment.