Skip to content

Commit

Permalink
test: migrate Jest to node:test (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Jan 6, 2025
1 parent 6609e96 commit 80c0424
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 2,916 deletions.
5 changes: 3 additions & 2 deletions lib/__tests__/aggregateReport.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, expect } from "@jest/globals";
import { strict as assert } from "node:assert";
import { test } from "node:test";

import aggregateReport from "../aggregateReport.js";

Expand Down Expand Up @@ -30,7 +31,7 @@ test("aggregateReport()", async () => {
after.set("rimraf:rimraf", "3.0.2");

const result = await aggregateReport(audit, before, after);
expect(result).toEqual({
assert.deepEqual(result, {
added: [
{
location: null,
Expand Down
16 changes: 8 additions & 8 deletions lib/__tests__/audit.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { strict as assert } from "node:assert";
import { readFileSync } from "node:fs";
import { jest, test, expect } from "@jest/globals";
import { test } from "node:test";

import audit from "../audit.js";

Expand All @@ -15,14 +16,13 @@ const auditJson2 = JSON.parse(readFileSync(fixture("audit-2.json"), "utf8"));
* @param {unknown} json
* @returns {typeof import("@actions/exec").getExecOutput}
*/
const mockExec = (json) =>
// @ts-expect-error -- TS2322: Type 'Mock<UnknownFunction>' is not assignable to type '(commandLine: string, args?: string[] | undefined, options?: ExecOptions | undefined) => Promise<ExecOutput>'.
jest.fn().mockImplementationOnce(() => ({ stdout: JSON.stringify(json) }));
const mockExec = (json) => () =>
Promise.resolve({ exitCode: 0, stdout: JSON.stringify(json), stderr: "" });

test("audit()", async () => {
const packages = await audit(mockExec(auditJson));
expect(packages.size).toEqual(1);
expect(packages.get("y18n")).toEqual({
assert.equal(packages.size, 1);
assert.deepEqual(packages.get("y18n"), {
name: "y18n",
severity: "high",
title: "Prototype Pollution",
Expand All @@ -32,8 +32,8 @@ test("audit()", async () => {

test("audit() - 2", async () => {
const packages = await audit(mockExec(auditJson2));
expect(packages.size).toEqual(1);
expect(packages.get("underscore.string")).toEqual({
assert.equal(packages.size, 1);
assert.deepEqual(packages.get("underscore.string"), {
name: "underscore.string",
severity: "moderate",
title: "Regular Expression Denial of Service",
Expand Down
18 changes: 12 additions & 6 deletions lib/__tests__/buildCommitBody.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import { strict as assert } from "node:assert";
import { readFileSync } from "node:fs";
import { test, expect } from "@jest/globals";
import { test } from "node:test";

import buildCommitBody from "../buildCommitBody.js";

const report = JSON.parse(readFileSync(new URL("./fixtures/report.json", import.meta.url), "utf8"));

test("buildCommitBody()", () => {
expect(buildCommitBody(report)).toEqual(`Summary:
assert.equal(
buildCommitBody(report),
`Summary:
- Updated packages: 2
- Added packages: 1
- Removed packages: 1
Fixed vulnerabilities:
- mocha: "Prototype Pollution" (https://npmjs.com/advisories/1179)
`);
`,
);

expect(buildCommitBody({ added: [], removed: [], updated: [], packageCount: 0, packageUrls: {} }))
.toEqual(`Summary:
assert.equal(
buildCommitBody({ added: [], removed: [], updated: [], packageCount: 0, packageUrls: {} }),
`Summary:
- Updated packages: 0
- Added packages: 0
- Removed packages: 0
No fixed vulnerabilities.
`);
`,
);
});
6 changes: 4 additions & 2 deletions lib/__tests__/buildPullRequestBody.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { strict as assert } from "node:assert";
import { readFileSync } from "node:fs";
import { test, expect } from "@jest/globals";
import { test } from "node:test";

import buildPullRequestBody from "../buildPullRequestBody.js";

const report = JSON.parse(readFileSync(new URL("./fixtures/report.json", import.meta.url), "utf8"));

test("buildPullRequestBody()", () => {
expect(buildPullRequestBody(report, "7.7.0")).toBe(
assert.equal(
buildPullRequestBody(report, "7.7.0"),
`
This pull request fixes the vulnerable packages via npm [7.7.0](https://github.com/npm/cli/releases/tag/v7.7.0).
Expand Down
18 changes: 11 additions & 7 deletions lib/__tests__/listPackages.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { strict as assert } from "node:assert";
import path from "node:path";
import { test, expect } from "@jest/globals";
import { test } from "node:test";

import listPackages from "../listPackages.js";

const fixtures = new URL("./fixtures", import.meta.url).pathname;

test("listPackages()", async () => {
const packages = await listPackages({ silent: true });
expect(packages.size).not.toEqual(0);
expect(packages.get("jest:jest")).toMatch(/^\d+\.\d+\.\d+$/u);
expect(packages.get("@actions/core:@actions/core")).toMatch(/^\d+\.\d+\.\d+$/u);
expect(packages.get("eslint/node_modules/eslint-scope:eslint-scope")).toMatch(/^\d+\.\d+\.\d+$/u);
assert.notEqual(packages.size, 0);
assert.match(String(packages.get("eslint:eslint")), /^\d+\.\d+\.\d+$/u);
assert.match(String(packages.get("@actions/core:@actions/core")), /^\d+\.\d+\.\d+$/u);
assert.match(
String(packages.get("eslint/node_modules/eslint-scope:eslint-scope")),
/^\d+\.\d+\.\d+$/u,
);
});

test("listPackages() with an empty package.json", async () => {
const packages = await listPackages({
silent: true,
cwd: path.join(fixtures, "empty_package.json"),
});
expect(packages.size).toEqual(0);
assert.equal(packages.size, 0);
});

test("listPackages() with a package.json having no version", async () => {
const packages = await listPackages({
silent: true,
cwd: path.join(fixtures, "noversion_package.json"),
});
expect(packages.size).toEqual(0);
assert.equal(packages.size, 0);
});
7 changes: 4 additions & 3 deletions lib/__tests__/npmArgs.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { test, expect } from "@jest/globals";
import { strict as assert } from "node:assert";
import { test } from "node:test";

import npmArgs from "../npmArgs.js";

test("npmArgs() without arguments", () => {
expect(npmArgs()).toEqual(["--ignore-scripts", "--no-progress"]);
assert.deepEqual(npmArgs(), ["--ignore-scripts", "--no-progress"]);
});

test("npmArgs() with arguments", () => {
expect(npmArgs("a", "b")).toEqual(["a", "b", "--ignore-scripts", "--no-progress"]);
assert.deepEqual(npmArgs("a", "b"), ["a", "b", "--ignore-scripts", "--no-progress"]);
});
30 changes: 18 additions & 12 deletions lib/utils/__tests__/commaSeparatedList.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { test, expect } from "@jest/globals";
import { strict as assert } from "node:assert";
import { test } from "node:test";

import commaSeparatedList from "../commaSeparatedList.js";

test("commaSeparatedList()", () => {
expect(commaSeparatedList("")).toEqual([]);
expect(commaSeparatedList(" ")).toEqual([]);
expect(commaSeparatedList(",")).toEqual([]);
expect(commaSeparatedList(" , ")).toEqual([]);
expect(commaSeparatedList("a,")).toEqual(["a"]);
expect(commaSeparatedList("a,b")).toEqual(["a", "b"]);
expect(commaSeparatedList("a ,b")).toEqual(["a", "b"]);
expect(commaSeparatedList("a, b")).toEqual(["a", "b"]);
expect(commaSeparatedList(",a, b")).toEqual(["a", "b"]);
expect(commaSeparatedList("a, b,")).toEqual(["a", "b"]);
expect(commaSeparatedList(" a, b, ")).toEqual(["a", "b"]);
assert.deepEqual(commaSeparatedList(""), []);
assert.deepEqual(commaSeparatedList(" "), []);
assert.deepEqual(commaSeparatedList(","), []);
assert.deepEqual(commaSeparatedList(" , "), []);
assert.deepEqual(commaSeparatedList("a"), ["a"]);
assert.deepEqual(commaSeparatedList("a,"), ["a"]);
assert.deepEqual(commaSeparatedList(",a"), ["a"]);
assert.deepEqual(commaSeparatedList("a,b"), ["a", "b"]);
assert.deepEqual(commaSeparatedList("a ,b"), ["a", "b"]);
assert.deepEqual(commaSeparatedList("a, b"), ["a", "b"]);
assert.deepEqual(commaSeparatedList("a , b"), ["a", "b"]);
assert.deepEqual(commaSeparatedList("a, b,"), ["a", "b"]);
assert.deepEqual(commaSeparatedList("a , b,"), ["a", "b"]);
assert.deepEqual(commaSeparatedList("a, b, "), ["a", "b"]);
assert.deepEqual(commaSeparatedList("a , b, "), ["a", "b"]);
assert.deepEqual(commaSeparatedList(" a, b,"), ["a", "b"]);
});
17 changes: 9 additions & 8 deletions lib/utils/__tests__/semverToNumber.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { test, expect } from "@jest/globals";
import { strict as assert } from "node:assert";
import { test } from "node:test";

import semverToNumber from "../semverToNumber.js";

test("semverToNumber()", () => {
expect(semverToNumber("1.2.3")).toEqual(10203);
expect(semverToNumber("0.0.3")).toEqual(3);
expect(semverToNumber("0.9.3")).toEqual(903);
expect(semverToNumber("0.10.3")).toEqual(1003);
expect(semverToNumber("1.0.3")).toEqual(10003);
expect(semverToNumber("1.2.4")).toEqual(10204);
expect(semverToNumber("1.2.4-beta.1")).toEqual(10204);
assert.equal(semverToNumber("1.2.3"), 10203);
assert.equal(semverToNumber("0.0.3"), 3);
assert.equal(semverToNumber("0.9.3"), 903);
assert.equal(semverToNumber("0.10.3"), 1003);
assert.equal(semverToNumber("1.0.3"), 10003);
assert.equal(semverToNumber("1.2.4"), 10204);
assert.equal(semverToNumber("1.2.4-beta.1"), 10204);
});
Loading

0 comments on commit 80c0424

Please sign in to comment.