diff --git a/package.json b/package.json index cbd244c3..cb1edcd2 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -71,7 +71,6 @@ "cross-env": "^7.0.3", "esbuild": "^0.24.0", "eslint-plugin-jsdoc": "^50.6.2", - "esmock": "^2.6.7", "glob": "^11.0.0", "http-server": "^14.1.1", "pkg-ok": "^3.0.0", diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index 58ab6eef..0a0b52c7 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -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)); @@ -97,25 +98,21 @@ 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 = git@github.com:myawesome/repository.git" - ].join("\n") - } - }); +test("should retrieve repository within git config", async() => { + const readFileSyncMock = mock.method(fs, "readFileSync", () => [ + "[remote \"origin\"]", + "\turl = git@github.com:myawesome/repository.git" + ].join("\n")); - 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); diff --git a/test/httpServer.test.js b/test/httpServer.test.js index 08ecc0a6..77ac3651 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -1,10 +1,11 @@ // 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"; @@ -12,12 +13,14 @@ 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; @@ -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() => { @@ -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() => { @@ -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() => {