Testing Classes and Interfaces
Testing Classes and Interfaces — why it matters
Interfaces define structural contracts for compile-time checking, while classes provide runtime objects whose methods can be exercised. An interface cannot be instantiated or tested directly after compilation. Test the observable behavior of a concrete implementation that promises to satisfy it.
What you’ll learn
- Explain testing classes and interfaces and the problem it solves.
- Identify the relevant compile-time checks and runtime responsibilities.
- Run the example, predict its output, and try a realistic modification.
Understand the mechanism
MemoryCounter implements the structural contract. The test calls a real class object through the Counter interface and observes that increment changes the state returned by read.
Passing implements Counter checks shape but does not verify every intended behavior or invariant.
Follow the concept flow
Run the primary TypeScript example
Save as testing-classes-interfaces.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.
TypeScript source
interface Counter { increment(): number; read(): number }
class MemoryCounter implements Counter {
private count = 0;
increment(): number { return ++this.count; }
read(): number { return this.count; }
}
const counter: Counter = new MemoryCounter();
console.log(counter.increment());
console.log(counter.read());Verified output
1
1Compile: npx tsc --strict --target es2022 --module commonjs testing-classes-interfaces.ts; run: node testing-classes-interfaces.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.
The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.
Trace the result
MemoryCounter implements the structural contract. The test calls a real class object through the Counter interface and observes that increment changes the state returned by read.
Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.
Common mistake and correction
Passing implements Counter checks shape but does not verify every intended behavior or invariant.
Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.
Practice the concept
Test several increment calls and check that two independent instances maintain separate counts.
Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.
Check your understanding
What to remember
Interfaces define structural contracts for compile-time checking, while classes provide runtime objects whose methods can be exercised.
Run the primary example, then complete the practice task or environment-specific lab.
Reaching the recap and completing the quizzes are separate milestones.