Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Node test environments to share more #6798

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Comment on lines -21 to -35
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main idea behind this refactor is to move this into a shared "node/src/setup-globals.ts" used across these two "runners" and the test ran with bare mocha (without mocha-remote - used when debugging native code).


// 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!");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asserting that this gets imported from node instead of simply commenting the intent.

}

// 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;
}
Comment on lines +58 to +60
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed to get the title from "inject-dev-environment.ts" to propagate as expected.

/**
* @see [typings.d.ts](./typings.d.ts) for documentation.
*/
Expand Down
Loading