Skip to content

Commit

Permalink
test: migrate some tests to Node test runner (#3245)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Jul 27, 2024
1 parent fc19779 commit b3fd0b3
Show file tree
Hide file tree
Showing 22 changed files with 502 additions and 463 deletions.
2 changes: 2 additions & 0 deletions .changeset/witty-rocks-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
7 changes: 0 additions & 7 deletions packages/babel-plugin-import-path-remapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,13 @@
},
"devDependencies": {
"@rnx-kit/eslint-config": "*",
"@rnx-kit/jest-preset": "*",
"@rnx-kit/scripts": "*",
"@rnx-kit/tsconfig": "*",
"@types/babel__core": "^7.0.0",
"@types/babel__helper-plugin-utils": "^7.0.0",
"@types/jest": "^29.2.1",
"@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",
"testRegex": "/test/.*\\.test\\.js$"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
"use strict";
import { equal, throws } from "node:assert/strict";
import { afterEach, describe, it } from "node:test";

describe("@rnx-kit/babel-plugin-import-path-remapper", () => {
const babel = require("@babel/core");
Expand All @@ -9,20 +9,18 @@ describe("@rnx-kit/babel-plugin-import-path-remapper", () => {

/**
* Returns whether requested source is in @rnx-kit scope.
* @param {string} source
* @returns {boolean}
*/
function isRNXKit(source) {
function isRNXKit(source: string): boolean {
return source.startsWith("@rnx-kit/");
}

/**
* Transforms the specified code.
* @param {string} code
* @param {unknown=} options
* @returns {string | null | undefined}
*/
function transform(code, options = { test: isRNXKit }) {
function transform(
code: string,
options: unknown | undefined = { test: isRNXKit }
): string | null | undefined {
const result = babel.transformSync(code, {
plugins: [[path.join(__dirname, "..", "src", "index.js"), options]],
});
Expand All @@ -33,19 +31,18 @@ describe("@rnx-kit/babel-plugin-import-path-remapper", () => {
process.chdir(currentWorkingDir);
});

test("throws if no test function is specified", () => {
expect(() => transform("", {})).toThrow(
"Expected option `test` to be a function"
);
it("throws if no test function is specified", () => {
throws(() => transform("", {}), "Expected option `test` to be a function");
});

test("throws if remap is not a function", () => {
expect(() => transform("", { test: isRNXKit, remap: "error" })).toThrow(
it("throws if remap is not a function", () => {
throws(
() => transform("", { test: isRNXKit, remap: "error" }),
"Expected option `remap` to be undefined or a function"
);
});

test("leaves unmatched import/export statements", () => {
it("leaves unmatched import/export statements", () => {
[
"export const zero = () => 0;",
`export * from "@contoso/example/lib/index";`,
Expand All @@ -56,99 +53,100 @@ describe("@rnx-kit/babel-plugin-import-path-remapper", () => {
`require("@contoso/example/lib/index");`,
`require("fs").readFileSync("@contoso/example/lib/index");`,
].forEach((code) => {
expect(transform(code)).toBe(code);
equal(transform(code), code);
});
});

test("remaps require() calls", () => {
expect(transform(`require("@rnx-kit/example/lib/index");`)).toBe(
it("remaps require() calls", () => {
equal(
transform(`require("@rnx-kit/example/lib/index");`),
`require("@rnx-kit/example/src/index");`
);
});

test("remaps import() calls", () => {
expect(transform(`import("@rnx-kit/example/lib/index");`)).toBe(
it("remaps import() calls", () => {
equal(
transform(`import("@rnx-kit/example/lib/index");`),
`import("@rnx-kit/example/src/index");`
);
});

test("remaps import declaration (all)", () => {
expect(
transform(`import * as Example from "@rnx-kit/example/lib/index";`)
).toBe(`import * as Example from "@rnx-kit/example/src/index";`);
it("remaps import declaration (all)", () => {
equal(
transform(`import * as Example from "@rnx-kit/example/lib/index";`),
`import * as Example from "@rnx-kit/example/src/index";`
);
});

test("remaps import declaration (named)", () => {
expect(
transform(`import { a, b } from "@rnx-kit/example/lib/index";`)
).toBe(`import { a, b } from "@rnx-kit/example/src/index";`);
it("remaps import declaration (named)", () => {
equal(
transform(`import { a, b } from "@rnx-kit/example/lib/index";`),
`import { a, b } from "@rnx-kit/example/src/index";`
);
});

test("remaps export declaration (all)", () => {
expect(transform(`export * from "@rnx-kit/example/lib/index";`)).toBe(
it("remaps export declaration (all)", () => {
equal(
transform(`export * from "@rnx-kit/example/lib/index";`),
`export * from "@rnx-kit/example/src/index";`
);
});

test("remaps export declaration (named)", () => {
expect(
transform(`export { a, b } from "@rnx-kit/example/lib/index";`)
).toBe(`export { a, b } from "@rnx-kit/example/src/index";`);
it("remaps export declaration (named)", () => {
equal(
transform(`export { a, b } from "@rnx-kit/example/lib/index";`),
`export { a, b } from "@rnx-kit/example/src/index";`
);
});

test("remaps `lib` only", () => {
expect(transform(`import A from "@rnx-kit/example/lib";`)).toBe(
it("remaps `lib` only", () => {
equal(
transform(`import A from "@rnx-kit/example/lib";`),
`import A from "@rnx-kit/example/src";`
);
});

test("ignores subsequent `lib` folders", () => {
expect(
transform(`import A from "@rnx-kit/example/lib/index/lib/index";`)
).toBe(`import A from "@rnx-kit/example/src/index/lib/index";`);
it("ignores subsequent `lib` folders", () => {
equal(
transform(`import A from "@rnx-kit/example/lib/index/lib/index";`),
`import A from "@rnx-kit/example/src/index/lib/index";`
);
});

test("preserves magic comments", () => {
expect(
it("preserves magic comments", () => {
equal(
transform(
`import(/* webpackChunkName: "example" */ "@rnx-kit/example/lib/index");`
)
).toBe(
),
`import( /* webpackChunkName: "example" */"@rnx-kit/example/src/index");`
);
});

test("uses custom remap function when importing a module without path", () => {
it("uses custom remap function when importing a module without path", () => {
process.chdir("test/__fixtures__/with-main");
expect(
equal(
transform(
`import(/* webpackChunkName: "example" */ "@rnx-kit/example");`,
{
test: isRNXKit,
remap: (
/** @type {string} */ moduleName,
/** @type {string} */ path
) => `${moduleName}/__mocks__/${path}`,
remap: (moduleName: string, path: string) =>
`${moduleName}/__mocks__/${path}`,
}
)
).toBe(
),
`import( /* webpackChunkName: "example" */"@rnx-kit/example/__mocks__/index.js");`
);
});

test("uses custom remap function when importing a module with path", () => {
expect(
it("uses custom remap function when importing a module with path", () => {
equal(
transform(
`import(/* webpackChunkName: "example" */ "@rnx-kit/example/lib/index");`,
{
test: isRNXKit,
remap: (
/** @type {string} */ moduleName,
/** @type {string} */ path
) => `${moduleName}/__mocks__/${path}`,
remap: (moduleName: string, path: string) =>
`${moduleName}/__mocks__/${path}`,
}
)
).toBe(
),
`import( /* webpackChunkName: "example" */"@rnx-kit/example/__mocks__/lib/index");`
);
});
Expand Down
6 changes: 0 additions & 6 deletions packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,11 @@
},
"devDependencies": {
"@rnx-kit/eslint-config": "*",
"@rnx-kit/jest-preset": "*",
"@rnx-kit/scripts": "*",
"@rnx-kit/tsconfig": "*",
"@types/jest": "^29.2.1",
"@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"
}
}
6 changes: 5 additions & 1 deletion packages/console/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const error: Log = (...args) => console.error(errorTag, ...args);
export const info: Log = (...args) => console.log(infoTag, ...args);
export const warn: Log = (...args) => console.warn(warnTag, ...args);

if (WriteStream.prototype.hasColors() && process.env["NODE_ENV"] !== "test") {
if (
WriteStream.prototype.hasColors() &&
!process.env["NODE_TEST_CONTEXT"] &&
process.env["NODE_ENV"] !== "test"
) {
errorTag = "\u001B[31m\u001B[1merror\u001B[22m\u001B[39m";
infoTag = "\u001B[36m\u001B[1minfo\u001B[22m\u001B[39m";
warnTag = "\u001B[33m\u001B[1mwarn\u001B[22m\u001B[39m";
Expand Down
38 changes: 19 additions & 19 deletions packages/console/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { deepEqual, equal } from "node:assert/strict";
import { describe, it } from "node:test";
import { error, info, warn } from "../src/index";

describe("console", () => {
const consoleErrorSpy = jest.spyOn(global.console, "error");
const consoleLogSpy = jest.spyOn(global.console, "log");
const consoleWarnSpy = jest.spyOn(global.console, "warn");

const args = ["string", 0, true];

beforeEach(() => {
consoleErrorSpy.mockReset();
consoleLogSpy.mockReset();
consoleWarnSpy.mockReset();
});
it("prints error messages", (t) => {
const errorMock = t.mock.method(console, "error", () => null);

afterAll(() => {
jest.clearAllMocks();
});

test("prints error messages", () => {
error(...args);
expect(consoleErrorSpy).toHaveBeenCalledWith("error", ...args);

equal(errorMock.mock.calls.length, 1);
deepEqual(errorMock.mock.calls[0].arguments, ["error", ...args]);
});

test("prints info messages", () => {
it("prints info messages", (t) => {
const infoMock = t.mock.method(console, "log", () => null);

info(...args);
expect(consoleLogSpy).toHaveBeenCalledWith("info", ...args);

equal(infoMock.mock.calls.length, 1);
deepEqual(infoMock.mock.calls[0].arguments, ["info", ...args]);
});

test("prints warning messages", () => {
it("prints warning messages", (t) => {
const warnMock = t.mock.method(console, "warn", () => null);

warn(...args);
expect(consoleWarnSpy).toHaveBeenCalledWith("warn", ...args);

equal(warnMock.mock.calls.length, 1);
deepEqual(warnMock.mock.calls[0].arguments, ["warn", ...args]);
});
});
6 changes: 0 additions & 6 deletions packages/metro-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@rnx-kit/eslint-config": "*",
"@rnx-kit/jest-preset": "*",
"@rnx-kit/scripts": "*",
"@rnx-kit/tsconfig": "*",
"@types/babel__core": "^7.0.0",
"@types/connect": "^3.4.36",
"@types/jest": "^29.2.1",
"eslint": "^8.56.0",
"jest": "^29.2.1",
"metro": "^0.80.0",
"metro-config": "^0.80.0",
"metro-resolver": "^0.80.0",
Expand All @@ -67,8 +64,5 @@
"metro-resolver",
"type-fest"
]
},
"jest": {
"preset": "@rnx-kit/jest-preset/private"
}
}
14 changes: 6 additions & 8 deletions packages/metro-config/test/assetPluginForMonorepos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { IncomingMessage, ServerResponse } from "http";
import type { AssetData } from "metro";
import type { Middleware } from "metro-config";
import type Server from "metro/src/Server";
import { equal } from "node:assert/strict";
import { describe, it } from "node:test";

describe("@rnx-kit/metro-config/assetPluginForMonorepos", () => {
const assetPlugin = require("../src/assetPluginForMonorepos");
Expand All @@ -15,21 +17,17 @@ describe("@rnx-kit/metro-config/assetPluginForMonorepos", () => {
"/assets/node_modules/@@/@@/react-native",
};

test("escapes `..` in URLs", () => {
it("escapes `..` in URLs", () => {
Object.entries(cases).forEach(([input, output]) => {
const assetData = { httpServerLocation: input } as AssetData;
expect(assetPlugin(assetData)).toEqual(
expect.objectContaining({
httpServerLocation: output,
})
);
equal(assetPlugin(assetData).httpServerLocation, output);
});
});

test("unescapes `..` in URLs", () => {
it("unescapes `..` in URLs", () => {
Object.entries(cases).forEach(([output, input]) => {
const middleware: Middleware = (req: Middleware) => {
expect(req).toEqual(expect.objectContaining({ url: output }));
equal(req.url, output);
return middleware;
};
const server = {
Expand Down
Loading

0 comments on commit b3fd0b3

Please sign in to comment.