lecture 7 of 200 completed

Choose data that exposes a bug

Two tests pass with the same input. Give each case a value that can expose the mistake.

What you'll learn

  • Find the input a passing test actually uses.
  • Give each input its own named test.
  • Explain which input exposes the mistake.

expect(link.searchParams.get('find')).toBe('paper') passes. The test is called “shares words joined by &”.

You copy a search for ink & paper from a reading list. Someone opens your link. Their search has lost the word paper.

Here is the feature and its tests. The reading list is invented. The example builds an address without opening a website.

import { test, expect } from '@playwright/test'

function copiedLink(words) {
  return 'https://example.org/read?find=' + words
}

test('shares the word paper', () => {
  const link = new URL(copiedLink('paper'))
  expect(link.searchParams.get('find')).toBe('paper')
})

test('shares words joined by &', () => {
  const link = new URL(copiedLink('paper'))
  expect(link.searchParams.get('find')).toBe('paper')
})

Start with the last test. Read its title. Then look at the value passed to copiedLink. It is paper. The & appears only in the title.

The first line brings in test and expect. test gives the check a name. expect compares the result with what you expected.

copiedLink is a function. It takes the search words and returns a link as text. The + joins those words to the end of the address.

new URL(...) reads that text as a web address. The part after ? holds named values. searchParams.get('find') reads the value named find.

For paper, the link keeps the whole word. Both tests use that input. Both pass.

Now follow ink & paper through the same function. In an address, & starts another field. The value of find ends after ink and its following space. A reader opening that link gets a different search.

We could edit the second test. But its title and its input would still be written separately. The next person copying it could make the same mistake.

Give each search its own named test. Run this block in place of the first one.

import { test, expect } from '@playwright/test'

function copiedLink(words) {
  return 'https://example.org/read?find=' + words
}

for (const words of ['paper', 'ink & paper']) {
  test(`shares "${words}"`, () => {
    const link = new URL(copiedLink(words))
    expect(link.searchParams.get('find')).toBe(words)
  })
}

The square brackets hold an array. An array is a list of values.

for...of takes each value in turn. Inside the loop, words holds the current search.

The test line gives that search its own test. The backticks let ${words} put the actual input in the title. Playwright registers each test before it runs the checks.

copiedLink(words) sends that same input to the feature. The expectation compares what the link returns with what the reader typed. We get the expected value from the original search.

Run the tests. shares "paper" passes. shares "ink & paper" fails. The title now tells you how to reproduce the problem.

Keep that failing check. Fix the feature by replacing its copiedLink function:

function copiedLink(words) {
  const link = new URL('https://example.org/read')
  link.searchParams.set('find', words)
  return link.href
}

The first line inside the function builds the address. set stores the whole search as one value. href gives us the finished link as text. Run the same tests again. Both searches now keep their words.

When you review a copied test, follow its input from the title to the check. A different title can still exercise exactly the same behavior.

Try that decision in the shop. Before reading its instructions, think of a quantity that could hide a bad calculation.

The problem “The subtotal forgets the quantity” lets you check your choice against the shop.

Next lesson. Give each test its own data (paid)

Remember this. Choose an input that exposes the mistake. Give that input its own named test.

Playwright’s guide to running a test with different inputs

now prove it

Reading is the setup. Solve this problem in the editor. We break the app on purpose to check your test would catch it.