Skip to main content

API Test Utilities (@utils-test-api)

The @wrkbelt/api/utils-test-api package provides a set of utilities for API testing with Playwright. These utilities simplify testing API endpoints by providing reusable controllers, assertions, and mocks.

Installation​

npm install @wrkbelt/api/utils-test-api --save-dev

Components​

BaseController​

The BaseController is an abstract class that provides a foundation for API testing. It wraps Playwright's APIRequestContext with standardized methods for making HTTP requests.

import { BaseController } from '@wrkbelt/api/utils-test-api';

// Extend BaseController to create specific API controllers
class MyApiController extends BaseController {
private readonly endpoint = 'my-endpoint';

async getItems(organizationId: string) {
return this.get(this.endpoint, {
query: { organization_id: organizationId },
});
}
}

Key features:

  • Standardized methods for HTTP operations (GET, POST, PUT, DELETE, PATCH)
  • Automatic request and response logging
  • Request tracing in test reports

Specialized Controllers​

The library includes specialized controllers for common API endpoints:

  • BookingAnswerCategoriesController: For managing booking answer categories
  • Other controllers for specific API endpoints

Example usage:

import { test } from '@playwright/test';
import { BookingAnswerCategoriesController } from '@wrkbelt/api/utils-test-api';

test('should create a booking answer category', async ({ request }) => {
const controller = new BookingAnswerCategoriesController(request);

const response = await controller.createBookingAnswerCategory('org-123', {
title: 'Test Category',
// other properties
});

expect(response.status()).toBe(201);
});

Assertions​

The library includes specialized assertions for API testing that extend Playwright's assertion capabilities.

Mocks​

Mocks for API testing allow simulation of API responses for testing scenarios that depend on external APIs.

Best Practices​

  • Extend the BaseController for new API endpoints
  • Use the standardized HTTP methods for consistent request handling
  • Leverage the built-in logging for debugging test failures
  • Combine with Playwright's test fixtures for efficient test setup