[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) <aab@odoo.com>
This commit is contained in:
Lucas Perais (lpe)
2021-06-18 21:31:32 +02:00
committed by Aaron Bohy
parent 12500d8917
commit bc25afa538
+35
View File
@@ -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();