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

Testing

Unit Testing

Write focused, fast unit tests for your CanxJS application components. Test controllers, services, models, and utilities in isolation.

Best Practices

Test one thing per test case
Use descriptive test names
Arrange-Act-Assert (AAA) pattern
Mock external dependencies
Keep tests independent
Use beforeEach for common setup

Testing Controllers

Test your controller methods in isolation by instantiating them directly and calling their methods.

tests/UserController.test.ts
1import { describe, test, expect, beforeEach } from "bun:test";
2import { UserController } from "../controllers/UserController";
3
4describe("UserController", () => {
5 let controller: UserController;
6
7 beforeEach(() => {
8 controller = new UserController();
9 });
10
11 test("index returns list of users", async () => {
12 const users = await controller.index();
13
14 expect(Array.isArray(users)).toBe(true);
15 expect(users.length).toBeGreaterThan(0);
16 });
17
18 test("show returns single user by ID", async () => {
19 const user = await controller.show(1);
20
21 expect(user).toBeDefined();
22 expect(user.id).toBe(1);
23 });
24
25 test("store creates new user", async () => {
26 const userData = { name: "John", email: "john@example.com" };
27 const user = await controller.store(userData);
28
29 expect(user.name).toBe("John");
30 expect(user.email).toBe("john@example.com");
31 });
32});

Testing Services

Services contain your business logic. Test them thoroughly to ensure core functionality works correctly.

tests/AuthService.test.ts
1import { describe, test, expect, mock } from "bun:test";
2import { AuthService } from "../services/AuthService";
3
4describe("AuthService", () => {
5 test("hash password correctly", async () => {
6 const service = new AuthService();
7 const password = "mySecurePassword";
8
9 const hashed = await service.hashPassword(password);
10
11 expect(hashed).not.toBe(password);
12 expect(hashed.length).toBeGreaterThan(20);
13 });
14
15 test("verify password returns true for correct password", async () => {
16 const service = new AuthService();
17 const password = "mySecurePassword";
18 const hashed = await service.hashPassword(password);
19
20 const isValid = await service.verifyPassword(password, hashed);
21
22 expect(isValid).toBe(true);
23 });
24
25 test("verify password returns false for wrong password", async () => {
26 const service = new AuthService();
27 const hashed = await service.hashPassword("correct");
28
29 const isValid = await service.verifyPassword("wrong", hashed);
30
31 expect(isValid).toBe(false);
32 });
33});

Mocking Dependencies

Use Bun's built-in mock() and spyOn() to mock external dependencies.

tests/UserService.test.ts
1import { describe, test, expect, mock, spyOn } from "bun:test";
2import { EmailService } from "../services/EmailService";
3import { UserService } from "../services/UserService";
4
5describe("UserService with mocks", () => {
6 test("sends welcome email on registration", async () => {
7 // Create mock
8 const sendEmailMock = mock(() => Promise.resolve(true));
9
10 const emailService = new EmailService();
11 spyOn(emailService, "send").mockImplementation(sendEmailMock);
12
13 const userService = new UserService(emailService);
14 await userService.register({
15 name: "John",
16 email: "john@example.com"
17 });
18
19 expect(sendEmailMock).toHaveBeenCalled();
20 expect(sendEmailMock).toHaveBeenCalledWith(
21 "john@example.com",
22 "Welcome to CanxJS!"
23 );
24 });
25});

Next Steps

Learn more about testing patterns and explore integration testing.