Skip to content

Commit 8a813f4

Browse files
Боровский ДмитрийБоровский Дмитрий
authored andcommitted
feat(*): add support vitest
1 parent 5955731 commit 8a813f4

9 files changed

Lines changed: 1034 additions & 5 deletions

File tree

packages/arui-scripts/docs/commands.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ _package.json_
5757
arui-scripts test
5858
```
5959

60+
### test:vitest
61+
Команда `arui-scripts test:vitest` запускает unit тесты через [Vitest](https://vitest.dev/).
62+
63+
Конфигурация включает в себя:
64+
- Использование Jest-совместимого API (`describe`, `it`, `expect`)
65+
- Замену всех импортов `.css` файлов на пустые модули
66+
- Маппинг путей из `tsconfig.json` (paths)
67+
- Поддержку `setupFiles` из `package.json``jest.setupFiles` (например, для настройки Enzyme)
68+
69+
По умолчанию под маску для поиска тестов попадают файлы:
70+
- `src/**/__tests__/**/*.{ts,tsx,js,jsx}`
71+
- `src/**/__test__/**/*.{ts,tsx,js,jsx}`
72+
- `src/**/*.{test,spec,tests}.{ts,tsx,js,jsx}`
73+
74+
Для настройки Enzyme и других setup-файлов укажите их в `package.json`:
75+
76+
```json
77+
{
78+
"jest": {
79+
"setupFiles": ["<rootDir>/__tests__/setup.js"]
80+
}
81+
}
82+
```
83+
84+
**Как запустить?**
85+
86+
```bash
87+
arui-scripts test:vitest
88+
```
89+
6090
### docker-build
6191
Собирает клиентский и серверный код в production-режиме, создает docker-образ и пушит его в docker-репозиторий.
6292

packages/arui-scripts/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"ts-jest": "29.1.0",
115115
"ts-loader": "9.4.4",
116116
"ts-node": "9.1.1",
117+
"vitest": "^2.1.0",
117118
"webpack-bundle-analyzer": "4.10.2",
118119
"webpack-deduplication-plugin": "^0.0.8",
119120
"webpack-manifest-plugin": "3.2.0",
@@ -148,6 +149,7 @@
148149
"scripts": {
149150
"build": "sh bin/build.sh",
150151
"test": "jest",
152+
"test:vitest": "vitest",
151153
"lint:scripts": "arui-presets-lint scripts",
152154
"format": "arui-presets-lint format",
153155
"format:check": "arui-presets-lint format:check",

packages/arui-scripts/src/bin/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const commands: Record<string, () => void> = {
99
'docker-build': () => require('../commands/docker-build'),
1010
'docker-build:compiled': () => require('../commands/docker-build-compiled'),
1111
test: () => require('../commands/test'),
12+
'test:vitest': () => require('../commands/test-vitest'),
1213
'ensure-yarn': () => require('../commands/ensure-yarn'),
1314
'archive-build': () => require('../commands/archive-build'),
1415
'bundle-analyze': () => require('../commands/bundle-analyze'),
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { spawn } from 'child_process';
2+
import path from 'path';
3+
4+
// Do this as the first thing so that any code reading it knows the right env.
5+
process.env.BABEL_ENV = 'test';
6+
process.env.NODE_ENV = 'test';
7+
process.env.PUBLIC_URL = '';
8+
9+
// Makes the script crash on unhandled rejections instead of silently
10+
// ignoring them. In the future, promise rejections that are not handled will
11+
// terminate the Node.js process with a non-zero exit code.
12+
process.on('unhandledRejection', (err) => {
13+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
14+
throw err;
15+
});
16+
17+
const vitestConfigPath = path.resolve(__dirname, '../../configs/vitest/config.js');
18+
const args = process.argv.slice(3);
19+
const vitestArgs = ['run', '--config', vitestConfigPath, ...args];
20+
21+
const vitestDir = path.dirname(require.resolve('vitest/package.json'));
22+
const vitestBin = path.join(vitestDir, 'vitest.mjs');
23+
24+
const vitestProcess = spawn(process.execPath, [vitestBin, ...vitestArgs], {
25+
stdio: 'inherit',
26+
shell: false,
27+
});
28+
29+
vitestProcess.on('close', (code) => {
30+
process.exit(code ?? 0);
31+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from 'vitest/config';
2+
3+
import { getVitestConfig } from './settings';
4+
5+
export default defineConfig(getVitestConfig());
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
import { parseConfigFileTextToJson } from 'typescript';
5+
6+
function getSetupFiles(cwd: string): string[] {
7+
const packagePath = path.join(cwd, 'package.json');
8+
9+
let setupFiles: string[] = [];
10+
11+
if (fs.existsSync(packagePath)) {
12+
const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8')) as {
13+
jest?: { setupFiles?: string[] };
14+
};
15+
16+
setupFiles = pkg.jest?.setupFiles ?? [];
17+
}
18+
19+
return setupFiles.map((file: string) =>
20+
path.resolve(cwd, file.replace('<rootDir>', '.')),
21+
);
22+
}
23+
24+
export function getVitestConfig() {
25+
const cwd = process.cwd();
26+
const tsconfigPath = path.join(cwd, 'tsconfig.json');
27+
const baseUrl = path.dirname(tsconfigPath);
28+
29+
const cssMockPlugin = {
30+
name: 'arui-scripts-css-mock',
31+
enforce: 'pre' as const,
32+
load(id: string) {
33+
if (id.endsWith('.css')) {
34+
return 'export default {}';
35+
}
36+
37+
return undefined;
38+
},
39+
};
40+
const alias: Array<{ find: string | RegExp; replacement: string }> = [];
41+
42+
if (fs.existsSync(tsconfigPath)) {
43+
const tsConfigText = fs.readFileSync(tsconfigPath, 'utf8');
44+
const { config } = parseConfigFileTextToJson(tsconfigPath, tsConfigText);
45+
const paths = config?.compilerOptions?.paths as Record<string, string[]> | undefined;
46+
const tsBaseUrl = config?.compilerOptions?.baseUrl
47+
? path.resolve(baseUrl, config.compilerOptions.baseUrl)
48+
: baseUrl;
49+
50+
if (paths) {
51+
for (const [key, value] of Object.entries(paths)) {
52+
const pattern = key.replace(/\/\*$/, '');
53+
const target = value[0]?.replace(/\/\*$/, '') ?? '';
54+
55+
alias.push({
56+
find: pattern,
57+
replacement: path.resolve(tsBaseUrl, target),
58+
});
59+
}
60+
}
61+
}
62+
63+
const setupFiles = getSetupFiles(cwd);
64+
65+
return {
66+
plugins: [cssMockPlugin],
67+
resolve: {
68+
alias,
69+
},
70+
test: {
71+
globals: true,
72+
environment: 'jsdom' as const,
73+
setupFiles,
74+
include: [
75+
'src/**/__tests__/**/*.{ts,tsx,js,jsx}',
76+
'src/**/__test__/**/*.{ts,tsx,js,jsx}',
77+
'src/**/*.{test,spec,tests}.{ts,tsx,js,jsx}',
78+
],
79+
exclude: ['**/node_modules/**', '**/build/**', '**/.build/**'],
80+
coverage: {
81+
provider: 'v8' as const,
82+
include: ['src/**/*.{js,jsx,ts,tsx}'],
83+
exclude: ['**/*.d.ts', '**/__tests__/**'],
84+
},
85+
environmentOptions: {
86+
jsdom: {
87+
url: 'http://localhost',
88+
},
89+
},
90+
},
91+
};
92+
}

packages/example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"bundle-analyze": "arui-scripts bundle-analyze",
99
"start": "NODE_ENV=localhost arui-scripts start",
1010
"test": "arui-scripts test",
11+
"test:vitest": "arui-scripts test:vitest",
1112
"lint:styles": "arui-presets-lint styles",
1213
"lint:scripts": "arui-presets-lint scripts",
1314
"format": "arui-presets-lint format",
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`app > should be match to snapshot 1`] = `ShallowWrapper {}`;
24

35
exports[`app should be match to snapshot 1`] = `ShallowWrapper {}`;

0 commit comments

Comments
 (0)