Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Read Tests as Examples

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, and assert.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 in test/pricing.test.js, each in a different test. The test/ 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 inside assert.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 test runs node --test, which finds the test files itself. In this repository, that’s every JavaScript file under test/. 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, so tests 15 at the bottom means 14 real tests. With pass 15 and fail 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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your criteria for the student discount are examples: a basket, a code, and the total you expect. Edsger wrote down examples like that for today’s pricing, as tests.

00:12 You read this one in the first course. A test is a call to test(), with a name and a function that Node runs. freshShop() gives the test its own copy of the seed shop.

00:23 assert.equal() fails the test if the actual total differs from the expected one. Here, one Small Maps with OLD120 switched on must total 0. Your intent lists what must not change.

00:38 Edsger’s tests tell you what pricing does now.

00:43 Start from the function the student discount will change, priceOrder(), and search for calls to it. The test/ at the end limits the search to the test folder.

00:54 Four lines come back, all in pricing.test.js, each in a different test. You’ve read line 32’s test.

01:03 Now read the file around them. It holds seven tests. The other three check code choice, quantities and money. “Only one code is accepted” guards your one-code line.

01:14 Its body checks that two codes throw.

01:18 Here’s the first, from line 10. Read its body in three parts. Arrange: lines 11 and 12 set up a fresh shop and a basket of one Small Maps. Act: line 13 prices that basket with the READ20 code.

01:35 Assert: on the same line, assert.equal() checks that the total is 600. Now say it the way you wrote your criteria. Given one Small Maps, when it’s priced with READ20, then the total is 600p.

01:50 That’s one of your must-not criteria.

01:55 The second test, from line 16, arranges Small Maps and A Bookshop Window, marked complimentary. It prices them with READ20. It asserts three values: a gross subtotal of 1750, a paid-items subtotal of 750, and a discount of 150.

02:15 Your turn: pause the video and predict. Is READ20’s 20 percent taken from the 1750 or from the 750?

02:29 It’s the 750. Twenty percent of 750p is 150p. Of 1750p, it would be 350p.

02:42 So given a paid book and a complimentary one, when they’re priced with READ20, then the discount comes only from the paid book. Grace chose the same reading for students.

02:54 The last test, from line 53, arranges a fresh shop, where OLD120 is inactive. Act and assert share one call. assert.throws() runs a function and passes only if it throws.

03:07 The arrow function wraps the pricing call, so it runs inside assert.throws(), not before it. The pattern between the slashes says the message must contain the word inactive.

03:20 Given an inactive code, when an order is priced with it, then pricing throws an error that mentions inactive.

03:28 Watch out for the name. “Do not apply” could mean full price, but the body says pricing throws. A name is only a summary, and the body is the check. When they disagree, trust the body.

03:42 Run the whole suite with npm test. It runs node --test, which finds the test files itself. Here, that’s every JavaScript file in the test folder.

03:53 Each check-mark line is a passing test. The four you read are among them. The first line is a file without tests. It’s helpers.js, where freshShop() lives.

04:05 At the bottom, tests says 15, helpers.js included, so 14 real tests. Pass says 15, and fail says 0. Everything you read is what pricing does today.

04:21 Write what you found in map.md, under a new Tests heading. Name the test file and the function you searched for. Then add one given, when, then line per test you read, from its body.

04:35 That works for any suite you inherit. Search for the function’s calls, then read the file around them. Then read each body as given, when, then. Next, in See What a Green Run Leaves Out, you’ll look at what that green run doesn’t check.

Become a Member to join the conversation.