Learn how to use the TestCase class to write expressive integration tests.
Extend your test files with TestCase to access helper methods.
import { TestClient } from "canxjs";
import { describe, test } from "bun:test";
describe("User API", () => {
test("guest cannot access profile", async () => {
const api = new TestClient();
// Fluent assertion API
const response = await api.get("/api/user");
response.assertStatus(401);
});
});Use the actingAs helper to mock authentication headers easily.
test("authenticated user can access profile", async () => {
const api = new TestClient();
// Simulate logged in user with a token
const response = await api.withToken("fake-jwt-token").get("/api/user");
response.assertStatus(200);
// Verify response body
const data = await response.json();
expect(data.email).toBe("user@example.com");
});Supported HTTP methods:
api.get(url)api.post(url, body)api.put(url, body)api.delete(url)For unit testing controllers and services with mocked dependencies, use the Test utility.
import { Test, TestingModule } from 'canxjs';
import { UserController } from './UserController';
import { UserService } from './UserService';
test("it returns users", async () => {
// Create a testing module
const module: TestingModule = await Test.createTestingModule({
controllers: [UserController],
providers: [UserService],
})
.overrideProvider(UserService)
.useValue({
findAll: () => ['test-user'],
})
.compile();
const controller = module.get(UserController);
const result = await controller.findAll();
expect(result).toEqual(['test-user']);
});