Skip to content

Commit d8bfdb6

Browse files
ciampoMamaduka
andauthored
Docs: Update deprecation testing guidance for shared reset (#83989)
Co-authored-by: ciampo <mciampini@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
1 parent 71c69de commit d8bfdb6

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

‎packages/deprecated/README.md‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ _Type_
7373

7474
## Unit testing deprecations
7575

76-
Each deprecation message is logged only once per loaded instance of this package. The exported `logged` object tracks the full message text, so an earlier test can prevent a later test from observing the same warning. Resetting a console spy does not clear this state.
76+
Each deprecation message is logged only once per loaded instance of this package. The exported `logged` object tracks the full message text. Resetting a console spy does not clear this state.
7777

78-
For a suite that exercises a deprecated feature, mark its message as logged in `beforeEach` to suppress it during tests of other behavior. Delete that entry before testing the warning itself, and clean it up in `afterEach` so tests do not depend on their execution order. Limit cleanup to the messages the suite uses.
78+
Gutenberg's internal Vitest setup clears `logged` before each test in Node, jsdom, and Browser Mode. Suites do not need their own deprecation-history cleanup. If every test that triggers a deprecation asserts the warning, no interaction with `logged` is needed. A message still logs only once within a test. See [Expected console calls](/docs/contributors/code/testing-overview.md#expected-console-calls).
79+
80+
For a suite that exercises a deprecated feature, mark its message as logged in `beforeEach` to suppress it during tests of other behavior. Delete that entry before testing the warning itself.
7981

8082
This example uses Vitest and the `toHaveWarnedWith` matcher provided by Gutenberg's test setup:
8183

8284
```js
83-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
85+
import { beforeEach, describe, expect, it } from 'vitest';
8486
import deprecated, { logged } from '@wordpress/deprecated';
8587

8688
const DEPRECATION_MESSAGE = 'legacyFeature is deprecated.';
@@ -90,10 +92,6 @@ describe( 'legacyFeature', () => {
9092
logged[ DEPRECATION_MESSAGE ] = true;
9193
} );
9294

93-
afterEach( () => {
94-
delete logged[ DEPRECATION_MESSAGE ];
95-
} );
96-
9795
it( 'warns that the feature is deprecated', () => {
9896
delete logged[ DEPRECATION_MESSAGE ];
9997
deprecated( 'legacyFeature' );
@@ -105,7 +103,7 @@ describe( 'legacyFeature', () => {
105103

106104
Setting a `logged` entry also prevents the `deprecated` action from firing. Leave the entry absent when testing that action.
107105

108-
If every test that triggers a deprecation already asserts the warning, delete the message's entry in both `beforeEach` and `afterEach` instead of marking it as logged. This keeps the warning assertions independent without suppressing them.
106+
If your test setup does not clear `logged`, an earlier test can prevent a later test from observing the same warning. Delete the entries for messages your suite uses in `beforeEach` and `afterEach` to keep tests independent. For tests that suppress a warning, set its entry in `beforeEach` instead of deleting it. Limit cleanup to the messages the suite uses.
109107

110108
## Contributing to this package
111109

0 commit comments

Comments
 (0)