In this lesson, you read Edsger’s pricing tests as examples of what priceOrder() is supposed to do today, the criteria someone already wrote down before you change it.
Key Points
- A test is a written-down example.
test()takes a name and a function that Node runs.freshShop()gives each test its own copy of the seed shop, andassert.equal()fails the test when the actual value differs from the expected one. - Find a feature’s tests from the function they call.
git grep -n "priceOrder(" -- test/finds four lines intest/pricing.test.js, each in a different test. Thetest/at the end limits the search to the test folder, and the two dashes mark where the paths start. - Then read the file around those calls. It holds seven tests, and the three that don’t call
priceOrder()check code choice, quantities and money. One of them, “only one code is accepted,” guards your intent’s one-code-per-order line. Its one-line body checks that passing two codes throws an error. - Read each body as arrange, act and assert. In the test on line 10, lines 11 and 12 arrange a fresh shop and one Small Maps, and line 13 acts (prices it with READ20) and asserts (the total is 600).
- Restate each test as given/when/then, the form you used for your criteria. The test on line 16 asserts a discount of 150 on the 750p paid book plus a complimentary A Bookshop Window, so READ20’s 20% comes only from the paid book: 20% of 1750p would be 350p.
assert.throws()checks for an error. It runs the function it’s given and passes only if that function throws. The arrow function wraps the pricing call so it runs insideassert.throws(), not before it, and/inactive/is a pattern the error’s message must contain.- Trust the body, not the name. “Inactive discount codes do not apply” could mean full price, but the body says pricing throws an error. A name is only a summary, and the body is the check.
- Read the report.
npm testrunsnode --test, which finds the test files itself. In this repository, that’s every JavaScript file undertest/. Each check-mark line is a passing test. The first,test/helpers.js, is the helper file and holds no tests, but Node counts it, sotests 15at the bottom means 14 real tests. Withpass 15andfail 0, the examples you read are what pricing does today.
Your Map’s Tests Section
Here’s the Tests section you add to map.md: the test file and the function you searched for, then one given/when/then line per test you read, each taken from the test’s body rather than its name. Keep it next to your intent, so you can see which of these examples your change must leave alone.