JCC Express

Testing

Unit Testing

Unit tests focus on isolated logic — utility classes, model methods, service behavior — without booting a full HTTP server. They run with vitest (npm run test) and live in tests/Unit/.


Basic unit test

No setup required for pure logic tests. Import the code directly:

TypeScript
import { describe, it, expect } from "vitest";
import { Str } from "jcc-express-mvc/Core/Str";

describe("Str helper", () => {
  it("converts to slug", () => {
    expect(Str.slug("Hello World")).toBe("hello-world");
  });

  it("converts to camelCase", () => {
    expect(Str.camel("hello_world")).toBe("helloWorld");
  });
});

Unit tests that need the app container

If your code under test reads config, resolves services from the DI container, or hits the database, extend tests/TestCase.ts and boot the app:

TypeScript
import { describe, it, beforeEach, afterEach, expect } from "vitest";
import { TestCase } from "../TestCase";

describe("UserService", () => {
  const test = new (class extends TestCase {})();

  beforeEach(async () => await test.setUp());
  afterEach(async () => await test.tearDown());

  it("resolves UserService from the container", async () => {
    const service = (test as any).app.resolve("UserService");
    expect(service).toBeDefined();
  });
});

Use (test as any).app to access the Application instance when you need to reach the container directly.


Testing jobs without the full app

Unit-test queue jobs by calling handle() directly and registering a Queue on the app container:

TypeScript
import { describe, it, expect, beforeEach } from "vitest";
import { Application } from "jcc-express-mvc/Core";
import { Queue } from "jcc-express-mvc/Queue";
import { SendWelcomeEmail } from "../../app/Jobs/SendWelcomeEmail";

describe("SendWelcomeEmail job", () => {
  beforeEach(() => {
    const app = Application.getInstance();
    (globalThis as any).app = app;
    const queue = new Queue({ driver: "memory", queue: "default" });
    app.instance("Queue", queue);
  });

  it("dispatches successfully", async () => {
    const id = await SendWelcomeEmail.dispatch({ userId: 1 });
    expect(typeof id).toBe("string");
  });
});

Run unit tests

Bash
npm run test               # run all vitest tests once
npm run test:watch         # watch mode