From 361cf5ddebf625b355b88757a84731b1c0f1b755 Mon Sep 17 00:00:00 2001 From: "Bonaventure C. J. Ugwu" <73999585+BonaventureCJ@users.noreply.github.com> Date: Mon, 8 Jun 2026 06:54:47 +0100 Subject: [PATCH] docs(guide): clarify beforeEach state isolation (#10512) --- docs/guide/learn/setup-teardown.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/guide/learn/setup-teardown.md b/docs/guide/learn/setup-teardown.md index 2eacac656..2985ec4a5 100644 --- a/docs/guide/learn/setup-teardown.md +++ b/docs/guide/learn/setup-teardown.md @@ -33,15 +33,20 @@ test('items starts with 3 fruits', () => { expect(items).toHaveLength(3) }) +test('can remove an item', () => { + items.pop() + expect(items).toHaveLength(2) +}) + test('can add an item', () => { items.push('date') expect(items).toHaveLength(4) - // afterEach will reset items for the next test, - // so this mutation won't leak into other tests + // beforeEach reset the array to 3 items before this test ran, + // proving that mutations from the previous test do not leak. }) ``` -Without these hooks, the second test's `push` would affect any test that runs after it, which is a classic source of flaky tests. The hooks guarantee clean state for every test. +Without these hooks, mutations like `pop` or `push` from earlier tests would affect subsequent ones, which is a classic source of flaky tests, while the hooks guarantee clean state for every test. ## One-Time Setup -- 2.51.2