[{"id":"jestintermediate-001","courseId":"jest-intermediate","domain":"test-structure","topic":"Describe handlers run before any test","prompt":"In a test file, Jest runs the code directly inside each describe callback just before the tests of that block, so describe-level code is interleaved with the tests.","hint":"Think about when Jest reads a describe callback compared with when it runs the tests.","explain":"Jest reads a test file in two passes: first it evaluates every describe callback to register the tests, then it runs those tests. Where code sits in the file decides when it runs relative to the tests.","source":"https://jestjs.io/docs/setup-teardown","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Jest runs every describe handler in the file before any test starts, so describe code does not wait for the tests."},{"id":"b","text":"False","why":"All describe handlers run first, so setup written there is not interleaved with the tests; that is why setup belongs in before and after hooks."}],"answer":"b","explanation":"Jest executes all describe handlers in a test file before it executes any of the actual tests, so setup and teardown belong in before* and after* handlers.","concept":"Jest executes all describe handlers in a test file before it executes any of the actual tests, so setup and teardown belong in before* and after* handlers.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Code in a describe callback runs before any test in the file, not between them."]},{"id":"jestintermediate-002","courseId":"jest-intermediate","domain":"test-structure","topic":"Scope of hooks inside describe blocks","prompt":"A file has a beforeEach and an afterEach at the top level, and the same pair inside a describe block. Put these steps, for one test inside that block, in the order Jest runs them.","hint":"Work out which hooks wrap the test and which sit closest to it.","explain":"Hooks in a file are layered by the blocks they sit in, and a nested block's layer sits inside its enclosing block's layer. Consider which layers a single run enters first and which it leaves last.","source":"https://jestjs.io/docs/setup-teardown","interaction":{"type":"sort","items":[{"text":"Outer beforeEach","why":"The top-level hook applies to every test in the file, so it starts the run for this test."},{"text":"Inner beforeEach","why":"The describe block's hook comes after the top-level one, once the outer setup has finished."},{"text":"The test body","why":"The test runs only after all the setup for its scopes has finished."},{"text":"Inner afterEach","why":"The nested block's teardown runs right after the test, before the enclosing teardown."},{"text":"Outer afterEach","why":"The top-level teardown runs last, after the nested block has cleaned up."}]},"options":[],"answer":"o:abcde","explanation":"Jest calls before and after hooks in declaration order; the example output lists the nested block's afterEach before the enclosing block's afterEach.","concept":"Jest calls before and after hooks in declaration order; the example output lists the nested block's afterEach before the enclosing block's afterEach.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Outer setup runs first and outer teardown runs last, with the test between them."]},{"id":"jestintermediate-003","courseId":"jest-intermediate","domain":"test-structure","topic":"Parameterized tests with test.each","prompt":"A discount function must be checked against six carts with the same assertion. Which Jest tool lets you write that test body once and pass the six cases in as data?","hint":"Look for a way to reuse one test body across several data sets.","explain":"A check that repeats across many inputs can keep its body in one place, with a table of values fed into it. Hooks are a different tool and do not create separate cases.","source":"https://jestjs.io/docs/api","interaction":{"type":"single"},"options":[{"id":"a","text":"test.each","why":"It writes the test once and passes the data in, so one body covers every input set."},{"id":"b","text":"test.only","whyNot":"It runs a single test and leaves the rest out, so it does not cover several inputs."},{"id":"c","text":"test.todo","whyNot":"It marks a test as a placeholder with no body, so it checks no inputs at all."},{"id":"d","text":"beforeAll with an array of inputs","whyNot":"beforeAll runs once before the tests; it does not create a separate test for each input."}],"answer":"a","explanation":"test.each allows you to write the test once and pass data in, so one test body is reused across several rows of input.","concept":"test.each allows you to write the test once and pass data in, so one test body is reused across several rows of input.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Reach for a table-driven test when only the inputs change between cases."]},{"id":"jestintermediate-004","courseId":"jest-intermediate","domain":"test-structure","topic":"Parameterized suites with describe.each","prompt":"A suite uses describe.each over the row { a: 2, b: 3 }, with the title shown in the code. What name does Jest show for that suite?","hint":"Look at how the suite title is built from each row.","explain":"A single suite can run over many data rows, and its heading can pull values from each row. Each row gets its own heading, so the report tells the rows apart.","source":"https://jestjs.io/docs/api","interaction":{"type":"single"},"options":[{"id":"a","text":"sum of 2 and 3","why":"Each $variable in the title is replaced with that property of the row object."},{"id":"b","text":"sum of $a and $b","whyNot":"The placeholders are filled in from each row, so the raw $ names do not appear in the title."},{"id":"c","text":"sum of a=2 and b=3","whyNot":"Jest injects only the values into the title, not the key names."},{"id":"d","text":"sum of [object Object]","whyNot":"Jest reads each $variable as a property of the row, so the whole object is never printed."}],"answer":"a","explanation":"describe.each titles can inject properties of each row object with $variable, so every row gets its own suite name.","concept":"describe.each titles can inject properties of each row object with $variable, so every row gets its own suite name.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"describe.each([\n  { a: 2, b: 3 },\n])('sum of $a and $b', ({ a, b }) => {\n  test('adds', () => {\n    expect(a + b).toBe(5);\n  });\n});","takeaways":["Use $variable in describe.each titles to tell the rows apart in the report."]},{"id":"jestintermediate-005","courseId":"jest-intermediate","domain":"test-structure","topic":"Skipping a whole suite with describe.skip","prompt":"With describe.skip, none of the tests inside the block run, but code written directly in the describe callback still executes.","hint":"Consider what skip is meant to switch off.","explain":"A skip marker chooses which tests get executed. Jest first reads a file to define its blocks, and running the tests is a separate step from that reading.","source":"https://jestjs.io/docs/api","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"Skipping leaves the describe block itself running, so its direct code runs even though its tests do not."},{"id":"b","text":"False","whyNot":"The describe block still runs under describe.skip; only the tests inside it are left out."}],"answer":"a","explanation":"describe.skip leaves the describe block running, so its tests are skipped while direct code in the block still executes; setup meant to be skipped belongs in a hook.","concept":"describe.skip leaves the describe block running, so its tests are skipped while direct code in the block still executes; setup meant to be skipped belongs in a hook.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Code in a skipped describe still runs; move setup you want skipped into a hook."]}]