[{"id":"jestadvanced-001","courseId":"jest-advanced","domain":"test-structure","topic":"Nested after hooks run first","prompt":"A file has an afterEach inside describe('matching cities to foods'). A test outside that block finishes. Does that afterEach run after it?","hint":"Ask which tests a hook declared inside a describe block is attached to.","explain":"A before or after hook can belong to the whole file or to one group of tests. Consider which tests a hook covers when it is declared inside a group.","source":"https://jestjs.io/docs/setup-teardown","interaction":{"type":"single"},"options":[{"id":"a","text":"No. It runs only for tests inside that describe block.","why":"Hooks declared inside a describe block apply only to the tests within that block."},{"id":"b","text":"Yes. Every afterEach in a file runs after every test.","whyNot":"Only the top-level hooks apply to every test in a file; nested hooks stay in their block."},{"id":"c","text":"Yes, but only when the outer block declares no afterEach.","whyNot":"A nested hook is confined to its own block, whatever hooks the outer scope has."},{"id":"d","text":"Yes, once, after the first test in the whole file.","whyNot":"Nested hooks do not run once per file; they run around each test in their block."}],"answer":"a","explanation":"Top-level before* and after* hooks apply to every test in a file, while hooks declared inside a describe block apply only to the tests within that block.","concept":"Top-level before* and after* hooks apply to every test in a file, while hooks declared inside a describe block apply only to the tests within that block.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Declare a hook in the describe block whose tests need it; top-level hooks reach every test in the file."]},{"id":"jestadvanced-002","courseId":"jest-advanced","domain":"test-structure","topic":"Serial execution in collection order by default","prompt":"A test file declares, at its top level and in this order: beforeEach connection setup, beforeEach database setup, afterEach database teardown, afterEach connection teardown. It has one test. Put the steps of one test run in the order Jest runs them.","hint":"Work out which resource depends on which before you order the steps.","explain":"Some setup steps prepare resources that other steps depend on, and cleanup steps release them. The body of a run sits between these layers, and the order that hooks are declared in is part of the picture.","source":"https://jestjs.io/docs/setup-teardown","interaction":{"type":"sort","items":[{"text":"connection setup","why":"The first beforeEach runs first, so the connection exists before anything uses it."},{"text":"database setup","why":"The second beforeEach runs next, because the database needs the connection to be ready."},{"text":"test 1","why":"The body runs only after every beforeEach has finished."},{"text":"database teardown","why":"The afterEach declared first runs first, releasing the database before its connection."},{"text":"connection teardown","why":"The afterEach declared last runs last, closing the connection after the database is released."}]},"options":[],"answer":"o:abcde","explanation":"Jest calls before* and after* hooks in the order of declaration, so setup and teardown that depend on each other can be declared in matching layers.","concept":"Jest calls before* and after* hooks in the order of declaration, so setup and teardown that depend on each other can be declared in matching layers.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Declare dependent setup and teardown in matching order; Jest runs after hooks in declaration order too."]},{"id":"jestadvanced-003","courseId":"jest-advanced","domain":"test-structure","topic":"Retrying failed tests with jest.retryTimes","prompt":"A test is flaky. You call jest.retryTimes(3) at the top of its file and do not pass any other options. When does Jest retry the failed test?","hint":"Look at what the retryImmediately option changes about timing.","explain":"A failed test can be retried right after it fails, or held back until later in the run. Which one happens depends on an option you pass, and the default is not the immediate one.","source":"https://jestjs.io/docs/jest-object","interaction":{"type":"single"},"options":[{"id":"a","text":"After Jest finishes running all other tests in that file","why":"Without retryImmediately, the retry waits until the rest of the file's tests have run."},{"id":"b","text":"Immediately after the failure, before the next test starts","whyNot":"That timing needs retryImmediately set to true; this call does not set it."},{"id":"c","text":"Never, because retryTimes only retries tests inside a describe block","whyNot":"A top-level call is allowed; retryTimes can be declared at the top level of a file."},{"id":"d","text":"Only once, since the argument counts the test's total runs","whyNot":"The argument is the number of retries, so up to three retries can follow the first failure."}],"answer":"a","explanation":"Without retryImmediately, jest.retryTimes retries a failed test after Jest has finished running the other tests in the file; retryImmediately retries at once.","concept":"Without retryImmediately, jest.retryTimes retries a failed test after Jest has finished running the other tests in the file; retryImmediately retries at once.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["retryImmediately: true retries a failed test at once; the default waits until the rest of the file has run."]},{"id":"jestadvanced-004","courseId":"jest-advanced","domain":"test-structure","topic":"Expected failures with test.failing","prompt":"You mark a known bug with test.failing. Which TWO statements about how Jest reports that test are correct?","hint":"Think about what an error thrown inside the body does to the verdict.","explain":"A test marked to expect failure reverses the usual meaning of its outcome. Consider what the body must do for the test to count as a success, and what it means when the body finishes cleanly.","source":"https://jestjs.io/docs/api","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"A test.failing test passes when its body throws an error.","why":"An error thrown in the body is the failure the test expects, so it is scored as a pass."},{"id":"b","text":"A test.failing test fails when its body finishes without throwing.","why":"A clean finish means the expected failure did not happen, so the test is scored as a failure."},{"id":"c","text":"A failed expect inside a test.failing body still makes the test fail.","whyNot":"A failed expect throws, which is the outcome test.failing wants, so the test passes."},{"id":"d","text":"A test.failing body that throws is shown as a red failure.","whyNot":"A throwing body is scored as a pass, so it is not reported as a failure."},{"id":"e","text":"test.failing is available under the jasmine2 runner as well.","whyNot":"The API is only available with the default jest-circus runner."},{"id":"f","text":"test.failing needs a timeout argument, or it cannot tell pass from fail.","whyNot":"The timeout is optional and only sets how long the test may run before it is aborted."}],"answer":"ab","explanation":"test.failing inverts the outcome: a body that throws passes, and a body that finishes without throwing fails. It requires the default jest-circus runner.","concept":"test.failing inverts the outcome: a body that throws passes, and a body that finishes without throwing fails. It requires the default jest-circus runner.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Use test.failing for a known bug, then remove the modifier once the bug is fixed."]},{"id":"jestadvanced-005","courseId":"jest-advanced","domain":"test-structure","topic":"Concurrency cap with maxConcurrency","prompt":"A jest config uses a projects array, and one project entry sets several options. Which TWO of these options does Jest ignore when they are written inside that entry?","hint":"Think about which options govern the whole run instead of one project.","explain":"A projects entry can hold settings of its own, but some options describe the run as a whole. Consider which of these options would make sense to differ between projects.","source":"https://jestjs.io/docs/configuration","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"maxConcurrency","why":"It is a root-level option; written inside a project entry it is ignored, so it belongs in the root config."},{"id":"b","text":"bail","why":"It is a root-level option, so a value written inside a project entry is ignored."},{"id":"c","text":"testMatch","whyNot":"It is not on the root-only list, so a project entry's value is still used."},{"id":"d","text":"moduleNameMapper","whyNot":"It is not on the root-only list, so a project entry can set its own module mappings."},{"id":"e","text":"testTimeout","whyNot":"It is not on the root-only list, so a project entry's value still applies to its tests."},{"id":"f","text":"setupFilesAfterEnv","whyNot":"It is not on the root-only list, so a project entry can list its own setup files."}],"answer":"ab","explanation":"Options such as maxConcurrency and bail take effect only in the root config; set inside a project config they are ignored, while options not on that list still apply per project.","concept":"Options such as maxConcurrency and bail take effect only in the root config; set inside a project config they are ignored, while options not on that list still apply per project.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Keep run-wide limits such as concurrency and bail in the root config, not in a project entry."]}]