From bc25afa538026b4570b2319a5ff94a7422de1f45 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Thu, 10 Jun 2021 09:57:28 +0000 Subject: [PATCH] [IMP] web: tests setup: check integrity of legacy global objects Legacy tests and business code heavily use some global objects (session and _t) Wowl tests are set to clean the execution environment at the end of each tests while legacy tests clean their environment when the widget is destroyed This discrepancy may introduce weird, untimely and virtually undebuggable issues for the average Joe, because some tests may pollute the execution environment for following tests This commit aims at limiting this risk by hardcoding a few objects and their field to be checked at the end of each test closes odoo-dev/odoo#945 Signed-off-by: Aaron Bohy (aab) --- addons/web/static/tests/setup.js | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/addons/web/static/tests/setup.js b/addons/web/static/tests/setup.js index 6722a5dd9c9..b44be18b306 100644 --- a/addons/web/static/tests/setup.js +++ b/addons/web/static/tests/setup.js @@ -2,6 +2,7 @@ import core from "web.core"; import session from "web.session"; +import { _t } from "web.core"; import { browser, makeRAMLocalStorage } from "@web/core/browser/browser"; import { patchWithCleanup } from "@web/../tests/helpers/utils"; import { legacyProm } from "web.test_legacy"; @@ -13,6 +14,39 @@ const { whenReady, loadFile } = owl.utils; owl.config.enableTransitions = false; owl.QWeb.dev = true; +function stringifyObjectValues(obj, properties) { + let res = ""; + for (const dotted of properties) { + const keys = dotted.split("."); + let val = obj; + for (const k of keys) { + val = val[k]; + } + res += JSON.stringify(val); + } + return res; +} + +function checkGlobalObjectsIntegrity() { + const objects = [ + [session, ["user_context", "currencies"]], + [_t, ["database.multi_lang", "database.parameters"]], + ]; + const initials = objects.map((obj) => stringifyObjectValues(obj[0], obj[1])); + + registerCleanup((infos) => { + const finals = objects.map((obj) => stringifyObjectValues(obj[0], obj[1])); + for (const index in initials) { + if (initials[index] !== finals[index]) { + const [global, keys] = objects[index]; + throw new Error( + `The keys "${keys}" of some global objects (usually session or _t) may have been polluted by the test "${infos.testName}" in module "${infos.moduleName}"` + ); + } + } + }); +} + function forceLocaleAndTimezoneWithCleanup() { const originalLocale = luxon.Settings.defaultLocale; luxon.Settings.defaultLocale = "en"; @@ -140,6 +174,7 @@ function patchOdoo() { export async function setupTests() { QUnit.testStart(() => { + checkGlobalObjectsIntegrity(); prepareRegistriesWithCleanup(); forceLocaleAndTimezoneWithCleanup(); patchBrowserWithCleanup();