Skip to content
Snippets Groups Projects
Commit db4b04a5 authored by Hervé Donner's avatar Hervé Donner
Browse files

ISAICP-8958: Simplify, leverage core js_testing_log_test.

parent d43f0c70
No related branches found
No related tags found
1 merge request!191Release/v1.113.1
......@@ -6,21 +6,18 @@
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use Drupal\joinup\Traits\JavascriptTrait;
use Symfony\Component\Yaml\Yaml;
use WebDriver\Exception\JavaScriptError;
/**
* Context to report JS errors.
*
* Relies on js_errors_collector.js in joinup_test module.
* Relies on js_testing_log.js injected from joinup_test.
* JS errors will throw exceptions; making behat fail.
* Drupal JS deprecations are logged in PHP (E_USER_DEPRECATED).
*/
class JsErrorsContext extends RawDrupalContext {
use JavascriptTrait;
/**
* Constructs a new context.
*
......@@ -40,12 +37,12 @@ public function __construct(
* @beforeScenario @javascript
*/
public function prepare(): void {
if (!$this->hasJavascriptSupport() || !$this->getSession()->isStarted()) {
if (!$this->getSession()->isStarted()) {
return;
}
// Clear the session storage.
$this->getSession()->executeScript("['joinup_test.js_errors', 'joinup_test.js_warnings'].forEach(key => sessionStorage.removeItem(key))");
$this->getSession()->executeScript("['js_testing_log_test.errors', 'js_testing_log_test.warnings'].forEach(key => sessionStorage.removeItem(key))");
}
/**
......@@ -56,14 +53,14 @@ public function prepare(): void {
* @throws \WebDriver\Exception\JavaScriptError
*/
public function report(AfterScenarioScope $scope): void {
if (!$this->hasJavascriptSupport() || !$this->getSession()->isStarted()) {
if (!$this->getSession()->isStarted()) {
return;
}
// Get warnings/errors and clear them from storage.
$warnings = $this->getSession()->evaluateScript("JSON.parse(sessionStorage.getItem('joinup_test.js_warnings') || JSON.stringify([]))");
$errors = $this->getSession()->evaluateScript("JSON.parse(sessionStorage.getItem('joinup_test.js_errors') || JSON.stringify([]))");
$this->getSession()->executeScript("['joinup_test.js_errors', 'joinup_test.js_warnings'].forEach(key => sessionStorage.removeItem(key))");
$warnings = $this->getSession()->evaluateScript("JSON.parse(sessionStorage.getItem('js_testing_log_test.warnings') || JSON.stringify([]))");
$errors = $this->getSession()->evaluateScript("JSON.parse(sessionStorage.getItem('js_testing_log_test.errors') || JSON.stringify([]))");
$this->getSession()->executeScript("['js_testing_log_test.errors', 'js_testing_log_test.warnings'].forEach(key => sessionStorage.removeItem(key))");
// Report.
$this->reportWarnings($warnings, $scope);
......@@ -79,9 +76,9 @@ public function reportWarnings(array $warnings, AfterScenarioScope $scope): void
}
foreach ($warnings as $warning) {
if (str_starts_with($warning['message'], '[Deprecation]')) {
if (str_starts_with($warning, '[Deprecation]')) {
$path = sprintf("%s:%d", $scope->getFeature()->getFile(), $scope->getScenario()->getLine());
@trigger_error(sprintf("%s\n JS deprecation: %s\n URL: %s", $path, substr($warning['message'], 13), $warning['url']), E_USER_DEPRECATED);
@trigger_error(sprintf("%s\n JS deprecation: %s", $path, substr($warning, 13)), E_USER_DEPRECATED);
}
}
}
......
/**
* @file
* Collects JS errors and deprecations (used in behat).
*
* @see \Drupal\joinup\Context\JsErrorsContext
* @see js_testing_log.js from js_testing_log_test module
*/
(function () {
function addSessionStorageRecord(key, value) {
const data = JSON.parse(sessionStorage.getItem(key) || JSON.stringify([]));
data.push(value);
sessionStorage.setItem(key, JSON.stringify(data));
}
if (typeof console !== "undefined" && console.warn) {
const originalWarn = console.warn;
console.warn = (warning) => {
originalWarn(warning);
addSessionStorageRecord("joinup_test.js_warnings", {
message: warning,
url: window.location.href,
});
};
}
window.addEventListener("error", (evt) => {
addSessionStorageRecord("joinup_test.js_errors", [
`Message: ${evt.message}`,
`URL: ${window.location.href}`,
`File: ${evt.filename}`,
`Line: ${evt.lineno}`,
`Column ${evt.colno}`,
`Stack: ${evt.error instanceof Error ? evt.error.stack : "N/A"}`,
]);
});
})();
js_errors_collector:
# Relies on core js_testing_log_test.
testing.js_errors_log:
header: true
js:
assets/js/js_errors_collector.js: { weight: -1000 }
/core/modules/system/tests/modules/js_testing_log_test/js/js_testing_log.js: { weight: -1000 }
......@@ -11,8 +11,9 @@
* Implements hook_page_attachments().
*/
function joinup_test_page_attachments(array &$attachments): void {
// @todo Inject this conditionally, when behat is active.
// Attach JS errors collector (used in behat).
$attachments['#attached']['library'][] = 'joinup_test/js_errors_collector';
$attachments['#attached']['library'][] = 'joinup_test/testing.js_errors_log';
}
/**
......
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