Lesson 7 of 7
Practice & Quiz - Testing & Type Safety
Solidify your TypeScript testing skills - Jest setup, typed mocks, in-memory fakes, React component testing, and type-level assertions - with a practical test exercise and 5 quiz questions.
Practical: Service Integration Test with In-Memory Fake
// Full integration test for TaskService using an in-memory fake repository
import { TaskService } from "../src/services/TaskService";
import { InMemoryTaskRepository } from "../src/repositories/InMemoryTaskRepository";
// Fake repo - satisfies the interface, zero network calls
function makeService() {
const repo = new InMemoryTaskRepository();
const notifier = { notify: jest.fn() }; // typed spy
const logger = { log: jest.fn(), error: jest.fn() }; // null logger
const service = new TaskService(repo, notifier, logger);
return { service, repo, notifier, logger };
}
describe("TaskService", () => {
describe("create", () => {
it("returns created task with id and PENDING status", async () => {
const { service } = makeService();
const result = await service.create({ title: "Ship it", priority: "high" });
// Narrow Result<T, E> before accessing value
if (!result.ok) throw new Error(`Unexpected error: ${result.error}`);
expect(result.value.id).toBeDefined();
expect(result.value.status).toBe("PENDING");
expect(result.value.title).toBe("Ship it");
});
it("rejects blank titles", async () => {
const { service } = makeService();
const result = await service.create({ title: " ", priority: "low" });
expect(result.ok).toBe(false);
});
});
describe("complete", () => {
it("sets task status to COMPLETED and fires notifier", async () => {
const { service, notifier } = makeService();
const created = await service.create({ title: "Launch", priority: "high" });
if (!created.ok) throw new Error("create failed");
const result = await service.complete(created.value.id);
if (!result.ok) throw new Error(`complete failed: ${result.error}`);
expect(result.value.status).toBe("COMPLETED");
expect(notifier.notify).toHaveBeenCalledTimes(1);
expect(notifier.notify).toHaveBeenCalledWith(
expect.objectContaining({ status: "COMPLETED" })
);
});
it("returns error when task not found", async () => {
const { service } = makeService();
const result = await service.complete(9999);
expect(result.ok).toBe(false);
});
});
});Module Quiz
Why are in-memory fake repositories preferred over `jest.mock()` module mocks for service integration tests?
Tip
Tip
Practice Practice Quiz Testing Type Safety in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
TypeScript = JavaScript + static types → fewer bugs, better tooling
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz Testing Type Safety from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Practice Quiz Testing Type Safety is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Finished this lesson?