Skip to content

Commit

Permalink
E2E Test Utils: Refactor hasPageError as getPageError
Browse files Browse the repository at this point in the history
Return error message so it can be used in failure output
  • Loading branch information
aduth committed Apr 7, 2020
1 parent 0164e95 commit 965900a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 43 deletions.
6 changes: 6 additions & 0 deletions packages/e2e-test-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New Features

- Added `getPageError` function, returning a promise which resolves to an error message present in the page, if any exists.

## 4.0.0 (2019-12-19)

### Breaking Changes
Expand Down
23 changes: 12 additions & 11 deletions packages/e2e-test-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,27 +252,28 @@ _Returns_

- `Promise`: Promise resolving with post content markup.

<a name="hasBlockSwitcher" href="#hasBlockSwitcher">#</a> **hasBlockSwitcher**
<a name="getPageError" href="#getPageError">#</a> **getPageError**

Returns a boolean indicating if the current selected block has a block switcher or not.
Returns a promise resolving to one of either a string or null. A string will
be resolved if an error message is present in the contents of the page. If no
error is present, a null value will be resolved instead. This requires the
environment be configured to display errors.

_Returns_
_Related_

- `Promise`: Promise resolving with a boolean.
- <http://php.net/manual/en/function.error-reporting.php>

<a name="hasPageError" href="#hasPageError">#</a> **hasPageError**
_Returns_

Returns a promise resolving to a boolean reflecting whether a PHP notice is
present anywhere within the document's markup. This requires the environment
be configured to display errors.
- `Promise<?string>`: Promise resolving to a string or null, depending whether a page error is present.

_Related_
<a name="hasBlockSwitcher" href="#hasBlockSwitcher">#</a> **hasBlockSwitcher**

- <http://php.net/manual/en/function.error-reporting.php>
Returns a boolean indicating if the current selected block has a block switcher or not.

_Returns_

- `Promise`: Promise resolving to a boolean reflecting whether a PHP notice is present.
- `Promise`: Promise resolving with a boolean.

<a name="insertBlock" href="#insertBlock">#</a> **insertBlock**

Expand Down
25 changes: 25 additions & 0 deletions packages/e2e-test-utils/src/get-page-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Regular expression matching a displayed PHP error within a markup string.
*
* @see https://github.com/php/php-src/blob/598175e/main/main.c#L1257-L1297
*
* @type {RegExp}
*/
const REGEXP_PHP_ERROR = /(<b>)?(Fatal error|Recoverable fatal error|Warning|Parse error|Notice|Strict Standards|Deprecated|Unknown error)(<\/b>)?: (.*?) in (.*?) on line (<b>)?\d+(<\/b>)?/;

/**
* Returns a promise resolving to one of either a string or null. A string will
* be resolved if an error message is present in the contents of the page. If no
* error is present, a null value will be resolved instead. This requires the
* environment be configured to display errors.
*
* @see http://php.net/manual/en/function.error-reporting.php
*
* @return {Promise<?string>} Promise resolving to a string or null, depending
* whether a page error is present.
*/
export async function getPageError() {
const content = await page.content();
const match = content.match( REGEXP_PHP_ERROR );
return match ? match[ 0 ] : null;
}
22 changes: 0 additions & 22 deletions packages/e2e-test-utils/src/has-page-error.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/e2e-test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export { getAvailableBlockTransforms } from './get-available-block-transforms';
export { getBlockSetting } from './get-block-setting';
export { getEditedPostContent } from './get-edited-post-content';
export { hasBlockSwitcher } from './has-block-switcher';
export { hasPageError } from './has-page-error';
export { getPageError } from './get-page-error';
export { insertBlock } from './insert-block';
export { installPlugin } from './install-plugin';
export { isCurrentURL } from './is-current-url';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { hasPageError } from '../has-page-error';
import { getPageError } from '../get-page-error';

describe( 'hasPageError', () => {
describe( 'getPageError', () => {
let originalPage;

beforeEach( () => {
Expand All @@ -14,31 +14,31 @@ describe( 'hasPageError', () => {
global.page = originalPage;
} );

it( 'returns false if there is no error', async () => {
it( 'resolves to null if there is no error', async () => {
global.page = {
content: () => 'Happy!',
};

expect( await hasPageError() ).toBe( false );
expect( await getPageError() ).toBe( null );
} );

it.each( [
[
'PHP, HTML',
'<b>Notice</b>: Undefined property: WP_Block_Type_Registry::$x in <b>/var/www/html/wp-content/plugins/gutenberg/lib/blocks.php</b> on line <b>47</b><br />',
'<b>Notice</b>: Undefined property: WP_Block_Type_Registry::$x in <b>/var/www/html/wp-content/plugins/gutenberg/lib/blocks.php</b> on line <b>47</b>',
],
[
'PHP, Plaintext',
'Notice: Undefined property: WP_Block_Type_Registry::$x in /var/www/html/wp-content/plugins/gutenberg/lib/blocks.php on line 47',
],
] )(
'returns true if there is an error (%s)',
'resolves to the error message if there is an error (%s)',
async ( _variant, error ) => {
global.page = {
content: () => error,
};

expect( await hasPageError() ).toBe( true );
expect( await getPageError() ).toBe( error );
}
);
} );
4 changes: 2 additions & 2 deletions packages/e2e-test-utils/src/visit-admin-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { join } from 'path';
import { createURL } from './create-url';
import { isCurrentURL } from './is-current-url';
import { loginUser } from './login-user';
import { hasPageError } from './has-page-error';
import { getPageError } from './get-page-error';

/**
* Visits admin page; if user is not logged in then it logging in it first, then visits admin page.
Expand All @@ -25,5 +25,5 @@ export async function visitAdminPage( adminPath, query ) {
await visitAdminPage( adminPath, query );
}

expect( await hasPageError() ).toBe( false );
expect( await getPageError() ).toBe( null );
}

0 comments on commit 965900a

Please sign in to comment.