Change MockConsoleLog to an actual class

This commit is contained in:
Michael Li 2025-01-10 15:51:42 -05:00 committed by Sirz Benjie
parent 21c111b18c
commit 6049f6a776
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
2 changed files with 74 additions and 73 deletions

View File

@ -1,72 +1,75 @@
const MockConsoleLog = (_logDisabled = false, _phaseText = false) => {
let logs: any[] = [];
const logDisabled: boolean = _logDisabled;
const phaseText: boolean = _phaseText;
const originalLog = console.log; const originalLog = console.log;
const originalError = console.error; const originalError = console.error;
const originalDebug = console.debug; const originalDebug = console.debug;
const originalWarn = console.warn; const originalWarn = console.warn;
const notified: any[] = [];
const blacklist = [ "Phaser", "variant icon does not exist", "Texture \"%s\" not found" ];
// eslint-disable-next-line quotes
const blacklist = [ "Phaser", "variant icon does not exist", 'Texture "%s" not found' ];
const whitelist = [ "Phase" ]; const whitelist = [ "Phase" ];
return ({ export class MockConsoleLog {
log(...args) { constructor(
private logDisabled = false,
private phaseText = false,
) {}
private logs: any[] = [];
private notified: any[] = [];
public log(...args) {
const argsStr = this.getStr(args); const argsStr = this.getStr(args);
logs.push(argsStr); this.logs.push(argsStr);
if (logDisabled && (!phaseText)) { if (this.logDisabled && !this.phaseText) {
return; return;
} }
if ((phaseText && !whitelist.some((b) => argsStr.includes(b))) || blacklist.some((b) => argsStr.includes(b))) { if ((this.phaseText && !whitelist.some((b) => argsStr.includes(b))) || blacklist.some((b) => argsStr.includes(b))) {
return; return;
} }
originalLog(args); originalLog(args);
}, }
error(...args) { public error(...args) {
const argsStr = this.getStr(args); const argsStr = this.getStr(args);
logs.push(argsStr); this.logs.push(argsStr);
originalError(args); // Appelle le console.error originel originalError(args); // Appelle le console.error originel
}, }
debug(...args) { public debug(...args) {
const argsStr = this.getStr(args); const argsStr = this.getStr(args);
logs.push(argsStr); this.logs.push(argsStr);
if (logDisabled && (!phaseText)) { if (this.logDisabled && !this.phaseText) {
return; return;
} }
if (!whitelist.some((b) => argsStr.includes(b)) || blacklist.some((b) => argsStr.includes(b))) { if (!whitelist.some((b) => argsStr.includes(b)) || blacklist.some((b) => argsStr.includes(b))) {
return; return;
} }
originalDebug(args); originalDebug(args);
}, }
warn(...args) { warn(...args) {
const argsStr = this.getStr(args); const argsStr = this.getStr(args);
logs.push(args); this.logs.push(args);
if (logDisabled && (!phaseText)) { if (this.logDisabled && !this.phaseText) {
return; return;
} }
if (!whitelist.some((b) => argsStr.includes(b)) || blacklist.some((b) => argsStr.includes(b))) { if (!whitelist.some((b) => argsStr.includes(b)) || blacklist.some((b) => argsStr.includes(b))) {
return; return;
} }
originalWarn(args); originalWarn(args);
}, }
notify(msg) { notify(msg) {
originalLog(msg); originalLog(msg);
notified.push(msg); this.notified.push(msg);
}, }
getLogs() { getLogs() {
return logs; return this.logs;
}, }
clearLogs() { clearLogs() {
logs = []; this.logs = [];
}, }
getStr(...args) { getStr(...args) {
return args.map(arg => { return args
.map((arg) => {
if (typeof arg === "object" && arg !== null) { if (typeof arg === "object" && arg !== null) {
// Handle objects including arrays // Handle objects including arrays
return JSON.stringify(arg, (key, value) => return JSON.stringify(arg, (_key, value) => (typeof value === "bigint" ? value.toString() : value));
typeof value === "bigint" ? value.toString() : value
);
} else if (typeof arg === "bigint") { } else if (typeof arg === "bigint") {
// Handle BigInt values // Handle BigInt values
return arg.toString(); return arg.toString();
@ -74,9 +77,7 @@ const MockConsoleLog = (_logDisabled = false, _phaseText = false) => {
// Handle all other types // Handle all other types
return arg.toString(); return arg.toString();
} }
}).join(";"); })
}, .join(";");
}); }
}; }
export default MockConsoleLog;

View File

@ -13,7 +13,7 @@ import { initVouchers } from "#app/system/voucher";
import { initStatsKeys } from "#app/ui/game-stats-ui-handler"; import { initStatsKeys } from "#app/ui/game-stats-ui-handler";
import { setCookie } from "#app/utils"; import { setCookie } from "#app/utils";
import { blobToString } from "#test/testUtils/gameManagerUtils"; import { blobToString } from "#test/testUtils/gameManagerUtils";
import { mockConsoleLog } from "#test/testUtils/mocks/mockConsoleLog"; import { MockConsoleLog } from "#test/testUtils/mocks/mockConsoleLog";
import { mockLocalStorage } from "#test/testUtils/mocks/mockLocalStorage"; import { mockLocalStorage } from "#test/testUtils/mocks/mockLocalStorage";
import { MockImage } from "#test/testUtils/mocks/mocksContainer/mockImage"; import { MockImage } from "#test/testUtils/mocks/mocksContainer/mockImage";
import Phaser from "phaser"; import Phaser from "phaser";
@ -32,7 +32,7 @@ export function initTestFile() {
value: mockLocalStorage(), value: mockLocalStorage(),
}); });
Object.defineProperty(window, "console", { Object.defineProperty(window, "console", {
value: mockConsoleLog(false), value: new MockConsoleLog(false),
}); });
Object.defineProperty(document, "fonts", { Object.defineProperty(document, "fonts", {
writable: true, writable: true,