Skip to content

Commit

Permalink
test: support Node test runner (#3163)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Jun 4, 2024
1 parent d62a4bc commit 6f4721a
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 204 deletions.
2 changes: 2 additions & 0 deletions .changeset/angry-moons-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
5 changes: 0 additions & 5 deletions packages/tools-language/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,11 @@
},
"devDependencies": {
"@rnx-kit/eslint-config": "*",
"@rnx-kit/jest-preset": "*",
"@rnx-kit/scripts": "*",
"@rnx-kit/tsconfig": "*",
"@types/node": "^20.0.0",
"eslint": "^8.56.0",
"jest": "^29.2.1",
"prettier": "^3.0.0",
"typescript": "^5.0.0"
},
"jest": {
"preset": "@rnx-kit/jest-preset/private"
}
}
90 changes: 46 additions & 44 deletions packages/tools-language/test/properties.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
import { deepEqual, equal } from "node:assert/strict";
import { describe, it } from "node:test";
import { hasProperty, pickValues } from "../src/properties";

describe("Language > Props > pickValues", () => {
test("returns undefined when no keys are found", () => {
expect(pickValues({} as { x?: string }, ["x"])).toBeUndefined();
it("returns undefined when no keys are found", () => {
equal(pickValues({} as { x?: string }, ["x"]), undefined);
});

test("returns undefined when no keys are given", () => {
expect(pickValues({ x: 123 }, [])).toBeUndefined();
it("returns undefined when no keys are given", () => {
equal(pickValues({ x: 123 }, []), undefined);
});

test("picks a single value", () => {
expect(pickValues({ x: 123 }, ["x"])).toEqual({ x: 123 });
it("picks a single value", () => {
deepEqual(pickValues({ x: 123 }, ["x"]), { x: 123 });
});

test("picks a single value from an object with multiple values", () => {
expect(pickValues({ x: 123, y: "test", z: true }, ["y"])).toEqual({
it("picks a single value from an object with multiple values", () => {
deepEqual(pickValues({ x: 123, y: "test", z: true }, ["y"]), {
y: "test",
});
});

test("picks a single value using the given name", () => {
expect(
pickValues({ x: 123, y: "test", z: true }, ["y"], ["picked"])
).toEqual({ picked: "test" });
it("picks a single value using the given name", () => {
deepEqual(pickValues({ x: 123, y: "test", z: true }, ["y"], ["picked"]), {
picked: "test",
});
});

it("picks all keys", () => {
deepEqual(pickValues({ x: 123, y: "test", z: true }, ["x", "y", "z"]), {
x: 123,
y: "test",
z: true,
});
});

test("picks all keys", () => {
expect(pickValues({ x: 123, y: "test", z: true }, ["x", "y", "z"])).toEqual(
it("picks some keys using different names", () => {
deepEqual(
pickValues({ x: 123, y: "test", z: true }, ["x", "z"], ["a", "b-b"]),
{
x: 123,
y: "test",
z: true,
a: 123,
"b-b": true,
}
);
});

test("picks some keys using different names", () => {
expect(
pickValues({ x: 123, y: "test", z: true }, ["x", "z"], ["a", "b-b"])
).toEqual({
a: 123,
"b-b": true,
});
});

test("picks some keys", () => {
expect(
it("picks some keys", () => {
deepEqual(
pickValues(
{ x: 123, y: "test", z: true } as {
x: number;
Expand All @@ -54,32 +55,33 @@ describe("Language > Props > pickValues", () => {
foo?: string;
},
["x", "y", "foo"]
)
).toEqual({ x: 123, y: "test" });
),
{ x: 123, y: "test" }
);
});
});

describe("Language > Props > hasProperty", () => {
test("returns false for primitives", () => {
expect(hasProperty("string", "0")).toBe(false);
expect(hasProperty(0, "0")).toBe(false);
expect(hasProperty(true, "0")).toBe(false);
expect(hasProperty(null, "0")).toBe(false);
expect(hasProperty(undefined, "0")).toBe(false);
it("returns false for primitives", () => {
equal(hasProperty("string", "0"), false);
equal(hasProperty(0, "0"), false);
equal(hasProperty(true, "0"), false);
equal(hasProperty(null, "0"), false);
equal(hasProperty(undefined, "0"), false);
});

test("returns true for objects with specified property", () => {
it("returns true for objects with specified property", () => {
const testObj = { message: "code", code: 0 };
expect(hasProperty(testObj, "code")).toBe(true);
expect(hasProperty(testObj, "message")).toBe(true);
expect(hasProperty(testObj, "stack")).toBe(false);
equal(hasProperty(testObj, "code"), true);
equal(hasProperty(testObj, "message"), true);
equal(hasProperty(testObj, "stack"), false);
});

test("returns true for objects with inherited property", () => {
it("returns true for objects with inherited property", () => {
const testProto = { message: "code", code: 0 };
const testObj = Object.create(testProto);
expect(hasProperty(testObj, "code")).toBe(true);
expect(hasProperty(testObj, "message")).toBe(true);
expect(hasProperty(testObj, "stack")).toBe(false);
equal(hasProperty(testObj, "code"), true);
equal(hasProperty(testObj, "message"), true);
equal(hasProperty(testObj, "stack"), false);
});
});
5 changes: 0 additions & 5 deletions packages/tools-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,11 @@
},
"devDependencies": {
"@rnx-kit/eslint-config": "*",
"@rnx-kit/jest-preset": "*",
"@rnx-kit/scripts": "*",
"@rnx-kit/tsconfig": "*",
"@types/node": "^20.0.0",
"eslint": "^8.56.0",
"jest": "^29.2.1",
"prettier": "^3.0.0",
"typescript": "^5.0.0"
},
"jest": {
"preset": "@rnx-kit/jest-preset/private"
}
}
82 changes: 43 additions & 39 deletions packages/tools-node/test/module.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from "fs";
import os from "os";
import path from "path";
import { deepEqual, equal } from "node:assert/strict";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { before, describe, it } from "node:test";
import { URL, fileURLToPath } from "node:url";
import {
getPackageModuleRefFromModulePath,
isFileModuleRef,
Expand All @@ -9,65 +12,65 @@ import {
} from "../src/module";

describe("Node > Module", () => {
const fixtureDir = path.resolve(__dirname, "__fixtures__");
const fixtureDir = fileURLToPath(new URL("__fixtures__", import.meta.url));

beforeAll(() => {
expect(fs.existsSync(fixtureDir)).toBe(true);
before(() => {
equal(fs.existsSync(fixtureDir), true);
});

test("parseModuleRef('react-native')", () => {
expect(parseModuleRef("react-native")).toEqual({
it("parseModuleRef('react-native')", () => {
deepEqual(parseModuleRef("react-native"), {
name: "react-native",
});
});

test("parseModuleRef('react-native/Libraries/Promise')", () => {
expect(parseModuleRef("react-native/Libraries/Promise")).toEqual({
it("parseModuleRef('react-native/Libraries/Promise')", () => {
deepEqual(parseModuleRef("react-native/Libraries/Promise"), {
name: "react-native",
path: "Libraries/Promise",
});
});

test("parseModuleRef('@babel/core')", () => {
expect(parseModuleRef("@babel/core")).toEqual({
it("parseModuleRef('@babel/core')", () => {
deepEqual(parseModuleRef("@babel/core"), {
scope: "@babel",
name: "core",
});
});

test("parseModuleRef('@babel/core/parse')", () => {
expect(parseModuleRef("@babel/core/parse")).toEqual({
it("parseModuleRef('@babel/core/parse')", () => {
deepEqual(parseModuleRef("@babel/core/parse"), {
scope: "@babel",
name: "core",
path: "parse",
});
});

test("parseModuleRef('@types/babel__core')", () => {
expect(parseModuleRef("@types/babel__core")).toEqual({
it("parseModuleRef('@types/babel__core')", () => {
deepEqual(parseModuleRef("@types/babel__core"), {
scope: "@types",
name: "babel__core",
});
});

test("parseModuleRef('./parser')", () => {
expect(parseModuleRef("./parser")).toEqual({
it("parseModuleRef('./parser')", () => {
deepEqual(parseModuleRef("./parser"), {
path: "./parser",
});
});

test("parseModuleRef('../../src/parser')", () => {
expect(parseModuleRef("../../src/parser")).toEqual({
it("parseModuleRef('../../src/parser')", () => {
deepEqual(parseModuleRef("../../src/parser"), {
path: "../../src/parser",
});
});

test("parseModuleRef('/absolute/path/src/parser')", () => {
expect(parseModuleRef("/absolute/path/src/parser")).toEqual({
it("parseModuleRef('/absolute/path/src/parser')", () => {
deepEqual(parseModuleRef("/absolute/path/src/parser"), {
path: "/absolute/path/src/parser",
});
if (os.platform() === "win32") {
expect(parseModuleRef("C:/absolute/path/src/parser")).toEqual({
deepEqual(parseModuleRef("C:/absolute/path/src/parser"), {
path: "C:/absolute/path/src/parser",
});
}
Expand All @@ -86,39 +89,40 @@ describe("Node > Module", () => {
"/repos/rnx-kit/package/tools/parser",
];

test("isPackageModuleRef() returns true for package-based refs", () => {
it("isPackageModuleRef() returns true for package-based refs", () => {
for (const r of packageModuleRefs) {
expect(isPackageModuleRef(parseModuleRef(r))).toBe(true);
equal(isPackageModuleRef(parseModuleRef(r)), true);
}
});

test("isPackageModuleRef() returns false for file-based refs", () => {
it("isPackageModuleRef() returns false for file-based refs", () => {
for (const r of fileModuleRefs) {
expect(isPackageModuleRef(parseModuleRef(r))).toBe(false);
equal(isPackageModuleRef(parseModuleRef(r)), false);
}
});

test("isFileModuleRef() returns true for file-based refs", () => {
it("isFileModuleRef() returns true for file-based refs", () => {
for (const r of fileModuleRefs) {
expect(isFileModuleRef(parseModuleRef(r))).toBe(true);
equal(isFileModuleRef(parseModuleRef(r)), true);
}
});

test("isFileModuleRef() returns false for package-based refs", () => {
it("isFileModuleRef() returns false for package-based refs", () => {
for (const r of packageModuleRefs) {
expect(isFileModuleRef(parseModuleRef(r))).toBe(false);
equal(isFileModuleRef(parseModuleRef(r)), false);
}
});

test("getPackageModuleRefFromModulePath() returns a valid ref when given a path to a scoped module", () => {
expect(
it("getPackageModuleRefFromModulePath() returns a valid ref when given a path to a scoped module", () => {
deepEqual(
getPackageModuleRefFromModulePath(
path.join(fixtureDir, "node_modules", "@babel", "core", "foo")
)
).toEqual({
scope: "@babel",
name: "core",
path: "foo",
});
),
{
scope: "@babel",
name: "core",
path: "foo",
}
);
});
});
Loading

0 comments on commit 6f4721a

Please sign in to comment.