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

Switch from esmock to node.js mock tracker #446

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prepublishOnly": "rimraf ./dist && npm run build && pkg-ok",
"build": "node ./esbuild.config.js",
"test": "npm run test-only && npm run lint",
"test-only": "glob -c \"node --loader=esmock --no-warnings --test-concurrency 1 --test\" \"test/**/*.test.js\"",
"test-only": "glob -c \"node --test-concurrency 1 --test\" \"test/**/*.test.js\"",
"coverage": "c8 --reporter=lcov npm run test",
"clear:cache": "node ./scripts/clear-cache.js"
},
Expand Down Expand Up @@ -70,8 +70,6 @@
"c8": "^10.1.2",
"cross-env": "^7.0.3",
"esbuild": "^0.24.0",
"eslint-plugin-jsdoc": "^50.6.2",
Copy link
Member

Choose a reason for hiding this comment

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

This dep is needed for the eslint config, you removed it when resolving conflicts i guess (just reinstall it)

Copy link
Author

Choose a reason for hiding this comment

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

reinstalled "eslint-plugin-jsdoc": "^50.6.2", in latest commit

"esmock": "^2.6.7",
"glob": "^11.0.0",
"http-server": "^14.1.1",
"pkg-ok": "^3.0.0",
Expand Down
32 changes: 15 additions & 17 deletions test/commands/scorecard.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Import Node.js Dependencies
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { test } from "node:test";
import { test, mock } from "node:test";
import assert from "node:assert";

// Import Third-party Dependencies
import esmock from "esmock";
import { API_URL } from "@nodesecure/ossf-scorecard-sdk";
import { Ok } from "@openally/result";

// Import Internal Dependencies
import { runProcess } from "../helpers/cliCommandRunner.js";
import { arrayFromAsync, getExpectedScorecardLines } from "../helpers/utils.js";
import * as testingModule from "../../src/commands/scorecard.js";

// CONSTANTS
const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -97,25 +98,22 @@ test("should not display scorecard for unknown repository", async() => {
assert.deepEqual(givenLines, expectedLines, `lines should be ${expectedLines}`);
});

test("should retrieve repository whithin git config", async() => {
const testingModule = await esmock("../../src/commands/scorecard.js", {
fs: {
readFileSync: () => [
"[remote \"origin\"]",
"\turl = [email protected]:myawesome/repository.git"
].join("\n")
}
});
test("should retrieve repository within git config", async() => {
const readFileSyncMock = mock.method(fs, "readFileSync", () => [
"[remote \"origin\"]",
"\turl = [email protected]:myawesome/repository.git"
].join("\n")
);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
].join("\n")
);
].join("\n"));

Copy link
Author

Choose a reason for hiding this comment

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

fixed in latest commit


assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"]));
assert.deepEqual(
testingModule.getCurrentRepository(),
Ok(["myawesome/repository", "github"])
);

readFileSyncMock.restore();
});

test("should not find origin remote", async() => {
const testingModule = await esmock("../../src/commands/scorecard.js", {
fs: {
readFileSync: () => "just one line"
}
});
const result = testingModule.getCurrentRepository();

assert.equal(result.err, true);
Expand Down
62 changes: 36 additions & 26 deletions test/httpServer.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
// Import Node.js Dependencies
import fs from "node:fs";
import fs, { createReadStream } from "node:fs";
import { fileURLToPath } from "node:url";
import { after, before, describe, test } from "node:test";
import { after, before, describe, test, mock } from "node:test";
import { once } from "node:events";
import path from "node:path";
import assert from "node:assert";
import { pipeline as streamPipeline } from "node:stream";

// Import Third-party Dependencies
import { get, post, MockAgent, getGlobalDispatcher, setGlobalDispatcher } from "@myunisoft/httpie";
import zup from "zup";
import * as i18n from "@nodesecure/i18n";
import * as flags from "@nodesecure/flags";
import enableDestroy from "server-destroy";
import esmock from "esmock";
import cacache from "cacache";
import sendType from "@polka/send-type";

// Require Internal Dependencies
import { buildServer } from "../src/http-server/index.js";
import { CACHE_PATH } from "../src/http-server/cache.js";
import * as rootModule from "../src/http-server/endpoints/root.js";
import * as flagsModule from "../src/http-server/endpoints/flags.js";

// CONSTANTS
const HTTP_PORT = 17049;
Expand Down Expand Up @@ -79,20 +82,18 @@ describe("httpServer", { concurrency: 1 }, () => {
assert.equal(result.data, templateStr);
});

test("'/' should fail", async() => {
test("'/' should fail", () => {
const errors = [];
const module = await esmock("../src/http-server/endpoints/root.js", {
"@polka/send-type": {
default: (_res, _status, { error }) => errors.push(error)
}
});
const sendTypeMock = mock.method(sendType, "default", (res, status, { error }) => errors.push(error));

await module.get({}, ({
rootModule.get({}, {
writeHead: () => {
throw new Error("fake error");
}
}));
});

assert.deepEqual(errors, ["fake error"]);
sendTypeMock.mock.restore();
});

test("'/flags' should return the flags list as JSON", async() => {
Expand Down Expand Up @@ -121,20 +122,29 @@ describe("httpServer", { concurrency: 1 }, () => {
});
});

test("'/flags/description/:title' should fail", async() => {
const module = await esmock("../src/http-server/endpoints/flags.js", {
stream: {
pipeline: (_stream, _res, err) => err("fake error")
},
fs: {
createReadStream: () => "foo"
}
});
test("'/flags/description/:title' should fail", () => {
const logs = [];
const streamPipelineMock = mock.method(streamPipeline, "pipeline", (stream, res, err) => err("fake error"));
const createReadStreamMock = mock.method(createReadStream, "default", () => "foo");
console.error = (data) => logs.push(data);

await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true }));

flagsModule.get({ params: { title: "hasWarnings" } }, { writeHead: () => true });

assert.deepEqual(logs, ["fake error"]);

streamPipelineMock.restore();
createReadStreamMock.restore();
});

before(async() => {
const openMock = mock.method(buildServer, "open", () => {
opened = true;
});

httpServer = buildServer(JSON_PATH);
await once(httpServer.server, "listening");
enableDestroy(httpServer.server);
openMock.restore();
});

test("'/data' should return the fixture payload we expect", async() => {
Expand Down Expand Up @@ -323,17 +333,17 @@ describe("httpServer", { concurrency: 1 }, () => {
describe("httpServer without options", () => {
let httpServer;
let opened = false;
// We want to disable WS
process.env.NODE_ENV = "test";

before(async() => {
const module = await esmock("../src/http-server/index.js", {
open: () => (opened = true)
const openMock = mock.method(buildServer, "open", () => {
opened = true;
});

httpServer = module.buildServer(JSON_PATH);
httpServer = buildServer(JSON_PATH);
await once(httpServer.server, "listening");
enableDestroy(httpServer.server);
openMock.mock.restoreAll();
});

after(async() => {
Expand Down
Loading