Giao diện
@digiforce-nc/test
| Package | @digiforce-nc/test |
| Vai trò | Testing utilities - helpers, mocks, harnesses cho server, client, và e2e tests |
Tổng quan
@digiforce-nc/test cung cấp bộ công cụ test thống nhất cho toàn bộ monorepo. Package sử dụng sub-path exports để tách biệt utilities theo loại test.
Sub-path exports
| Import path | Loại test | Công cụ chính |
|---|---|---|
@digiforce-nc/test | Server test | supertest, DB mocks, mock server |
@digiforce-nc/test/client | React component test | Testing Library, provider wrappers |
@digiforce-nc/test/web | Schema / UI test | Mocked API, schema rendering |
@digiforce-nc/test/e2e | End-to-end test | Playwright helpers, page utilities |
@digiforce-nc/test/vitest.mjs | Vitest config | Shared Vitest configuration |
Server test utilities
Import từ @digiforce-nc/test:
| Utility | Mô tả |
|---|---|
startServerWithRandomPort() | Khởi động server test trên port ngẫu nhiên, tránh conflict |
createWsClient() | Tạo WebSocket client kết nối tới test server |
isPg() / isMysql() / isSqlite() | Kiểm tra dialect database đang test |
randomStr(len?) | Generate chuỗi ngẫu nhiên cho test data |
sleep(ms) | Promise-based delay |
createMockServer() | Tạo server instance với mock database |
Ví dụ: Server test
ts
import { startServerWithRandomPort, isPg, randomStr } from '@digiforce-nc/test';
describe('User API', () => {
let app;
let agent;
beforeAll(async () => {
const result = await startServerWithRandomPort();
app = result.app;
agent = result.agent; // supertest agent
});
afterAll(async () => {
await app.destroy();
});
it('should create user', async () => {
const username = randomStr(8);
const res = await agent
.post('/api/users:create')
.send({ username, email: `${username}@test.com` });
expect(res.status).toBe(200);
expect(res.body.data.username).toBe(username);
});
it('should handle DB-specific behavior', async () => {
if (isPg()) {
// Logic chỉ chạy trên PostgreSQL
}
});
});Client test utilities
Import từ @digiforce-nc/test/client:
| Utility | Mô tả |
|---|---|
render() | Render component với đầy đủ providers (Router, Theme, API) |
mockAPIResponse() | Mock response cho API calls |
waitForLoading() | Chờ loading state hoàn tất |
Ví dụ: Client test
tsx
import { render, mockAPIResponse } from '@digiforce-nc/test/client';
import { UserList } from '../UserList';
describe('UserList', () => {
it('should render users', async () => {
mockAPIResponse('users:list', {
data: [
{ id: 1, username: 'admin' },
{ id: 2, username: 'user1' },
],
});
const { findByText } = render(<UserList />);
expect(await findByText('admin')).toBeInTheDocument();
expect(await findByText('user1')).toBeInTheDocument();
});
});E2e test utilities
Import từ @digiforce-nc/test/e2e:
| Utility | Mô tả |
|---|---|
| Page helpers | Navigations, waits, common interactions |
| Test templates | Base test suites có thể extend |
| Fixtures | Shared test data và setup |
E2e tests sử dụng Playwright làm framework, với các helper bọc sẵn pattern thường dùng trong Digiforce UI.
Vitest configuration
ts
// vitest.config.ts trong bất kỳ package nào
import config from '@digiforce-nc/test/vitest.mjs';
export default config;Config chia sẻ bao gồm: alias resolution, transform rules, coverage settings, và test environment setup.
Dependencies
| Package | Vai trò |
|---|---|
@digiforce-nc/server | Server instance cho integration tests |
vitest | Test runner |
playwright | E2e browser automation |
@testing-library/react | React component testing |
supertest | HTTP assertion |