MediumPro challengePythonJavaScriptTypeScript
Implement Test Runner (describe/it)
TestingFunctions
Implement solve(plan), a tiny describe / it runner. The starter code provides the hooks and test bodies in HOOKS and TESTS; the plan refers to them by name:
{
suites: [
{
name: 'Math',
beforeEach: 'incrementCounter', // key of HOOKS, or null
afterEach: null, // key of HOOKS, or null
tests: [
{ name: 'first', fn: 'counterIsOne' }, // key of TESTS
{ name: 'second', fn: 'counterIsTwo' },
],
},
],
}Rules:
- Each suite gets a fresh, empty context object
ctx, shared by all of its hooks and tests. - For each test: run
beforeEach(ctx), then the test bodyfn(ctx), thenafterEach(ctx). - A test passes only if its body returns exactly
true. Otherwise it fails with anerror:
- beforeEach threw → its message (the body is skipped, afterEach still runs)
- the body threw → its message
- the body returned anything but true → 'Test did not return true'
- afterEach threw after a passing body → its message
- Return
{ passed, failed, results }, whereresultslists{ suite, test, pass, error }in execution order (errorisnullfor a passing test).
Sample tests
Test #1afterEach runs after each test
Input: [{"suites":[{"name":"After","tests":[{"fn":"alwaysTrue","name":"first"},{"fn":"wasCleaned","name":"second"}],"afterEach":"markCleaned","beforeEach":null}]}]
Output: {"failed":0,"passed":2,"results":[{"pass":true,"test":"first","error":null,"suite":"After"},{"pass":true,"test":"second","error":null,"suite":"After"}]}
Test #2beforeEach runs before every test on a shared context
Input: [{"suites":[{"name":"Math","tests":[{"fn":"counterIsOne","name":"first"},{"fn":"counterIsTwo","name":"second"}],"afterEach":null,"beforeEach":"incrementCounter"}]}]
Output: {"failed":0,"passed":2,"results":[{"pass":true,"test":"first","error":null,"suite":"Math"},{"pass":true,"test":"second","error":null,"suite":"Math"}]}
Test #3a body returning false fails
Input: [{"suites":[{"name":"Fail","tests":[{"fn":"onePlusOneIsThree","name":"wrong"}],"afterEach":null,"beforeEach":null}]}]
Output: {"failed":1,"passed":0,"results":[{"pass":false,"test":"wrong","error":"Test did not return true","suite":"Fail"}]}
Test #4a throwing body fails with its message
Input: [{"suites":[{"name":"Throws","tests":[{"fn":"throwsBad","name":"error"}],"afterEach":null,"beforeEach":null}]}]
Output: {"failed":1,"passed":0,"results":[{"pass":false,"test":"error","error":"bad","suite":"Throws"}]}