ProgramGuru

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

Testing Classes and Interfaces — concept flowInterface contract → Concrete instance → Behavior assertionsTesting Classes and Interfaces1Interface contract2Concrete instance3Behavior assertionsTests exercise behavior, while types check static contracts.
Follow the responsibilities from input to checked result.

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
1

Compile: 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

Which statement makes a trustworthy TypeScript testing practice?
Choose one answer. You can try again.

Answer and explanation

A test must exercise behavior and assert an expected outcome; compiler checks and test results provide different evidence.

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.

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.

    Next lesson: Testing API Clients →