← All exercises

Shipping tests that only confirm the code

The checkout service of an online store · difficulty 2/3 · ~8 min

The checkout service needs a shipping fee calculator. A teammate gave the AI assistant the pricing rules from the product spec and asked for "`calculateShipping(order)` with tests". Both the function and its tests came back in one go, and the test run is green. You are reviewing **the function and its tests together**. The visible tests are the ones the assistant wrote; treat them as part of the code under review, not as an independent oracle. The pricing rules from the spec: - A standard order whose subtotal is **100 or more** ships free. - Otherwise the fee is a base rate of 5.99, plus 1.50 for **every started kilogram above 2 kg** (2.0 kg adds nothing, 2.1 kg adds one kilogram, 3.0 kg adds one, 3.01 kg adds two). - Express delivery adds 9.99 on top of whatever the standard fee would be, so a free-shipping order pays 9.99 for express. - The result is rounded to two decimals. Decide whether the tests actually prove the rules, and whether the implementation follows them.

code.ts

Visible tests (they pass on the code as given)
import { describe, it, expect } from "@debunk/harness";
import {
  calculateShipping,
  BASE_RATE,
  PER_EXTRA_KG,
  EXPRESS_SUPPLEMENT,
} from "./code";

describe("calculateShipping", () => {
  it("ships free above the threshold", () => {
    expect(calculateShipping({ subtotal: 150, weightKg: 1 })).toBe(0);
  });

  it("charges the base rate plus the per-kg surcharge", () => {
    expect(calculateShipping({ subtotal: 40, weightKg: 1 })).toBe(BASE_RATE);
    expect(calculateShipping({ subtotal: 40, weightKg: 4 })).toBe(BASE_RATE + 2 * PER_EXTRA_KG);
  });

  it("adds the express supplement", () => {
    expect(calculateShipping({ subtotal: 40, weightKg: 1, express: true })).toBe(
      BASE_RATE + EXPRESS_SUPPLEMENT,
    );
  });
});