Skip to content
FrontDepth, Home

Back to the topic

Practice: What to test, and at which level

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    This is a plain-JS simulation of a test runner, not Vitest: test() runs a function and records "pass" unless it throws, and expect().toBe() throws when the values differ by Object.is, as Vitest's toBe does. cartTotal has a bug. What does it log?

    JavaScript
    const results = [];
    
    function test(name, fn) {
      try {
        fn();
        results.push("pass " + name);
      } catch (error) {
        results.push("fail " + name + ": " + error.message);
      }
    }
    
    function expect(actual) {
      return {
        toBe(expected) {
          if (!Object.is(actual, expected)) {
            throw new Error("expected " + expected + ", got " + actual);
          }
        },
      };
    }
    
    function cartTotal(items) {
      let total = 0;
      for (const item of items) total += item.price;
      return total;
    }
    
    test("sums one item", () => {
      expect(cartTotal([{ price: 4, quantity: 1 }])).toBe(4);
    });
    
    test("multiplies by quantity", () => {
      expect(cartTotal([{ price: 4, quantity: 2 }])).toBe(8);
    });
    
    test("handles two items", () => {
      cartTotal([
        { price: 4, quantity: 2 },
        { price: 10, quantity: 1 },
      ]);
    });
    
    for (const line of results) console.log(line);
  2. Open questionJunior

    What are unit, integration and end-to-end tests, and what does each one catch and cost?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    A plain-JS simulation of two tests run against three versions of a cart: V1 is the original, V2 is a refactor that stores items in a Map, and V3 keeps V1's storage but changes total(). internalsTest reads the private _items; behaviourTest uses only the public API. Each line logs whether the two tests pass. What does it log?

    JavaScript
    function createCartV1() {
      const items = [];
      return {
        _items: items,
        add(item) {
          const found = items.find((i) => i.id === item.id);
          if (found) found.quantity += 1;
          else items.push({ ...item, quantity: 1 });
        },
        total() {
          return items.reduce((sum, i) => sum + i.price * i.quantity, 0);
        },
      };
    }
    
    function createCartV2() {
      const items = new Map();
      return {
        _items: items,
        add(item) {
          const quantity = (items.get(item.id)?.quantity ?? 0) + 1;
          items.set(item.id, { ...item, quantity });
        },
        total() {
          let sum = 0;
          for (const i of items.values()) sum += i.price * i.quantity;
          return sum;
        },
      };
    }
    
    function createCartV3() {
      const cart = createCartV1();
      cart.total = () => cart._items.reduce((sum, i) => sum + i.price, 0);
      return cart;
    }
    
    function internalsTest(createCart) {
      const cart = createCart();
      cart.add({ id: "tea", price: 4 });
      cart.add({ id: "tea", price: 4 });
      return Array.isArray(cart._items) && cart._items[0].quantity === 2;
    }
    
    function behaviourTest(createCart) {
      const cart = createCart();
      cart.add({ id: "tea", price: 4 });
      cart.add({ id: "tea", price: 4 });
      return cart.total() === 8;
    }
    
    for (const createCart of [createCartV1, createCartV2, createCartV3]) {
      console.log(internalsTest(createCart), behaviourTest(createCart));
    }
  4. Predict the outputMiddle

    A plain-JS simulation of branch coverage: discount records which of its two branches ran, the way a coverage tool instruments code, and coverage() reports the share of branches that ran. The first two calls stand for tests without assertions, the third for a test with an assertion. What does it log?

    JavaScript
    const branchesRun = new Set();
    
    function discount(total, code) {
      if (code === "VIP") {
        branchesRun.add("vip");
        return total * 0.8;
      }
      branchesRun.add("regular");
      return total - 10;
    }
    
    const coverage = () => (branchesRun.size / 2) * 100 + "%";
    
    discount(100, "VIP");
    console.log(coverage());
    
    discount(100, "");
    console.log(coverage());
    
    console.log(discount(100, "") === 90);
    console.log(discount(5, ""));
  5. Open questionMiddle

    Compare the test pyramid and the testing trophy. Which would you follow for a React app, and why?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Open questionMiddle

    What does "test behaviour, not implementation details" mean, and how do you recognise a test that breaks the rule?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Open questionSenior

    A bug reached production. How do you decide which test to write for it, and how do you keep the whole test suite trustworthy in CI?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.