Skip to content
Snippets Groups Projects
Commit 8836d062 authored by catch's avatar catch
Browse files

Issue #3293090 by longwave, pooja saraah, mondrake, alexpott, Gábor Hojtsy:...

Issue #3293090 by longwave, pooja saraah, mondrake, alexpott, Gábor Hojtsy: Fail JavaScript tests on JavaScript errors
parent a223aa1c
No related branches found
No related tags found
No related merge requests found
......@@ -2,11 +2,12 @@
namespace Drupal\FunctionalJavascriptTests;
use PHPUnit\Framework\AssertionFailedError;
/**
* Tests that Drupal.throwError will cause a deprecation warning.
* Tests that Drupal.throwError will cause a test failure.
*
* @group javascript
* @group legacy
*/
class JavascriptErrorsTest extends WebDriverTestBase {
......@@ -21,15 +22,30 @@ class JavascriptErrorsTest extends WebDriverTestBase {
protected static $modules = ['js_errors_test'];
/**
* Tests that JavaScript console errors will result in a deprecation warning.
* Tests that JavaScript console errors will result in a test failure.
*/
public function testJavascriptErrors(): void {
$this->expectDeprecation('Not failing JavaScript test for JavaScript errors is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. This test had the following JavaScript errors: Error: A manually thrown error.');
// Visit page that will throw a JavaScript console error.
$this->drupalGet('js_errors_test');
// Ensure that errors from previous page loads will be
// detected.
$this->drupalGet('user');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessageMatches('/^Error: A manually thrown error/');
// Manually call the method under test, as it cannot be caught by PHPUnit
// when triggered from assertPostConditions().
$this->failOnJavaScriptErrors();
}
/**
* Clear the JavaScript error log to prevent this test failing for real.
*
* @postCondition
*/
public function clearErrorLog() {
$this->getSession()->executeScript("sessionStorage.removeItem('js_testing_log_test.errors')");
}
}
......@@ -115,18 +115,26 @@ protected function tearDown(): void {
@trigger_error('Javascript Deprecation:' . substr($warning, 13), E_USER_DEPRECATED);
}
}
if ($this->failOnJavascriptConsoleErrors) {
$errors = $this->getSession()->evaluateScript("JSON.parse(sessionStorage.getItem('js_testing_log_test.errors') || JSON.stringify([]))");
if (!empty($errors)) {
$all_errors = implode("\n", $errors);
@trigger_error("Not failing JavaScript test for JavaScript errors is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. This test had the following JavaScript errors: $all_errors. See https://www.drupal.org/node/3221100", E_USER_DEPRECATED);
}
}
}
parent::tearDown();
}
/**
* Triggers a test failure if a JavaScript error was encountered.
*
* @throws \PHPUnit\Framework\AssertionFailedError
*
* @postCondition
*/
protected function failOnJavaScriptErrors(): void {
if ($this->failOnJavascriptConsoleErrors) {
$errors = $this->getSession()->evaluateScript("JSON.parse(sessionStorage.getItem('js_testing_log_test.errors') || JSON.stringify([]))");
if (!empty($errors)) {
$this->fail(implode("\n", $errors));
}
}
}
/**
* {@inheritdoc}
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment