mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 13:54:57 +00:00
- Add JSON Test Interpreter for converting tests to Vitest suites - Implement unified test runner for discovering all test types - Support filtering by package and tags - Add comprehensive type definitions for test structures - Include documentation and usage examples Architecture: - Discover phase: Glob packages/*/[unit-tests|playwright|storybook]/tests.json - Register phase: Convert JSON to Vitest/Playwright/Storybook formats - Execute phase: Run through respective test frameworks Supported actions: function_call, render, click, fill, select, hover, focus, blur, waitFor Supported assertions: 20+ types from basic equals to React Testing Library matchers
6.4 KiB
6.4 KiB
Unified Test Runner
A JSON interpreter-based test runner that coordinates all test types across MetaBuilder:
- Unit Tests (JSON) - Discover and run from
packages/*/unit-tests/tests.json - E2E Tests (Playwright JSON) - Discover from
packages/*/playwright/tests.json - Storybook Stories (JSON) - Discover and validate from
packages/*/storybook/stories.json
Architecture
┌─────────────────────────────────────────────────────────┐
│ Unified Test Runner │
├─────────────────────────────────────────────────────────┤
│ │
│ 1. Discovery Phase │
│ ├─ Scan packages/*/unit-tests/tests.json │
│ ├─ Scan packages/*/playwright/tests.json │
│ └─ Scan packages/*/storybook/stories.json │
│ │
│ 2. Registration Phase │
│ ├─ JSON Test Interpreter (unit tests) │
│ ├─ E2E Test Coordinator (playwright) │
│ └─ Story Validator (storybook) │
│ │
│ 3. Execution Phase │
│ ├─ Vitest (unit tests) │
│ ├─ Playwright (E2E tests) │
│ └─ Storybook Build (stories) │
│ │
└─────────────────────────────────────────────────────────┘
Usage
Register and Run All Tests
import { runTests } from '@/e2e/test-runner';
await runTests();
With Configuration
await runTests({
verbose: true,
packages: ['ui_home', 'ui_auth'], // Filter by package
tags: ['@smoke', '@critical'], // Filter by tags
});
Get Statistics
import { UnifiedTestRunner } from '@/e2e/test-runner';
const runner = new UnifiedTestRunner();
const stats = await runner.getStatistics();
console.log(`Total test files: ${stats.totalFiles}`);
console.log(`Unit tests: ${stats.unitTests}`);
console.log(`E2E tests: ${stats.e2eTests}`);
console.log(`Storybook stories: ${stats.storybookStories}`);
JSON Test Interpreter
The JSONTestInterpreter class converts JSON test definitions to Vitest test suites at runtime:
import { JSONTestInterpreter } from '@/e2e/test-runner/json-interpreter';
const interpreter = new JSONTestInterpreter();
// Load imports
await interpreter.loadImports([
{ from: '@/components/Button', import: ['Button'] },
{ from: '@testing-library/react', import: ['render'] }
]);
// Register test suite
interpreter.registerTestSuite(jsonTestDefinition);
Supported Actions (Act Phase)
function_call- Call a function with fixturesrender- Render React component (requires Testing Library setup)click- Simulate click eventfill- Fill form inputselect- Select dropdown optionhover- Hover over elementfocus- Focus on elementblur- Blur from elementwaitFor- Wait for condition
Supported Assertions
Basic Assertions
equals- Strict equality (===)deepEquals- Deep object equalitynotEquals- Inequalitytruthy/falsy- Truthiness checks
Numeric Assertions
greaterThan- > comparisonlessThan- < comparisongreaterThanOrEqual- >= comparisonlessThanOrEqual- <= comparison
Type Assertions
null/notNull- Null checksundefined/notUndefined- Undefined checksinstanceOf- Instance checking
String/Collection Assertions
contains- String/array containsmatches- Regex matchinghasProperty- Property existshasLength- Collection length
DOM Assertions (React Testing Library)
toBeVisible- Element visibletoBeInTheDocument- Element in DOMtoHaveTextContent- Element text matchtoHaveAttribute- Element attributetoHaveClass- Element CSS classtoBeDisabled/toBeEnabled- Disabled statetoHaveValue- Input value
Control Flow
throws/notThrows- Exception handlingcustom- Custom assertion logic
Example: Unit Test in JSON
{
"$schema": "https://metabuilder.dev/schemas/tests.schema.json",
"schemaVersion": "2.0.0",
"package": "example_package",
"imports": [
{ "from": "@/lib/utils", "import": ["validateEmail"] }
],
"testSuites": [
{
"id": "suite_validate",
"name": "Email Validation",
"tests": [
{
"id": "test_valid_email",
"name": "should accept valid email",
"arrange": {
"fixtures": { "email": "user@example.com" }
},
"act": {
"type": "function_call",
"target": "validateEmail",
"input": "$arrange.fixtures.email"
},
"assert": {
"expectations": [
{
"type": "truthy",
"actual": "result",
"message": "Should return true for valid email"
}
]
}
}
]
}
]
}
Configuration
Place JSON test files in package directories:
packages/
├── my_package/
│ ├── unit-tests/
│ │ └── tests.json ← Unit tests (new)
│ ├── playwright/
│ │ └── tests.json ← E2E tests (existing)
│ └── storybook/
│ └── stories.json ← Component stories (existing)
Implementation Notes
- Minimal Dependencies: JSONTestInterpreter only depends on Vitest core
- Progressive Enhancement: DOM actions (click, render, etc.) warn if browser context missing
- Fixture Interpolation: Use
$arrange.fixtures.keyto reference fixture values - Mock Support: Integrated with Vitest's
vi.fn()for mocking
Files
index.ts- Main runner and orchestratorjson-interpreter.ts- JSON → Vitest convertertypes.ts- TypeScript type definitionsREADME.md- This file