-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arui-scripts): add in test command ability to use preset from co…
…nfigs
- Loading branch information
Огриско Егор Александрович
committed
Nov 6, 2024
1 parent
6f1e6ec
commit 5c94eea
Showing
6 changed files
with
869 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'arui-scripts': minor | ||
--- | ||
|
||
Добавление возможности использовать preset в jest конфигурации приложений, для переопределения настроек тестирования |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { replaceRootDirInPath } from 'jest-config'; | ||
import Resolver from 'jest-resolve'; | ||
import merge from 'lodash.merge'; | ||
|
||
import { configs } from '../../configs/app-configs'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import defaultJestConfig from '../../configs/jest/settings'; | ||
|
||
const PRESET_EXTENSIONS = ['.json', '.js', '.cjs', '.mjs']; | ||
const PRESET_NAME = 'jest-preset'; | ||
|
||
export const getJestConfig = async () => { | ||
const {preset, ...appJestConfig} = await getAppJestConfig(); | ||
|
||
let presetConfig = {}; | ||
|
||
if (preset) { | ||
presetConfig = await getPresetConfig(preset); | ||
} | ||
|
||
return merge(defaultJestConfig, presetConfig, appJestConfig); | ||
}; | ||
|
||
async function getAppJestConfig() { | ||
const jestConfigPath = path.resolve(process.cwd(), 'jest.config.js'); | ||
|
||
if (fs.existsSync(jestConfigPath)) { | ||
return (await import(jestConfigPath)).default; | ||
} | ||
|
||
if (configs.appPackage.jest) { | ||
return configs.appPackage.jest; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
async function getPresetConfig(presetPath?: string) { | ||
if (!presetPath) { | ||
return {}; | ||
} | ||
const rootDir = process.cwd(); | ||
|
||
const normalizedPresetPath = replaceRootDirInPath(rootDir, presetPath); | ||
const presetModule = Resolver.findNodeModule( | ||
normalizedPresetPath.startsWith('.') | ||
? normalizedPresetPath | ||
: path.join(normalizedPresetPath, PRESET_NAME), | ||
{ | ||
basedir: rootDir, | ||
extensions: PRESET_EXTENSIONS, | ||
}, | ||
); | ||
|
||
if (!presetModule) { | ||
throw new Error(`Cannot find module '${normalizedPresetPath}'`); | ||
} | ||
|
||
const { preset: subPreset, ...preset } = (await import(presetModule)).default; | ||
|
||
if (subPreset) { | ||
console.warn(`Jest can't handle preset chaining. Preset "${subPreset}" will be ignored.`); | ||
} | ||
|
||
return preset; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.