From c876ec63031215ded606975fff3495514534cab2 Mon Sep 17 00:00:00 2001 From: Nikita Skovoroda Date: Sun, 14 Jul 2024 11:20:41 +0300 Subject: [PATCH] fix jest config resolution --- src/jest.config.js | 50 +++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/jest.config.js b/src/jest.config.js index fa9cef9..dccf211 100644 --- a/src/jest.config.js +++ b/src/jest.config.js @@ -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' @@ -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) @@ -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`) }