Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESLint: Enable jest/recommended for tests #47065

Merged
merged 4 commits into from Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -372,6 +372,7 @@ module.exports = {
extends: [
'plugin:jest-dom/recommended',
'plugin:testing-library/react',
'plugin:jest/recommended',
Copy link
Member Author

Choose a reason for hiding this comment

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

We're enabling the jest/recommended set of rules:

https://github.com/jest-community/eslint-plugin-jest#recommended

],
},
{
Expand Down
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -185,6 +185,7 @@
"eslint-import-resolver-node": "0.3.4",
"eslint-plugin-eslint-comments": "3.1.2",
"eslint-plugin-import": "2.25.2",
"eslint-plugin-jest": "27.2.1",
Copy link
Member Author

Choose a reason for hiding this comment

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

We're now relying on it directly, so adding it as a dependency. Using the latest version.

"eslint-plugin-jest-dom": "4.0.2",
"eslint-plugin-playwright": "0.8.0",
"eslint-plugin-ssr-friendly": "1.0.6",
Expand Down
Expand Up @@ -50,7 +50,7 @@ describe( 'BlockSelectionClearer component', () => {

fireEvent.mouseDown( screen.getByTestId( 'selection-clearer' ) );

expect( mockClearSelectedBlock ).toBeCalled();
expect( mockClearSelectedBlock ).toHaveBeenCalled();
Copy link
Member Author

Choose a reason for hiding this comment

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

} );

it( 'should clear the selected block when multiple blocks are selected', () => {
Expand All @@ -71,7 +71,7 @@ describe( 'BlockSelectionClearer component', () => {

fireEvent.mouseDown( screen.getByTestId( 'selection-clearer' ) );

expect( mockClearSelectedBlock ).toBeCalled();
expect( mockClearSelectedBlock ).toHaveBeenCalled();
} );

it( 'should not clear the block selection when no blocks are selected', () => {
Expand All @@ -89,7 +89,7 @@ describe( 'BlockSelectionClearer component', () => {

fireEvent.mouseDown( screen.getByTestId( 'selection-clearer' ) );

expect( mockClearSelectedBlock ).not.toBeCalled();
expect( mockClearSelectedBlock ).not.toHaveBeenCalled();
} );

it( 'should not clear the block selection when the feature is disabled', () => {
Expand All @@ -113,6 +113,6 @@ describe( 'BlockSelectionClearer component', () => {

fireEvent.mouseDown( screen.getByTestId( 'selection-clearer' ) );

expect( mockClearSelectedBlock ).not.toBeCalled();
expect( mockClearSelectedBlock ).not.toHaveBeenCalled();
} );
} );
39 changes: 15 additions & 24 deletions packages/block-editor/src/utils/test/parse-css-unit-to-px.js
Expand Up @@ -22,20 +22,17 @@ describe( 'getPxFromCssUnit', () => {
[ '40Q', '38px' ], // 40 Q should be 1 cm.
];

test.each( testData )( 'getPxFromCssUnit( %s )', ( unit, expected ) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

expect( getPxFromCssUnit( unit ) ).toBe( expected );
} );
test.each( testData )(
'test getPxFromCssUnit( %s )',
( unit, expected ) => {
expect( getPxFromCssUnit( unit ) ).toBe( expected );
}
);
test.each( testData )(
'test memoizedGetPxFromCssUnit( %s )',
'memoizedGetPxFromCssUnit( %s )',
( unit, expected ) => {
expect( memoizedGetPxFromCssUnit( unit ) ).toBe( expected );
}
);
test.each( testData )(
'test cached memoizedGetPxFromCssUnit( %s )',
'cached memoizedGetPxFromCssUnit( %s )',
( unit, expected ) => {
expect( memoizedGetPxFromCssUnit( unit ) ).toBe( expected );
}
Expand Down Expand Up @@ -63,22 +60,19 @@ describe( 'getPxFromCssUnit', () => {
[ '120%', '12px' ],
];

test.each( testData )( 'getPxFromCssUnit( %s )', ( unit, expected ) => {
expect( getPxFromCssUnit( unit, settings ) ).toBe( expected );
} );
test.each( testData )(
'test getPxFromCssUnit( %s )',
( unit, expected ) => {
expect( getPxFromCssUnit( unit, settings ) ).toBe( expected );
}
);
test.each( testData )(
'test memoizedGetPxFromCssUnit( %s )',
'memoizedGetPxFromCssUnit( %s )',
( unit, expected ) => {
expect( memoizedGetPxFromCssUnit( unit, settings ) ).toBe(
expected
);
}
);
test.each( testData )(
'test cached memoizedGetPxFromCssUnit( %s )',
'cached memoizedGetPxFromCssUnit( %s )',
( unit, expected ) => {
expect( memoizedGetPxFromCssUnit( unit, settings ) ).toBe(
expected
Expand Down Expand Up @@ -123,22 +117,19 @@ describe( 'getPxFromCssUnit', () => {
[ 'calc(12vw * 10px', null ], // Missing closing bracket.
];

test.each( testData )( 'getPxFromCssUnit( %s )', ( unit, expected ) => {
expect( getPxFromCssUnit( unit, settings ) ).toBe( expected );
} );
test.each( testData )(
'test getPxFromCssUnit( %s )',
( unit, expected ) => {
expect( getPxFromCssUnit( unit, settings ) ).toBe( expected );
}
);
test.each( testData )(
'test memoizedGetPxFromCssUnit( %s )',
'memoizedGetPxFromCssUnit( %s )',
( unit, expected ) => {
expect( memoizedGetPxFromCssUnit( unit, settings ) ).toBe(
expected
);
}
);
test.each( testData )(
'test cached memoizedGetPxFromCssUnit( %s )',
'cached memoizedGetPxFromCssUnit( %s )',
( unit, expected ) => {
expect( memoizedGetPxFromCssUnit( unit, settings ) ).toBe(
expected
Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/draggable/test/index.native.js
Expand Up @@ -59,7 +59,7 @@ describe( 'Draggable', () => {
{ state: State.ACTIVE },
] );

expect( onLongPress ).toBeCalledTimes( 1 );
expect( onLongPress ).toHaveBeenCalledTimes( 1 );
Copy link
Member Author

Choose a reason for hiding this comment

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

expect( onLongPress ).toHaveBeenCalledWith( triggerId );
} );

Expand Down Expand Up @@ -111,16 +111,16 @@ describe( 'Draggable', () => {
{ state: State.END },
] );

expect( onDragStart ).toBeCalledTimes( 1 );
expect( onDragStart ).toHaveBeenCalledTimes( 1 );
expect( onDragStart ).toHaveBeenCalledWith( {
id: triggerId,
x: touchEvents[ 0 ].x,
y: touchEvents[ 0 ].y,
} );
expect( onDragOver ).toBeCalledTimes( 2 );
expect( onDragOver ).toHaveBeenCalledTimes( 2 );
expect( onDragOver ).toHaveBeenNthCalledWith( 1, touchEvents[ 1 ] );
expect( onDragOver ).toHaveBeenNthCalledWith( 2, touchEvents[ 2 ] );
expect( onDragEnd ).toBeCalledTimes( 1 );
expect( onDragEnd ).toHaveBeenCalledTimes( 1 );
expect( onDragEnd ).toHaveBeenCalledWith( {
id: triggerId,
x: touchEvents[ 2 ].x,
Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/theme/test/index.tsx
Expand Up @@ -26,7 +26,7 @@ const MyThemableComponent = ( props: MyThemableComponentProps ) => {

describe( 'Theme', () => {
describe( 'accent color', () => {
it( 'it does not define the accent color (and its variations) as a CSS variable when the `accent` prop is undefined', () => {
it( 'does not define the accent color (and its variations) as a CSS variable when the `accent` prop is undefined', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

render(
<Theme data-testid="theme">
<MyThemableComponent>Inner</MyThemableComponent>
Expand All @@ -49,7 +49,7 @@ describe( 'Theme', () => {
} );
} );

it( 'it defines the accent color (and its variations) as a CSS variable', () => {
it( 'defines the accent color (and its variations) as a CSS variable', () => {
render(
<Theme accent="#123abc" data-testid="theme">
<MyThemableComponent>Inner</MyThemableComponent>
Expand All @@ -66,7 +66,7 @@ describe( 'Theme', () => {
} );

describe( 'background color', () => {
it( 'it does not define the background color (and its dependent colors) as a CSS variable when the `background` prop is undefined', () => {
it( 'does not define the background color (and its dependent colors) as a CSS variable when the `background` prop is undefined', () => {
render(
<Theme data-testid="theme">
<MyThemableComponent>Inner</MyThemableComponent>
Expand All @@ -91,7 +91,7 @@ describe( 'Theme', () => {
} );
} );

it( 'it defines the background color (and its dependent colors) as a CSS variable', () => {
it( 'defines the background color (and its dependent colors) as a CSS variable', () => {
render(
<Theme background="#ffffff" data-testid="theme">
<MyThemableComponent>Inner</MyThemableComponent>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/tools-panel/test/index.js
Expand Up @@ -582,7 +582,7 @@ describe( 'ToolsPanel', () => {
// registerPanelItem has still only been called once.
expect( context.registerPanelItem ).toHaveBeenCalledTimes( 1 );
// deregisterPanelItem is called, given that we have switched panels.
expect( context.deregisterPanelItem ).toBeCalledWith(
expect( context.deregisterPanelItem ).toHaveBeenCalledWith(
Copy link
Member Author

Choose a reason for hiding this comment

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

altControlProps.label
);

Expand Down
Expand Up @@ -29,7 +29,7 @@ describe( 'fetchUrlData', () => {
fetchUrlData( 'https://www.wordpress.org' )
).resolves.toEqual( data );

expect( apiFetch ).toBeCalledTimes( 1 );
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
Copy link
Member Author

Choose a reason for hiding this comment

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

} );

it( 'rejects with error upon fetch failure', async () => {
Expand All @@ -49,7 +49,7 @@ describe( 'fetchUrlData', () => {
method: 'POST',
} );

expect( apiFetch ).toBeCalledTimes( 1 );
expect( apiFetch ).toHaveBeenCalledTimes( 1 );

const argsPassedToFetchApi = apiFetch.mock.calls[ 0 ][ 0 ];

Expand All @@ -74,7 +74,7 @@ describe( 'fetchUrlData', () => {
apiFetch.mockReturnValueOnce( Promise.resolve( data ) );

await expect( fetchUrlData( targetUrl ) ).resolves.toEqual( data );
expect( apiFetch ).toBeCalledTimes( 1 );
expect( apiFetch ).toHaveBeenCalledTimes( 1 );

// Allow us to reassert on calls without it being polluted by first fetch
// but retains the mock implementation from earlier.
Expand All @@ -84,7 +84,7 @@ describe( 'fetchUrlData', () => {
await expect( fetchUrlData( targetUrl ) ).resolves.toEqual( data );

// Should now be in cache so no need to refetch from API.
expect( apiFetch ).toBeCalledTimes( 0 );
expect( apiFetch ).toHaveBeenCalledTimes( 0 );
} );

it( 'does not cache failed requests', async () => {
Expand All @@ -109,15 +109,15 @@ describe( 'fetchUrlData', () => {
// with a new fetch.
await expect( fetchUrlData( targetUrl ) ).resolves.toEqual( data );

expect( apiFetch ).toBeCalledTimes( 2 );
expect( apiFetch ).toHaveBeenCalledTimes( 2 );
} );
} );

describe( 'URL validation', () => {
it.each( [ '#internal-link' ] )(
'errors when an invalid URL is passed',
async ( targetUrl ) => {
expect( apiFetch ).toBeCalledTimes( 0 );
expect( apiFetch ).toHaveBeenCalledTimes( 0 );

await expect( fetchUrlData( targetUrl ) ).rejects.toEqual(
expect.stringContaining(
Expand All @@ -134,7 +134,7 @@ describe( 'fetchUrlData', () => {
] )(
'errors when a non-http protocol (%s) is passed as part of URL',
async ( targetUrl ) => {
expect( apiFetch ).toBeCalledTimes( 0 );
expect( apiFetch ).toHaveBeenCalledTimes( 0 );

await expect( fetchUrlData( targetUrl ) ).rejects.toEqual(
expect.stringContaining(
Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/components/with-dispatch/test/index.js
Expand Up @@ -78,7 +78,7 @@ describe( 'withDispatch', () => {

// Function value reference should not have changed in props update.
// The spy method is only called during inital render.
expect( ButtonSpy ).toBeCalledTimes( 1 );
expect( ButtonSpy ).toHaveBeenCalledTimes( 1 );
Copy link
Member Author

Choose a reason for hiding this comment

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


await user.click( screen.getByRole( 'button' ) );
expect( registry.select( 'counter' ).getCount() ).toBe( 2 );
Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/plugins/persistence/test/index.js
Expand Up @@ -29,7 +29,7 @@ describe( 'persistence', () => {
expect( () => {
const options = Object.freeze( { persist: true, reducer() {} } );
registry.registerStore( 'test', options );
} ).not.toThrowError( /object is not extensible/ );
} ).not.toThrow( /object is not extensible/ );
Copy link
Member Author

Choose a reason for hiding this comment

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

} );

it( 'should load a persisted value as initialState', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/data/src/test/registry.js
Expand Up @@ -737,6 +737,7 @@ describe( 'createRegistry', () => {
describe( 'use', () => {
it( 'should pass through options object to plugin', () => {
const expectedOptions = {};
const anyObject = expect.any( Object );
let actualOptions;

function plugin( _registry, options ) {
Expand All @@ -748,7 +749,7 @@ describe( 'createRegistry', () => {
Object.fromEntries(
Object.entries( registry ).map( ( [ key ] ) => {
if ( key === 'stores' ) {
return [ key, expect.any( Object ) ];
return [ key, anyObject ];
Copy link
Member Author

Choose a reason for hiding this comment

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

}
// TODO: Remove this after namsespaces is removed.
if ( key === 'namespaces' ) {
Expand Down
10 changes: 8 additions & 2 deletions packages/editor/src/components/autosave-monitor/test/index.js
Expand Up @@ -174,7 +174,10 @@ describe( 'AutosaveMonitor', () => {

jest.runOnlyPendingTimers();

expect( setTimeout ).lastCalledWith( expect.any( Function ), 5000 );
expect( setTimeout ).toHaveBeenLastCalledWith(
Copy link
Member Author

Choose a reason for hiding this comment

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

expect.any( Function ),
5000
);
} );

it( 'should schedule itself in 1000 ms if the post is not autosaveable at a time', () => {
Expand All @@ -186,6 +189,9 @@ describe( 'AutosaveMonitor', () => {

jest.runOnlyPendingTimers();

expect( setTimeout ).lastCalledWith( expect.any( Function ), 1000 );
expect( setTimeout ).toHaveBeenLastCalledWith(
expect.any( Function ),
1000
);
} );
} );
1 change: 0 additions & 1 deletion packages/scripts/scripts/test-e2e.js
Expand Up @@ -12,7 +12,6 @@ process.on( 'unhandledRejection', ( err ) => {
/**
* External dependencies
*/
/* eslint-disable-next-line jest/no-jest-import */
Copy link
Member Author

Choose a reason for hiding this comment

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

No longer a valid rule, it was removed in jest-community/eslint-plugin-jest#1220.

const jest = require( 'jest' );
const { sync: spawn } = require( 'cross-spawn' );

Expand Down
1 change: 0 additions & 1 deletion packages/scripts/scripts/test-unit-jest.js
Expand Up @@ -12,7 +12,6 @@ process.on( 'unhandledRejection', ( err ) => {
/**
* External dependencies
*/
/* eslint-disable-next-line jest/no-jest-import */
const jest = require( 'jest' );

/**
Expand Down