Skip to main content

Quiz: TypeScript for testers

Why This Matters

A quick check that this module’s ideas hold up before you move on. Answer each question yourself before you open the answer.

Quiz

1. You forget await before page.click() in a test. What is the most likely result?

  • A. The test always fails with a TypeScript error
  • B. The test passes or fails depending on timing
  • C. Playwright adds the await automatically
  • D. The click is skipped
Show answer
B. Without await, the test continues before the click finishes. Whether the next step finds the page ready depends on timing: a flaky test. TypeScript itself does not complain (A); the no-floating-promises lint rule does, before the test runs.
Quiz

2. Why define a Product type for test data?

  • A. It makes tests run faster
  • B. A wrong field name or type becomes an editor error instead of a runtime failure
  • C. Playwright requires typed data
  • D. It replaces assertions
Show answer
B. Types catch mistakes in your test code before it runs. They say nothing about what the application does, so they never replace assertions.
Quiz

3. Playwright runs .ts files without a build step. What does that mean for type errors?

  • A. Playwright refuses to run a test with a type error
  • B. Type errors are only found if you run tsc --noEmit (or your editor) as well
  • C. Type errors are converted into failed assertions
  • D. Type errors cannot happen in test files
Show answer
B. Playwright strips the types and runs the JavaScript that is left, without checking it. That keeps the runner fast, which is why the example repository runs npm run typecheck as a separate CI step.
Quiz

4. Which line does not need await?

  • A. page.goto("/shopping")
  • B. page.getByRole("button", { name: "Add to Cart" })
  • C. expect(cart).toContainText("2 items")
  • D. button.click()
Show answer
B. Creating a locator only describes how to find an element; nothing happens in the browser yet. Navigation, actions and web-first assertions all return promises and must be awaited.
Quiz

5. A test compares 0.1 + 0.2 with 0.3 using toBe and fails. What is the best fix for money values?

  • A. Use toBeGreaterThan instead
  • B. Round to whole cents before comparing, as toCents() does
  • C. Store prices as strings
  • D. Retry the assertion
Show answer
B. Floating-point numbers cannot store most decimals exactly. Rounding to cents makes the comparison exact and matches what the page shows. For non-money values, toBeCloseTo is the other option.