-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ describe( 'BlockSelectionClearer component', () => { | |
|
||
fireEvent.mouseDown( screen.getByTestId( 'selection-clearer' ) ); | ||
|
||
expect( mockClearSelectedBlock ).toBeCalled(); | ||
expect( mockClearSelectedBlock ).toHaveBeenCalled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} ); | ||
|
||
it( 'should clear the selected block when multiple blocks are selected', () => { | ||
|
@@ -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', () => { | ||
|
@@ -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', () => { | ||
|
@@ -113,6 +113,6 @@ describe( 'BlockSelectionClearer component', () => { | |
|
||
fireEvent.mouseDown( screen.getByTestId( 'selection-clearer' ) ); | ||
|
||
expect( mockClearSelectedBlock ).not.toBeCalled(); | ||
expect( mockClearSelectedBlock ).not.toHaveBeenCalled(); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,20 +22,17 @@ describe( 'getPxFromCssUnit', () => { | |
[ '40Q', '38px' ], // 40 Q should be 1 cm. | ||
]; | ||
|
||
test.each( testData )( 'getPxFromCssUnit( %s )', ( unit, expected ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ describe( 'Draggable', () => { | |
{ state: State.ACTIVE }, | ||
] ); | ||
|
||
expect( onLongPress ).toBeCalledTimes( 1 ); | ||
expect( onLongPress ).toHaveBeenCalledTimes( 1 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
expect( onLongPress ).toHaveBeenCalledWith( triggerId ); | ||
} ); | ||
|
||
|
@@ -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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
render( | ||
<Theme data-testid="theme"> | ||
<MyThemableComponent>Inner</MyThemableComponent> | ||
|
@@ -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> | ||
|
@@ -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> | ||
|
@@ -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> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
altControlProps.label | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ describe( 'fetchUrlData', () => { | |
fetchUrlData( 'https://www.wordpress.org' ) | ||
).resolves.toEqual( data ); | ||
|
||
expect( apiFetch ).toBeCalledTimes( 1 ); | ||
expect( apiFetch ).toHaveBeenCalledTimes( 1 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} ); | ||
|
||
it( 'rejects with error upon fetch failure', async () => { | ||
|
@@ -49,7 +49,7 @@ describe( 'fetchUrlData', () => { | |
method: 'POST', | ||
} ); | ||
|
||
expect( apiFetch ).toBeCalledTimes( 1 ); | ||
expect( apiFetch ).toHaveBeenCalledTimes( 1 ); | ||
|
||
const argsPassedToFetchApi = apiFetch.mock.calls[ 0 ][ 0 ]; | ||
|
||
|
@@ -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. | ||
|
@@ -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 () => { | ||
|
@@ -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( | ||
|
@@ -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( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/ ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} ); | ||
|
||
it( 'should load a persisted value as initialState', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) { | ||
|
@@ -748,7 +749,7 @@ describe( 'createRegistry', () => { | |
Object.fromEntries( | ||
Object.entries( registry ).map( ( [ key ] ) => { | ||
if ( key === 'stores' ) { | ||
return [ key, expect.any( Object ) ]; | ||
return [ key, anyObject ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
// TODO: Remove this after namsespaces is removed. | ||
if ( key === 'namespaces' ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,7 +174,10 @@ describe( 'AutosaveMonitor', () => { | |
|
||
jest.runOnlyPendingTimers(); | ||
|
||
expect( setTimeout ).lastCalledWith( expect.any( Function ), 5000 ); | ||
expect( setTimeout ).toHaveBeenLastCalledWith( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
|
@@ -186,6 +189,9 @@ describe( 'AutosaveMonitor', () => { | |
|
||
jest.runOnlyPendingTimers(); | ||
|
||
expect( setTimeout ).lastCalledWith( expect.any( Function ), 1000 ); | ||
expect( setTimeout ).toHaveBeenLastCalledWith( | ||
expect.any( Function ), | ||
1000 | ||
); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ process.on( 'unhandledRejection', ( err ) => { | |
/** | ||
* External dependencies | ||
*/ | ||
/* eslint-disable-next-line jest/no-jest-import */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ); | ||
|
||
|
There was a problem hiding this comment.
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