Commit b7d642be 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

(cherry picked from commit 8836d062)
parent ed25429d
Loading
Loading
Loading
Loading
+20 −4
Original line number Diff line number Diff line
@@ -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')");
  }

}
+16 −8
Original line number Diff line number Diff line
@@ -115,16 +115,24 @@ protected function tearDown(): void {
          @trigger_error('Javascript Deprecation:' . substr($warning, 13), 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)) {
          $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);
        $this->fail(implode("\n", $errors));
      }
    }

    }
    parent::tearDown();
  }

  /**