From b0f3dd73b24b49aaf95ef047c63167343a6f518b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Fri, 19 Jul 2024 14:29:55 +0200 Subject: [PATCH] Refactored Node test environments to share more --- .vscode/launch.json | 8 +-- integration-tests/environments/node/index.cjs | 19 ++----- integration-tests/environments/node/index.mjs | 19 ++----- integration-tests/tests/.mocharc.json | 4 +- integration-tests/tests/src/node/index.ts | 9 +++- ...t-globals.ts => inject-dev-environment.ts} | 33 ++---------- integration-tests/tests/src/node/path.ts | 2 +- .../tests/src/node/setup-globals.ts | 50 +++++++++++++++++++ integration-tests/tests/src/setup-globals.ts | 4 ++ 9 files changed, 80 insertions(+), 68 deletions(-) rename integration-tests/tests/src/node/{inject-globals.ts => inject-dev-environment.ts} (57%) create mode 100644 integration-tests/tests/src/node/setup-globals.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 02d0a05c01..592d9c0ba8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -77,8 +77,8 @@ "--no-warnings", "${workspaceRoot}/node_modules/mocha/lib/cli/cli.js", "--require", - "src/node/inject-globals.ts", - "src/index.ts", + "src/node/inject-dev-environment.ts", + "src/node/index.ts", "--timeout", "10000", "--grep", @@ -103,8 +103,8 @@ ], "args": [ "--require", - "src/node/inject-globals.ts", - "src/index.ts", + "src/node/inject-dev-environment.ts", + "src/node/index.ts", "--timeout", "10000", "--grep", diff --git a/integration-tests/environments/node/index.cjs b/integration-tests/environments/node/index.cjs index 7930ff4fc4..4187224801 100644 --- a/integration-tests/environments/node/index.cjs +++ b/integration-tests/environments/node/index.cjs @@ -18,25 +18,14 @@ const os = require("node:os"); const { Client } = require("mocha-remote-client"); -const fs = require("fs-extra"); -const path = require("path"); -const v8 = require("v8"); -const vm = require("vm"); - -v8.setFlagsFromString("--expose_gc"); const client = new Client({ title: `Node.js v${process.versions.node} on ${os.platform()} (via CommonJS)`, tests(context) { - // Exposing the Realm constructor as a global - global.fs = fs; - global.path = path; - global.environment = { ...context, node: true }; - global.gc = vm.runInNewContext("gc"); - - // Add the integration test suite - require("@realm/integration-tests"); - // Load the Node.js specific part of the integration tests + Object.assign(global, { + environment: { ...context, node: true }, + }); + // Load the Node.js specific part of the integration tests (sets up globals) require("@realm/integration-tests/node"); }, }); diff --git a/integration-tests/environments/node/index.mjs b/integration-tests/environments/node/index.mjs index dbf71a7ca8..3fc6b2d358 100644 --- a/integration-tests/environments/node/index.mjs +++ b/integration-tests/environments/node/index.mjs @@ -18,26 +18,13 @@ import os from "node:os"; import { Client } from "mocha-remote-client"; -import fs from "fs-extra"; -import path from "path"; - -import v8 from "node:v8"; -import vm from "node:vm"; - -v8.setFlagsFromString("--expose_gc"); const client = new Client({ title: `Node.js v${process.versions.node} on ${os.platform()}`, async tests(context) { - // Exposing the Realm constructor as a global - global.fs = fs; - global.path = path; - global.environment = { ...context, node: true }; - global.gc = vm.runInNewContext("gc"); - - // Add the integration test suite - await import("@realm/integration-tests"); - // Load the Node.js specific part of the integration tests + Object.assign(global, { + environment: { ...context, node: true }, + }); await import("@realm/integration-tests/node"); }, }); diff --git a/integration-tests/tests/.mocharc.json b/integration-tests/tests/.mocharc.json index d24db13807..ee3ac57d2c 100644 --- a/integration-tests/tests/.mocharc.json +++ b/integration-tests/tests/.mocharc.json @@ -8,7 +8,7 @@ "reporter": "@realm/mocha-reporter", "require": [ "source-map-support/register", - "src/node/inject-globals.ts" + "src/node/inject-dev-environment.ts" ], - "spec": "src/index.ts" + "spec": ["src/node/index.ts"] } \ No newline at end of file diff --git a/integration-tests/tests/src/node/index.ts b/integration-tests/tests/src/node/index.ts index 73ec5a42c8..558bdfbaaa 100644 --- a/integration-tests/tests/src/node/index.ts +++ b/integration-tests/tests/src/node/index.ts @@ -16,7 +16,14 @@ // //////////////////////////////////////////////////////////////////////////// -// NOTE: This file is only supposed to be imported from a Node.js environment +if (typeof process.versions.node !== "string") { + throw new Error("This file is only supposed to be imported from a Node.js environment!"); +} + +// Import all the regular tests first +import "./setup-globals"; + +import "../index"; import "./analytics"; import "./clean-exit"; diff --git a/integration-tests/tests/src/node/inject-globals.ts b/integration-tests/tests/src/node/inject-dev-environment.ts similarity index 57% rename from integration-tests/tests/src/node/inject-globals.ts rename to integration-tests/tests/src/node/inject-dev-environment.ts index 7432412029..b916d2244d 100644 --- a/integration-tests/tests/src/node/inject-globals.ts +++ b/integration-tests/tests/src/node/inject-dev-environment.ts @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////// // -// Copyright 2020 Realm Inc. +// Copyright 2024 Realm Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -16,36 +16,11 @@ // //////////////////////////////////////////////////////////////////////////// -// Set the defult depth of objects logged with console.log to improve DX when debugging -import { inspect } from "node:util"; -import { existsSync } from "node:fs"; -import { dirname, resolve } from "node:path"; +/* eslint-disable no-restricted-globals */ -inspect.defaultOptions.depth = null; - -if (typeof gc !== "function") { - throw new Error("Run with --expose_gc to allow garbage collection between tests"); -} - -// Require this file to get the Realm constructor injected into the global. -// This is only useful when we want to run the tests outside of any particular environment - -Object.assign(global, { - title: "Realm JS development-mode", +Object.assign(globalThis, { + title: "Node.js development-mode", environment: { node: true }, - fs: { - exists(path: string) { - return existsSync(path); - }, - }, - path: { - dirname(path: string) { - return dirname(path); - }, - resolve(...paths: string[]) { - return resolve(...paths); - }, - }, }); function parseValue(value: string | undefined) { diff --git a/integration-tests/tests/src/node/path.ts b/integration-tests/tests/src/node/path.ts index fecc9539cf..44f4a4b2b7 100644 --- a/integration-tests/tests/src/node/path.ts +++ b/integration-tests/tests/src/node/path.ts @@ -20,7 +20,7 @@ import { expect } from "chai"; import Realm, { BSON, SessionStopPolicy } from "realm"; import path from "node:path"; import os from "node:os"; -import { existsSync, rmSync } from "node:fs"; +import { existsSync } from "node:fs"; import { importAppBefore, authenticateUserBefore } from "../hooks"; import { buildAppConfig } from "../utils/build-app-config"; diff --git a/integration-tests/tests/src/node/setup-globals.ts b/integration-tests/tests/src/node/setup-globals.ts new file mode 100644 index 0000000000..3007862dd5 --- /dev/null +++ b/integration-tests/tests/src/node/setup-globals.ts @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2020 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////// + +/* eslint-disable no-restricted-globals */ + +import { inspect } from "node:util"; +import { existsSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import v8 from "node:v8"; +import vm from "node:vm"; + +// Set the default depth of objects logged with console.log to improve DX when debugging +inspect.defaultOptions.depth = null; + +v8.setFlagsFromString("--expose_gc"); + +Object.assign(globalThis, { + fs: { + exists(path: string) { + return existsSync(path); + }, + }, + path: { + dirname(path: string) { + return dirname(path); + }, + resolve(...paths: string[]) { + return resolve(...paths); + }, + }, + gc: vm.runInNewContext("gc"), +}); + +// Indicate that the tests are running in Node +environment.node = true; diff --git a/integration-tests/tests/src/setup-globals.ts b/integration-tests/tests/src/setup-globals.ts index 8334b0ffa5..f25989a3c5 100644 --- a/integration-tests/tests/src/setup-globals.ts +++ b/integration-tests/tests/src/setup-globals.ts @@ -54,6 +54,10 @@ chai.use(chaiRealmObjects); const DEFAULT_LONG_TIMEOUT = 30 * 1000; // 30s describe("Test Harness", function (this: Mocha.Suite) { + // Inject the root suite title from the global + if (typeof title === "string" && this.parent?.root) { + this.parent.title = title; + } /** * @see [typings.d.ts](./typings.d.ts) for documentation. */