Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { afterEach, describe, expect, it } from 'vitest';
import { logged } from '@wordpress/deprecated';
import { describe, expect, it } from 'vitest';
import {
getComputedFluidTypographyValue,
getTypographyValueAndUnit,
} from '../fluid-utils';

describe( 'getComputedFluidTypographyValue()', () => {
afterEach( () => {
for ( const key in logged ) {
delete logged[ key ];
}
} );
it( 'should return `null` when given a font size is not a support value+unit', () => {
const fluidTypographyValues = getComputedFluidTypographyValue( {
fontSize:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { afterEach, describe, expect, it } from 'vitest';
import { logged } from '@wordpress/deprecated';
import { describe, expect, it } from 'vitest';
import { getFontSizeClass } from '../utils';

describe( 'getFontSizeClass()', () => {
afterEach( () => {
for ( const key in logged ) {
delete logged[ key ];
}
} );

it( 'Should return the correct font size class when given a string', () => {
const fontSizeClass = getFontSizeClass( '14px' );
expect( fontSizeClass ).toBe( 'has-14-px-font-size' );
Expand Down
26 changes: 22 additions & 4 deletions packages/block-editor/src/store/test/selectors.jsdom.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5317,6 +5317,17 @@ describe( 'getInserterItems with core blocks prioritization', () => {
} );

describe( '__unstableGetClientIdWithClientIdsTree', () => {
const DEPRECATION_MESSAGE =
"wp.data.select( 'core/block-editor' ).__unstableGetClientIdWithClientIdsTree is deprecated since version 6.3 and will be removed in version 6.5.";

beforeEach( () => {
delete logged[ DEPRECATION_MESSAGE ];
} );

afterEach( () => {
delete logged[ DEPRECATION_MESSAGE ];
} );

it( "should return a stripped down block object containing only its client ID and its inner blocks' client IDs", () => {
const state = {
blocks: {
Expand All @@ -5342,12 +5353,19 @@ describe( '__unstableGetClientIdWithClientIdsTree', () => {
{ clientId: 'baz', innerBlocks: [] },
],
} );
expect( console ).toHaveWarned();
expect( console ).toHaveWarnedWith( DEPRECATION_MESSAGE );
} );
} );
describe( '__unstableGetClientIdsTree', () => {
const DEPRECATION_MESSAGE =
"wp.data.select( 'core/block-editor' ).__unstableGetClientIdsTree is deprecated since version 6.3 and will be removed in version 6.5.";

beforeEach( () => {
delete logged[ DEPRECATION_MESSAGE ];
} );

afterEach( () => {
Object.keys( logged ).forEach( ( key ) => delete logged[ key ] );
delete logged[ DEPRECATION_MESSAGE ];
} );

it( "should return the full content tree starting from the given root, consisting of stripped down block object containing only its client ID and its inner blocks' client IDs", () => {
Expand All @@ -5370,7 +5388,7 @@ describe( '__unstableGetClientIdsTree', () => {
},
{ clientId: 'baz', innerBlocks: [] },
] );
expect( console ).toHaveWarned();
expect( console ).toHaveWarnedWith( DEPRECATION_MESSAGE );
} );

it( "should return the full content tree starting from the root, consisting of stripped down block object containing only its client ID and its inner blocks' client IDs", () => {
Expand Down Expand Up @@ -5398,7 +5416,7 @@ describe( '__unstableGetClientIdsTree', () => {
],
},
] );
expect( console ).toHaveWarned();
expect( console ).toHaveWarnedWith( DEPRECATION_MESSAGE );
} );
} );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { userEvent } from 'vitest/browser';
import { screen } from '@testing-library/react';
import { render } from 'vitest-browser-react';
Expand All @@ -25,7 +25,11 @@ function getOption( name: string ) {

describe( 'CircularOptionPicker', () => {
beforeEach( () => {
Object.keys( logged ).forEach( ( key ) => delete logged[ key ] );
delete logged[ AS_BUTTONS_DEPRECATION ];
} );

afterEach( () => {
delete logged[ AS_BUTTONS_DEPRECATION ];
} );

it( 'should preserve toggle-button semantics when an option is rendered without a picker', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
render,
screen,
Expand Down Expand Up @@ -110,13 +110,16 @@ function unescapeAndFormatSpaces( str: string ) {
return escaped.documentElement.textContent?.replace( / /g, nbsp ) ?? '';
}

const SHOW_HOW_TO_DEPRECATION =
'`__experimentalShowHowTo` prop in wp.components.FormTokenField is deprecated since version 7.1. Please use `help` prop instead. Note: The `help` prop now defaults to the previous how-to text. Pass an empty string to hide it.';

describe( 'FormTokenField', () => {
beforeEach( () => {
delete logged[ SHOW_HOW_TO_DEPRECATION ];
} );

afterEach( () => {
// `@wordpress/deprecated` caches each warning message after the first
// log; reset it so multiple tests can assert the same deprecation.
for ( const key in logged ) {
delete logged[ key ];
}
delete logged[ SHOW_HOW_TO_DEPRECATION ];
} );

describe( 'basic usage', () => {
Expand Down Expand Up @@ -632,9 +635,7 @@ describe( 'FormTokenField', () => {
<FormTokenFieldWithState __experimentalShowHowTo={ false } />
);

expect( console ).toHaveWarnedWith(
'`__experimentalShowHowTo` prop in wp.components.FormTokenField is deprecated since version 7.1. Please use `help` prop instead. Note: The `help` prop now defaults to the previous how-to text. Pass an empty string to hide it.'
);
expect( console ).toHaveWarnedWith( SHOW_HOW_TO_DEPRECATION );

expect(
screen.queryByText( 'Separate with commas or the Enter key.' )
Expand All @@ -652,7 +653,7 @@ describe( 'FormTokenField', () => {
/>
);

expect( console ).toHaveWarned();
expect( console ).toHaveWarnedWith( SHOW_HOW_TO_DEPRECATION );
expect(
screen.getByRole( 'combobox' )
).toHaveAccessibleDescription( 'Help text' );
Expand Down
17 changes: 10 additions & 7 deletions packages/compose/src/higher-order/pure/test/index.jsdom.test.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { afterEach, describe, expect, it } from 'vitest';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { render, screen } from '@testing-library/react';
import { logged } from '@wordpress/deprecated';
import pure from '../';

const DEPRECATION_MESSAGE =
'wp.compose.pure is deprecated since version 7.1. Please use Use `memo` or `PureComponent` instead instead.';

describe( 'pure', () => {
beforeEach( () => {
delete logged[ DEPRECATION_MESSAGE ];
} );

afterEach( () => {
for ( const key in logged ) {
delete logged[ key ];
}
delete logged[ DEPRECATION_MESSAGE ];
} );

it( 'wraps a component and logs a deprecation warning', () => {
const MyComp = pure( () => <p data-testid="content">content</p> );

render( <MyComp /> );

expect( console ).toHaveWarnedWith(
'wp.compose.pure is deprecated since version 7.1. Please use Use `memo` or `PureComponent` instead instead.'
);
expect( console ).toHaveWarnedWith( DEPRECATION_MESSAGE );
expect( screen.getByTestId( 'content' ) ).toHaveTextContent(
'content'
);
Expand Down
11 changes: 10 additions & 1 deletion packages/data/src/test/registry.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { logged } from '@wordpress/deprecated';
import { createRegistry } from '../registry';
import { createRegistrySelector } from '../factory';
import createReduxStore from '../redux-store';
import coreDataStore from '../store';

const REGISTER_GENERIC_STORE_DEPRECATION =
'wp.data.registerGenericStore is deprecated since version 5.9. Please use wp.data.register( storeDescriptor ) instead.';

describe( 'createRegistry', () => {
let registry;

Expand All @@ -27,6 +31,7 @@ describe( 'createRegistry', () => {

beforeEach( () => {
vi.useFakeTimers();
logged[ REGISTER_GENERIC_STORE_DEPRECATION ] = true;
registry = createRegistry();
} );

Expand All @@ -35,6 +40,7 @@ describe( 'createRegistry', () => {
while ( ( unsubscribe = unsubscribes.shift() ) ) {
unsubscribe();
}
delete logged[ REGISTER_GENERIC_STORE_DEPRECATION ];
vi.useRealTimers();
} );

Expand All @@ -50,6 +56,7 @@ describe( 'createRegistry', () => {
} );

it( 'should throw if not all required config elements are present', () => {
delete logged[ REGISTER_GENERIC_STORE_DEPRECATION ];
expect( () =>
registry.registerGenericStore( 'grocer', {} )
).toThrow();
Expand All @@ -65,7 +72,9 @@ describe( 'createRegistry', () => {
subscribe,
} )
).toThrow();
expect( console ).toHaveWarned();
expect( console ).toHaveWarnedWith(
REGISTER_GENERIC_STORE_DEPRECATION
);
} );

describe( 'getSelectors', () => {
Expand Down
36 changes: 36 additions & 0 deletions packages/deprecated/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,42 @@ _Type_

<!-- END TOKEN(Autogenerated API docs) -->

## Unit testing deprecations

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mamaduka hope this looks good!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 🎉

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll actually apply this guidance elsewhere in the repo in this PR, while I'm at it.


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.

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.

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

```js
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import deprecated, { logged } from '@wordpress/deprecated';

const DEPRECATION_MESSAGE = 'legacyFeature is deprecated.';

describe( 'legacyFeature', () => {
beforeEach( () => {
logged[ DEPRECATION_MESSAGE ] = true;
} );

afterEach( () => {
delete logged[ DEPRECATION_MESSAGE ];
} );

it( 'warns that the feature is deprecated', () => {
delete logged[ DEPRECATION_MESSAGE ];
deprecated( 'legacyFeature' );

expect( console ).toHaveWarnedWith( DEPRECATION_MESSAGE );
} );
} );
```

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

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.

## Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
Expand Down
14 changes: 13 additions & 1 deletion packages/interface/src/store/test/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import type { DataRegistry } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as interfaceStore } from '../';

const DEPRECATION_MESSAGES = [
"dispatch( 'core/interface' ).setFeatureDefaults is deprecated since version 6.0. Please use dispatch( 'core/preferences' ).setDefaults instead.",
"dispatch( 'core/interface' ).setFeatureValue is deprecated since version 6.0. Please use dispatch( 'core/preferences' ).set instead.",
"dispatch( 'core/interface' ).toggleFeature is deprecated since version 6.0. Please use dispatch( 'core/preferences' ).toggle instead.",
"select( 'core/interface' ).isFeatureActive( scope, featureName ) is deprecated since version 6.0. Please use select( 'core/preferences' ).get( scope, featureName ) instead.",
];

function createRegistryWithStores() {
// Create a registry and register used stores.
const registry = createRegistry();
Expand All @@ -15,10 +22,15 @@ function createRegistryWithStores() {
describe( 'actions', () => {
let registry: DataRegistry;
beforeEach( () => {
for ( const message of DEPRECATION_MESSAGES ) {
delete logged[ message ];
}
registry = createRegistryWithStores();
} );
afterEach( () => {
Object.keys( logged ).forEach( ( key ) => delete logged[ key ] );
for ( const message of DEPRECATION_MESSAGES ) {
delete logged[ message ];
}
} );

describe( 'enableComplementaryArea', () => {
Expand Down
Loading