Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(arui-scripts): add in test command ability to use preset from configs #282

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-numbers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'arui-scripts': minor
---

Добавление возможности использовать preset в jest конфигурации приложений, для переопределения настроек тестирования
6 changes: 4 additions & 2 deletions packages/arui-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"image-minimizer-webpack-plugin": "^3.8.3",
"imagemin": "^8.0.1",
"imagemin-svgo": "^10.0.1",
"jest": "28.1.3",
"jest": "^29.7.0",
"jest-config": "^29.7.0",
"jest-resolve": "^29.7.0",
"jest-snapshot-serializer-class-name-to-string": "1.0.0",
"lodash.merge": "4.6.2",
"mini-css-extract-plugin": "2.7.6",
Expand Down Expand Up @@ -118,7 +120,7 @@
"@types/case-sensitive-paths-webpack-plugin": "2.1.6",
"@types/compression-webpack-plugin": "4.0.1",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^26.0.24",
"@types/jest": "^29.5.14",
"@types/lodash.merge": "^4.6.7",
"@types/node": "12.20.55",
"@types/react-dev-utils": "9.0.11",
Expand Down
70 changes: 70 additions & 0 deletions packages/arui-scripts/src/commands/test/get-config.ts
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;
}
26 changes: 15 additions & 11 deletions packages/arui-scripts/src/commands/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// TODO: remove eslint-disable and eslint-disable-next-line
/* eslint-disable @typescript-eslint/no-var-requires */
import * as jestRunner from 'jest';

import { getJestConfig } from './get-config';

// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';
process.env.PUBLIC_URL = '';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const jestRunner = require('jest');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const jestConfig = require('../../configs/jest');

// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
Expand All @@ -25,6 +20,15 @@ process.on('unhandledRejection', (err) => {
// @ts-ignore
const argv = process.argv.slice(3);

argv.push('--config', JSON.stringify(jestConfig));
const runJest = async () => {
const jestConfig = await getJestConfig();

argv.push('--config', JSON.stringify(jestConfig));

jestRunner.run(argv);
jestRunner.run(argv);
};

runJest().catch((error) => {
console.error('Error running Jest:', error);
process.exit(1);
});
22 changes: 0 additions & 22 deletions packages/arui-scripts/src/configs/jest/index.js

This file was deleted.

Loading
Loading