Skip to content

TypeScript

Toy Robot Simulator with Test-Driven Development (TDD) in TypeScript

The Toy Robot Simulator is a classic coding challenge. A robot sits on a 5x5 grid. It can be placed, moved forward, rotated left or right, and report its ...

17 Apr 2024 · 2 min read

Toy Robot Simulator with Test-Driven Development (TDD) in TypeScript

The Toy Robot Simulator is a classic coding challenge. A robot sits on a 5x5 grid. It can be placed, moved forward, rotated left or right, and report its position. Simple rules, but enough complexity to make TDD worthwhile.

I built this in TypeScript using test-driven development. Tests first, implementation second.

You can follow along in an empty project. Node.js, npm, TypeScript and a test runner are all you need:

Bash
npm init -y
npm install --save-dev typescript jest ts-jest @types/jest

The approach: tests first

TDD means writing a failing test before writing any code. You describe what the robot should do, watch it fail, then make it pass.

Start with the simplest behavior and build up. Place the robot. Check its position. Move it. Rotate it. Each step gets its own test.

Why TDD works here

The robot has clear inputs and outputs. Place it at (0,0) facing North, move once, it should be at (0,1). That's easy to test.

The benefit: every edge case gets covered. What happens when the robot tries to walk off the table? The test tells you.

The cost: it takes longer upfront. You write more code before you see anything "work." But when you're done, you're done. No manual testing, no surprise bugs at the edges.

Typescript
import Board from "../Board";

Build the rest yourself and the exercise pays off properly. Add obstacles. Make the grid bigger. Break things and let the tests catch you.

Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.

Go further
Live cohort on Maven

Forward Deployed AI Engineering: From Customer Problem to Production Value

A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.

January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.

View the live cohort

Keep reading