Skip to content

Commit

Permalink
Merge 1173809 into c29e866
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Dec 5, 2023
2 parents c29e866 + 1173809 commit 83b5339
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
1 change: 1 addition & 0 deletions integration-tests/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 1 addition & 16 deletions integration-tests/tests/src/hooks/import-app-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`);
});
}
});

Expand Down
43 changes: 42 additions & 1 deletion integration-tests/tests/src/setup-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidRealmLogLevel, chalk.Chalk> = {
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;
6 changes: 4 additions & 2 deletions integration-tests/tests/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Realm.App.Sync.LogLevel, "off" | "all">;
type RealmContext = {
realm: Realm;
/**
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83b5339

Please sign in to comment.