Skip to content

Commit

Permalink
Refactored Node test environments to share more (#6798)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen authored Jul 19, 2024
1 parent 13e70d7 commit 62195dc
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 68 deletions.
8 changes: 4 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
19 changes: 4 additions & 15 deletions integration-tests/environments/node/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
},
});
Expand Down
19 changes: 3 additions & 16 deletions integration-tests/environments/node/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
},
});
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/.mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
9 changes: 8 additions & 1 deletion integration-tests/tests/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/node/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
50 changes: 50 additions & 0 deletions integration-tests/tests/src/node/setup-globals.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions integration-tests/tests/src/setup-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 62195dc

Please sign in to comment.