# Exercise: Concert Tickets API Tests

Write the Supertest tests for Starlight Stage's working tickets API, from its shows to buying tickets, its errors and refunds, and prove they're good by catching three bugs the check plants in the API.

Journeyman · Node.js · Unit Testing · Updated November 19, 2026

By Gregor Dzierzon

Starlight Stage's tickets API works today. Fans can list the shows, buy 1 to 8 tickets at a time, get a clear error when a show is sold out, and get a refund. Nobody wrote tests for it, though, so the next person who changes it could break ticket sales and not find out until a fan does. This time the API is finished and the tests are yours to write. And to show your tests are worth having, a check plants three real bugs in the API, one at a time, and tells you whether your tests caught each one.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-node-concert-tickets-api-tests](https://github.com/ask-codey/exercise-node-concert-tickets-api-tests), also linked from the **Exercise** box beside the lesson. Clone it into your `nodejs-course` folder. In VS Code's terminal:

```bash
cd ~/nodejs-course
git clone https://github.com/ask-codey/exercise-node-concert-tickets-api-tests.git
```

Git makes a folder, `exercise-node-concert-tickets-api-tests`, with the exercise code inside. Open that folder in VS Code (**File > Open Folder**), open its terminal with **Terminal > New Terminal**, and install the packages, Express 5.3.0 and Supertest 7.3.2:

```bash
npm install
```

*Get the Exercise Code with Git* walks through cloning step by step, if you'd like a reminder.

The folder holds:

- `app.js`: the tickets API. The comment at its top lists every request it answers, with the status code and JSON body of each. It works: don't change it.
- `data.js`: the three shows and the tickets sold, kept in memory, and `resetData()`, which puts them back the way they start.
- `tickets.js`: `orderTotal(price, quantity)`, the price of an order: the ticket price times the number of tickets, plus a $2 fee, with no fee from 6 tickets.
- `server.js` and `tickets.http`: start the API at `http://localhost:3100` with `npm start`, and try its requests with REST Client.
- `app.test.js`: your tests. It already resets the data before every test, and it has one worked example.
- `check/`: the check, with its three planted-bug copies of `app.js`. You don't need to change it, and it's more fun not to read the copies until you're done.
- `README.md`: the list of behaviors your tests should cover.

## What to do

The shows the tests start from, every time:

| id | Show | Price | Seats left |
|---|---|---|---|
| 1 | The Velvet Owls | $25 | 40 |
| 2 | Midnight Brass Band | $32 | 4 |
| 3 | Juniper Lane | $18 | 0 |

The worked example in `app.test.js` shows the shape of a test:

```javascript
test("1) GET /shows answers 200 with all 3 shows, in order", async () => {
  const response = await request(app).get("/shows");

  assert.equal(response.status, 200);
  assert.equal(response.headers["content-type"], "application/json; charset=utf-8");
  assert.deepEqual(response.body.map((show) => show.title), ["The Velvet Owls", "Midnight Brass Band", "Juniper Lane"]);
});
```

1. **Read the README's list of behaviors.** There are twelve, numbered, each with the exact request, status code and body to expect. Behavior 1 is the worked example.
2. **Test the shows:** one show by its id, and a show that isn't there (behaviors 2 and 3).
3. **Test buying tickets:** a sale and what it does to the seats, the total with and without the fee, the last seats, one seat too many, a sold-out show, three bad quantities and a show that isn't there (behaviors 4 to 10).
4. **Test refunds:** buy first, then refund, then check the seats came back; and a ticket that isn't there (behaviors 11 and 12).

Name each test after what it sends and what it expects, starting with its behavior's number, such as `7) POST /shows/2/tickets with quantity 5 answers 409 and leaves the 4 seats alone`.

## Check your work

While you write, run your tests:

```bash
npm test
```

Every test you write should pass, because the API works. If one fails, work out the expected value again by hand from the README: the test is the part to fix.

Then see how good your tests are:

```bash
npm run check
```

It runs your tests four times: once on the working `app.js`, then once on each of three copies with a bug planted in it. One copy answers with a **wrong status**, one lets the **seats go negative**, and one gives a **refund that never restores a seat**. A bug counts as caught when at least one of your tests fails on its copy. At the start, check 1 passes (the worked example passes) and checks 2 to 4 fail: that's expected, because one test can't catch three bugs yet. A line that fails says what kind of test it's missing, for example *Every one of your tests passed on the copy with this bug, so it would reach real fans. Check the status code of every answer, not only its body: buying tickets answers 201.*

## Hints

- **Work out each expected value by hand,** from the README and the comment at the top of `app.js`. A test that copies whatever the API answers proves nothing.
- **Check the status code of every answer,** as well as its body, as *Testing an API with Supertest* does.
- **An order or a refund changes the data.** Check what changed with a second request: `GET /shows/2`, then read `body.seatsLeft`. A small helper function that does both keeps each test short.
- **A refused order changes nothing.** After a 400 or a 409, check that the seats left are still what they were, as *Testing Errors, with a Fresh Start for Every Test* does.
- **Bugs hide at the edges.** Show 2 has 4 seats left, so try 4 and 5, not only 2. *Your First Tests with node --test* explains edge cases.
- **Keep the `beforeEach`.** Every test starts from the table above, so ticket ids start at 1 in every test.

## When you're done

Every test is green with `npm test`, and `npm run check` shows four check marks:

```text
✔ 1) your tests run and pass on the working app.js (0.5345ms)
✔ 2) your tests catch planted bug 1, a wrong status (334.3719ms)
✔ 3) your tests catch planted bug 2, seats that go negative (380.2731ms)
✔ 4) your tests catch planted bug 3, a refund that never restores a seat (341.7039ms)
ℹ tests 4
ℹ suites 0
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1430.8767
```

Now open the three copies in `check/planted-bugs/` and find each bug. Which of your tests caught it? A sold-out test alone doesn't catch the seats bug: what does that tell you about testing one value at the edge, and one past it?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. *Hand In Your Work with Git* walks through each step.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-node-concert-tickets-api-tests`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-node-concert-tickets-api-tests.git
   git add .
   git commit -m "My tests"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-node-concert-tickets-api-tests`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

You can now prove an API works, and prove your proof is good: tests that catch a wrong status, a broken rule and a change that didn't happen are the ones that keep real users safe.

## Key terms

- [Supertest](https://www.ask-codey.com/glossary/supertest)
- [Edge Case](https://www.ask-codey.com/glossary/edge-case)
- [Test Isolation](https://www.ask-codey.com/glossary/test-isolation)
- [Assertion](https://www.ask-codey.com/glossary/assertion-unit-testing)

## Related

- [Your First Tests with node --test](https://www.ask-codey.com/lessons/node-testing-first-tests)
- [Testing an API with Supertest](https://www.ask-codey.com/lessons/node-testing-supertest)
- [Testing Errors, with a Fresh Start for Every Test](https://www.ask-codey.com/lessons/node-testing-errors-and-fresh-state)

Source: https://www.ask-codey.com/lessons/exercise-node-concert-tickets-api-tests
