Bỏ qua, đến nội dung

@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 pathLoại testCông cụ chính
@digiforce-nc/testServer testsupertest, DB mocks, mock server
@digiforce-nc/test/clientReact component testTesting Library, provider wrappers
@digiforce-nc/test/webSchema / UI testMocked API, schema rendering
@digiforce-nc/test/e2eEnd-to-end testPlaywright helpers, page utilities
@digiforce-nc/test/vitest.mjsVitest configShared Vitest configuration

Server test utilities

Import từ @digiforce-nc/test:

UtilityMô 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:

UtilityMô 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:

UtilityMô tả
Page helpersNavigations, waits, common interactions
Test templatesBase test suites có thể extend
FixturesShared 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

PackageVai trò
@digiforce-nc/serverServer instance cho integration tests
vitestTest runner
playwrightE2e browser automation
@testing-library/reactReact component testing
supertestHTTP assertion