Check JavaScript boundaries and await asynchronous test results.

Before starting: JavaScript functions, assertions and promises.

Practice project: Extend the examples into a small Jest study demo. Explain the setup, one normal case and one failure or boundary case.

Start here: concepts and guided practice

  1. Jest: test a boundary

    A useful test checks observable behavior at a decision boundary. The two assertions distinguish a passing score from the nearest failing score.

    Node.js with Jest installed in the project; save as score.test.js and run the local Jest command.

    test("score boundary", () => {
      const passed = score => score >= 60;
      expect(passed(59)).toBe(false);
      expect(passed(60)).toBe(true);
    });
    Check the output
    The test passes.

    Change >= to >; confirm that the 60 assertion fails.

  2. Jest: wait for asynchronous work

    An asynchronous test must await or return its promise so the runner can observe completion and failure. This small fake isolates promise handling; it does not test a real network.

    Node.js with Jest installed in the project; save as score.test.js and run the local Jest command.

    test("empty report", async () => {
      const load = async () => [];
      await expect(load()).resolves.toEqual([]);
    });
    Check the output
    The asynchronous test passes.

    Return [7] instead and confirm that the assertion fails.

Open the Jest learning path on DevStudyHub · Play coding games on this blog

logoblog

No comments:

Post a Comment

Please do not enter any spam link in the comment box.