[{"id":"cypressintermediate-001","courseId":"cypress-intermediate","domain":"test-structure","topic":"Test isolation: what carries between tests","prompt":"Test 1 in a spec logs in through a cookie-based app. Test 2 in the same spec expects that login cookie to still be there. Test isolation is enabled. What does test 2 see?","hint":"Think about what Cypress resets in the browser before each test begins.","explain":"Each test in a spec begins with a browser context that the runner prepares for it. How much of the browser's stored state from an earlier test reaches the next one is decided by a setting.","source":"https://docs.cypress.io/app/core-concepts/test-isolation","interaction":{"type":"single"},"options":[{"id":"a","text":"The login cookie is gone, because Cypress clears cookies before the test starts and the test must log in again.","why":"With test isolation enabled, Cypress clears cookies in all domains before each test, so a login from test 1 does not carry over."},{"id":"b","text":"The login cookie is still there, because Cypress only resets the page between tests.","whyNot":"The page is reset, but cookies are cleared too; isolation does not keep cookies between tests."},{"id":"c","text":"The login cookie is still there, because isolation only applies to the first test in a spec.","whyNot":"Isolation applies before every test in the spec, not only the first one."},{"id":"d","text":"The login cookie is still there, but only if test 1 ran before test 2.","whyNot":"Run order does not decide it: cookies are cleared before each test regardless of which test ran earlier."}],"answer":"a","explanation":"With test isolation enabled, Cypress resets the browser context before each test, including clearing cookies in all domains, localStorage and sessionStorage.","concept":"With test isolation enabled, Cypress resets the browser context before each test, including clearing cookies in all domains, localStorage and sessionStorage.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["With isolation on, a login made in one test does not survive into the next; tests that need it must log in again."]},{"id":"cypressintermediate-002","courseId":"cypress-intermediate","domain":"test-structure","topic":"cy.session() reuse by id","prompt":"A spec's first test calls cy.session() with the id 'user' to log in. A later test in the same spec calls cy.session() with the same id. What happens on that later call?","hint":"Think about what a cache keeps, and how long the spec keeps it.","explain":"Logging in is slow, so cy.session() can keep the browser state a login produces, filed under a name you choose. The first use of a name has to build that state, and later uses depend on what was kept.","source":"https://docs.cypress.io/api/commands/session","interaction":{"type":"single"},"options":[{"id":"a","text":"Its setup function is skipped, and the saved session is restored and validated.","why":"A repeat call with a known id bypasses setup and just restores and validates the cached session data."},{"id":"b","text":"Its setup function runs again and replaces the saved session with a new one.","whyNot":"Setup runs only when no session for that id is cached yet; a known id does not trigger it again."},{"id":"c","text":"The call throws an error, because that id was already used earlier in the spec.","whyNot":"Reusing an id is how a session is shared; the call does not fail for that reason."},{"id":"d","text":"The saved session is used only by the test that created it, so setup runs again.","whyNot":"A session for an id is cached for the duration of the spec file, across its tests."}],"answer":"a","explanation":"cy.session() caches a session per id for the duration of the spec file; a later call with a cached id bypasses setup and just restores and validates the saved data.","concept":"cy.session() caches a session per id for the duration of the spec file; a later call with a cached id bypasses setup and just restores and validates the saved data.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Reusing one cy.session() id across tests avoids repeating a slow login."]},{"id":"cypressintermediate-003","courseId":"cypress-intermediate","domain":"test-structure","topic":"cy.session() validate failure","prompt":"A cy.session() call restores a saved login from an earlier test. Its validate function then checks the page and finds the user is logged out. What does Cypress do next?","hint":"Consider whether a restored session still counts as good once its check fails.","explain":"A command that reuses saved browser state checks that state before trusting it. The response to a failed check depends on when the check runs, so the same failure can lead to different outcomes.","source":"https://docs.cypress.io/api/commands/session","interaction":{"type":"single"},"options":[{"id":"a","text":"It runs the setup function again to build a fresh session.","why":"If validation fails after a restore, the cached session is treated as invalid and setup re-runs."},{"id":"b","text":"The test fails at once, without running setup again.","whyNot":"An immediate failure happens when validation fails right after setup, not after a restore."},{"id":"c","text":"It keeps the restored session and logs a warning, so the test continues.","whyNot":"Cypress does not carry on with a session that failed validation; it rebuilds it."},{"id":"d","text":"It skips the rest of the spec file and reports the restore as passed.","whyNot":"A failed validation does not end the spec; the session is rebuilt instead."}],"answer":"a","explanation":"If validation fails immediately after setup, the test fails; if it fails after restoring a cached session, the session is considered invalid and setup runs again.","concept":"If validation fails immediately after setup, the test fails; if it fails after restoring a cached session, the session is considered invalid and setup runs again.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["The validate function decides whether a restored session is reused or rebuilt."]},{"id":"cypressintermediate-004","courseId":"cypress-intermediate","domain":"test-structure","topic":"testIsolation false inside a describe block","prompt":"A describe block sets testIsolation to false. Its before hook logs in through the UI. The block has three tests. What state do the tests see when they run?","hint":"Think about what a suite-level setting changes between tests, and what a before hook leaves behind.","explain":"Cypress normally resets the browser context before each test. A setting on one describe block can change how much of that reset applies inside the block. Consider what a test inherits from the tests before it.","source":"https://docs.cypress.io/app/core-concepts/test-isolation","interaction":{"type":"single"},"options":[{"id":"a","text":"The login persists, because cookies and storage are kept across the tests in that suite.","why":"With testIsolation false, the page is not cleared and cookies and storage stay available across the suite's tests."},{"id":"b","text":"Each test sees the login only if its own beforeEach hook repeats it.","whyNot":"The before hook runs once for the suite, and with isolation off its state simply stays in place."},{"id":"c","text":"The login is wiped before each test, as it would be with isolation on.","whyNot":"That is the isolation-on behaviour; turning it off for the block stops the wipe."},{"id":"d","text":"Only the first test sees the login, because the page resets after that test.","whyNot":"With isolation off the page is not cleared between tests, so the login is not lost after the first one."}],"answer":"a","explanation":"With testIsolation false in a describe block, the page is not cleared and cookies, localStorage and sessionStorage stay available across the tests, so state set in a before hook persists.","concept":"With testIsolation false in a describe block, the page is not cleared and cookies, localStorage and sessionStorage stay available across the tests, so state set in a before hook persists.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Turning isolation off saves time but lets tests affect each other; keep it to suites that really share state."]},{"id":"cypressintermediate-005","courseId":"cypress-intermediate","domain":"test-structure","topic":"Cleanup hooks versus built-in reset","prompt":"A team adds an afterEach hook that clears cookies and localStorage after every test in an E2E spec. Test isolation is enabled. What is the main problem with this hook?","hint":"Compare what the runner does between tests when isolation is on with what this hook does.","explain":"A spec can leave state behind for later tests, and a team can add code to remove it. Cleanup is only worth writing when one test's leftovers would actually reach another; check what the runner handles on its own first.","source":"https://docs.cypress.io/app/core-concepts/best-practices","interaction":{"type":"single"},"options":[{"id":"a","text":"Cypress already clears that state before each test, so the hook repeats work it does anyway.","why":"With isolation enabled, cookies and storage are cleared before every test, so clearing them again after each test is redundant."},{"id":"b","text":"Cypress never clears cookies, so the hook is the only thing keeping tests apart.","whyNot":"With isolation enabled, Cypress clears cookies before each test, so the hook is not what keeps them apart."},{"id":"c","text":"The hook is required, since cleanup in afterEach is the only reliable way to reset state.","whyNot":"Cypress already resets state before each test, so cleanup after a test is not the only reset."},{"id":"d","text":"The hook must clear the page too, because Cypress never resets the DOM between tests.","whyNot":"With isolation enabled, Cypress resets the DOM before each test by visiting about:blank."}],"answer":"a","explanation":"With test isolation enabled, Cypress clears the DOM, cookies, localStorage and sessionStorage before each test, so cleanup code that repeats this is usually unnecessary.","concept":"With test isolation enabled, Cypress clears the DOM, cookies, localStorage and sessionStorage before each test, so cleanup code that repeats this is usually unnecessary.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Before writing cleanup, check whether isolation already resets that state; only leftovers that reach later tests need it."]}]