diff --git a/integration-tests/tests/package.json b/integration-tests/tests/package.json index 331217422dc..ccbf5e9b93e 100644 --- a/integration-tests/tests/package.json +++ b/integration-tests/tests/package.json @@ -93,6 +93,7 @@ "@thi.ng/bench": "^3.1.16", "chai": "4.3.6", "chai-as-promised": "^7.1.1", + "chalk": "^4.1.2", "concurrently": "^6.0.2", "jsrsasign": "^10.6.1" }, diff --git a/integration-tests/tests/src/hooks/import-app-before.ts b/integration-tests/tests/src/hooks/import-app-before.ts index 8dd6c38c981..7ba88ca5fa6 100644 --- a/integration-tests/tests/src/hooks/import-app-before.ts +++ b/integration-tests/tests/src/hooks/import-app-before.ts @@ -16,16 +16,12 @@ // //////////////////////////////////////////////////////////////////////////// -import Realm, { AppConfiguration } from "realm"; +import Realm from "realm"; import { importApp } from "../utils/import-app"; import { AppConfig } from "@realm/app-importer"; import { mongodbServiceType } from "../utils/ExtendedAppConfigBuilder"; -const REALM_LOG_LEVELS = ["all", "trace", "debug", "detail", "info", "warn", "error", "fatal", "off"]; - -const { syncLogLevel = "warn" } = environment; - export type AppConfigurationRelaxed = { id?: string; baseUrl?: string; @@ -65,17 +61,6 @@ export function importAppBefore(config: AppConfig | { config: AppConfig }, sdkCo } else if (databaseNames.length > 1) { throw new Error("Expected at most 1 database name in the config"); } - - Realm.App.Sync.setLogLevel(this.app, syncLogLevel); - // Set a default logger as Android does not forward stdout - Realm.App.Sync.setLogger(this.app, (level, message) => { - const time = new Date().toISOString().split("T")[1].replace("Z", ""); - const magentaTime = `\x1b[35m${time}`; - const greenLogLevel = `\x1b[32m${REALM_LOG_LEVELS[level].toUpperCase()}`; - const whiteMessage = `\x1b[37m${message}}`; - - console.log(`${magentaTime}: ${greenLogLevel}:\t${whiteMessage}`); - }); } }); diff --git a/integration-tests/tests/src/setup-globals.ts b/integration-tests/tests/src/setup-globals.ts index 1f4849e93b7..f62c5ccbdcd 100644 --- a/integration-tests/tests/src/setup-globals.ts +++ b/integration-tests/tests/src/setup-globals.ts @@ -66,7 +66,48 @@ describe("Test Harness", function (this: Mocha.Suite) { import Realm from "realm"; // Disable the logger to avoid console flooding -const { defaultLogLevel = "off" } = environment; +const { printLogAfterTest = false, defaultLogLevel = printLogAfterTest ? "all" : "off" } = environment; Realm.setLogLevel(defaultLogLevel); +import chalk from "chalk"; + +/** + * Contains the Realm log since the test started. + */ +let log: { level: ValidRealmLogLevel; message: string }[] = []; + +if (printLogAfterTest) { + Realm.setLogger((level: Realm.App.Sync.LogLevel, message: string) => { + if (level !== "off" && level !== "all") { + log.push({ level, message }); + } + }); +} + +// Reset the log before each test +beforeEach(() => { + log = []; +}); + +const logColors: Record = { + trace: chalk.dim, + debug: chalk.dim, + detail: chalk.dim, + info: chalk.blue, + warn: chalk.yellow, + error: chalk.red, + fatal: chalk.red, +}; + +afterEach(function (this: Mocha.Context) { + if (printLogAfterTest === true || (printLogAfterTest === "on-failure" && this.currentTest?.isFailed())) { + console.log(); + for (const { level, message } of log) { + const color = logColors[level]; + console.log(`[${color(level)}] ${message}`); + } + console.log(); + } +}); + Realm.flags.THROW_ON_GLOBAL_REALM = true; diff --git a/integration-tests/tests/src/typings.d.ts b/integration-tests/tests/src/typings.d.ts index 7be39d84241..b21885ebaa9 100644 --- a/integration-tests/tests/src/typings.d.ts +++ b/integration-tests/tests/src/typings.d.ts @@ -44,8 +44,8 @@ type KnownEnvironment = { reuseApp?: true; /** Set the default log level to help debugging realm core issues */ defaultLogLevel?: Realm.App.Sync.LogLevel; - /** Set the sync client log level to help debugging sync client issues */ - syncLogLevel?: Realm.App.Sync.LogLevel; + /** Enable printing of the log after every test */ + printLogAfterTest?: boolean | "on-failure"; // BaaS server and Realm App Importer specific variables below @@ -159,6 +159,8 @@ declare namespace Mocha { type AppContext = { app: Realm.App; databaseName: string } & Mocha.Context; type UserContext = { user: Realm.User } & Mocha.Context; type CloseRealmOptions = { deleteFile: boolean; clearTestState: boolean; reopen: boolean }; + +type ValidRealmLogLevel = Exclude; type RealmContext = { realm: Realm; /** diff --git a/package-lock.json b/package-lock.json index 00628258dc0..25017d07827 100644 --- a/package-lock.json +++ b/package-lock.json @@ -923,6 +923,7 @@ "@thi.ng/bench": "^3.1.16", "chai": "4.3.6", "chai-as-promised": "^7.1.1", + "chalk": "^4.1.2", "concurrently": "^6.0.2", "jsrsasign": "^10.6.1" }, @@ -35599,6 +35600,7 @@ "@types/node": "^18.15.10", "chai": "4.3.6", "chai-as-promised": "^7.1.1", + "chalk": "^4.1.2", "concurrently": "^6.5.1", "jsrsasign": "^10.6.1", "mocha": "^10.1.0",