C
CanxJS
v1.6.2
  • Learn
  • Blog
  • Showcase
C
CanxJS

Ultra-fast async MVC backend framework for Bun. Build production-ready APIs with elegance and speed.

Resources

  • Documentation
  • Learn
  • Blog
  • Showcase

Documentation

  • Introduction
  • Installation
  • Core Concepts
  • CLI Commands
  • API Reference

Legal

  • Privacy Policy
  • Terms of Service

© 2026 CanxJS. All rights reserved.

Built with ❤️ for Candra Kirana

  • Installation
  • Writing Tests
  • Browser Testing

Writing Tests

Learn how to use the TestCase class to write expressive integration tests.

Basic Usage

Extend your test files with TestCase to access helper methods.

typescript
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);
  });
});

Authentication

Use the actingAs helper to mock authentication headers easily.

typescript
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");
});

Making Requests

Supported HTTP methods:

  • api.get(url)
  • api.post(url, body)
  • api.put(url, body)
  • api.delete(url)

Component Testing (Mocks)

For unit testing controllers and services with mocked dependencies, use the Test utility.

typescript
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']);
});