Most real tests are asynchronous: they wait for a fetch, a timer or a database. A runner must wait for each test's promise, run the tests one at a time so they do not trample each other's state, and give up on a test that never finishes, so one forgotten resolve cannot freeze the whole suite. At the end it prints a short report.
Write createRunner(options) and formatReport(report) in async-runner.js.
functioncreateRunner(options) // options: { timeout } in ms, default 2000constr=createRunner({ timeout: 100 });r.it(name, fn, { timeout }); // the third argument is optionalr.beforeEach(fn);r.afterEach(fn);r.run(); // returns a promise of the reportfunctionformatReport(report) // returns a string
The report:
JAVASCRIPT
{ total, passed, failed, duration, // ms from the start of run() to its endresults: [ { name, status: "pass", duration }, { name, status: "fail", duration, error }, // error: the message, as text ],}
error is the thrown or rejected value's message when it is an Error, and String(value) otherwise. Durations are measured with Date.now().
A test (or hook) passes when fn returns, or when the promise or thenable it returns resolves. It fails when fn throws synchronously or its promise rejects. The run goes on after a failure.
Tests run one at a time, in the order they were added: a test starts only after the previous one, with its hooks, has finished.
A test that has not settled after its timeout fails with the error Timed out after <ms> ms, and the run moves on. A test that settles before the timeout passes or fails normally.
The timeout is it's timeout option when given (not undefined), else the runner's timeout, else 2000 ms.
When a step settles in time, its timeout timer is cleared: no timer is left waiting after run() finishes.
A test that settles after its timeout does not change its result, and a late rejection never becomes an unhandled rejection.
Before each test, the beforeEach hooks run in order, each awaited; after it, the afterEach hooks run in order, each awaited, even when the test failed or timed out. A failing beforeEach skips the remaining beforeEach hooks and the test body. A test keeps only its first error.
Each hook gets the same timeout as its test. A hook that times out fails the test with beforeEach timed out after <ms> ms or afterEach timed out after <ms> ms.
A test's duration runs from the start of its first beforeEach to the end of its last afterEach.
Calling run() while a run is in progress returns a promise that rejects with an Error with the message The runner is already running. Once the run has finished, run() works again.
A timeout (in createRunner or in it) that is not a positive, finite number throws a RangeError with the message timeout must be a positive number. Only a missing or undefined timeout means "use the default"; null is a value given, and is refused like 0.
it, beforeEach and afterEach throw a TypeError with the message <method> expects a function (for example it expects a function) when fn is not a function.
formatReport writes PASS <name> (<duration> ms) or FAIL <name> (<duration> ms) for each result, puts the error on the next line after two spaces, and ends with <total> tests: <passed> passed, <failed> failed (<duration> ms), with test singular when the total is 1. Lines are joined with "\n", with no newline at the end.
constr=createRunner();r.it("slow upload", () =>upload(), { timeout: 5000 }); // its own timeoutr.it("sync throw", () => { thrownewError("bad"); }); // fails, the run goes on
JAVASCRIPT
createRunner({ timeout: 0 }); // throws RangeError: timeout must be a positive numbercreateRunner({ timeout: null }); // throws the same RangeError; only a missing timeout means 2000constr=createRunner();r.run(); r.run(); // the second rejects: The runner is already running