From a361420068df165a31f076e2aa5fdd06d534ace1 Mon Sep 17 00:00:00 2001 From: Albert Shay Date: Thu, 9 Jan 2025 21:47:48 -0800 Subject: [PATCH 01/10] converted all tests using esmock to Node.js MockTracker --- test/commands/scorecard.test.js | 58 ++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index 58ab6eef..73a5630e 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -1,7 +1,7 @@ // Import Node.js Dependencies 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 @@ -97,27 +97,55 @@ 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 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") +// } +// }); + +// assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); +// }); + +//this test below converts the above 'commentted out' test using the new MockTracker class +test("should retrieve repository within git config", async () => { + mock.module('fs', { + readFileSync: () => [ + "[remote \"origin\"]", + "\turl = git@github.com:myawesome/repository.git" + ].join("\n") }); + const testingModule = await import("../../src/commands/scorecard.js"); + assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); }); -test("should not find origin remote", async() => { - const testingModule = await esmock("../../src/commands/scorecard.js", { - fs: { - readFileSync: () => "just one line" - } +// 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); +// assert.equal(result.val, "Cannot find origin remote."); +// }); + +//this test below converts the above 'commentted out' test using the new MockTracker class +test("should not find origin remote", async () => { + mock.module('fs', { + readFileSync: () => "just one line" }); + + const testingModule = await import("../../src/commands/scorecard.js"); + const result = testingModule.getCurrentRepository(); assert.equal(result.err, true); assert.equal(result.val, "Cannot find origin remote."); -}); +}); \ No newline at end of file From d12af0af68c55f9a7d18777fb2e78f23b818f179 Mon Sep 17 00:00:00 2001 From: Albert Shay Date: Thu, 9 Jan 2025 21:49:54 -0800 Subject: [PATCH 02/10] conversion to MockTracker, including tests in httpSever.test.js --- test/httpServer.test.js | 132 ++++++++++++++++++++++++++++++++-------- 1 file changed, 105 insertions(+), 27 deletions(-) diff --git a/test/httpServer.test.js b/test/httpServer.test.js index f59500d4..ffcffcc5 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -1,7 +1,7 @@ // Import Node.js Dependencies import fs 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"; @@ -79,22 +79,48 @@ describe("httpServer", { concurrency: 1 }, () => { assert.equal(result.data, templateStr); }); - test("'/' should fail", async() => { + // test("'/' should fail", async() => { + // const errors = []; + // const module = await esmock("../src/http-server/endpoints/root.js", { + // "@polka/send-type": { + // default: (res, status, { error }) => errors.push(error) + // } + // }); + + + // await module.get({}, ({ + // writeHead: () => { + // throw new Error("fake error"); + // } + // })); + // assert.deepEqual(errors, ["fake error"]); + // }); + + //this converts the above 'commentted out' test using the new MockTracker class + test("'/' should fail", async () => { const errors = []; - const module = await esmock("../src/http-server/endpoints/root.js", { - "@polka/send-type": { - default: (res, status, { error }) => errors.push(error) - } + + // Mock the "@polka/send-type" module using mock.module + mock.module('@polka/send-type', { + default: (res, status, { error }) => errors.push(error) // Mock the default export function }); - - await module.get({}, ({ + + // Import the module after setting up the mock + const module = await import("../src/http-server/endpoints/root.js"); + + // Run the test logic + await module.get({}, { writeHead: () => { throw new Error("fake error"); - } - })); + }, + }); + + // Verify that the error was correctly pushed into the errors array assert.deepEqual(errors, ["fake error"]); }); + + test("'/flags' should return the flags list as JSON", async() => { const result = await get(new URL("/flags", HTTP_URL)); @@ -121,22 +147,42 @@ 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", async() => { + // const module = await esmock("../src/http-server/endpoints/flags.js", { + // stream: { + // pipeline: (stream, res, err) => err("fake error") + // }, + // fs: { + // createReadStream: () => "foo" + // } + // }); + // const logs = []; + // console.error = (data) => logs.push(data); + + // await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); + // assert.deepEqual(logs, ["fake error"]); + // }); + + //this converts the above 'commentted out' test using the new MockTracker class + test("'/flags/description/:title' should fail", async () => { const logs = []; - console.error = (data) => logs.push(data); - await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); + mock.module('stream', { + pipeline: (stream, res, err) => err("fake error"), + }); + mock.module('fs', { + createReadStream: () => "foo", + }); + + console.error = (data) => logs.push(data); + + const module = await import("../src/http-server/endpoints/flags.js"); + await module.get({ params: { title: "hasWarnings" } }, { writeHead: () => true }); assert.deepEqual(logs, ["fake error"]); }); + + test("'/data' should return the fixture payload we expect", async() => { const result = await get(new URL("/data", HTTP_URL)); @@ -320,23 +366,53 @@ 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) +// }); + +// httpServer = module.buildServer(JSON_PATH); +// await once(httpServer.server, "listening"); +// enableDestroy(httpServer.server); +// }); + +// after(async() => { +// httpServer.server.destroy(); +// }); + +// test("should listen on random port", () => { +// assert.ok(httpServer.server.address().port > 0); +// }); + +// test("should have openLink to true", () => { +// assert.equal(opened, true); +// }); +// }); + +//this converts the above 'commentted out' test using the new MockTracker class 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) + before(async () => { + mock.module("../src/http-server/index.js", { + open: () => (opened = true) }); - httpServer = module.buildServer(JSON_PATH); + const module = await import("../src/http-server/index.js"); + httpServer = module.buildServer(JSON_PATH); await once(httpServer.server, "listening"); enableDestroy(httpServer.server); }); - after(async() => { + after(async () => { httpServer.server.destroy(); }); @@ -349,6 +425,8 @@ describe("httpServer without options", () => { }); }); + + /** * HELPERS */ From e7bc79f2d47d2d4840179c5678fc9881689f3152 Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Sun, 12 Jan 2025 00:45:58 -0800 Subject: [PATCH 03/10] removed all test comments that were migrated and removed all the double jump lines --- test/commands/scorecard.test.js | 4 ---- test/httpServer.test.js | 21 ++------------------- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index 73a5630e..834dc834 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -109,8 +109,6 @@ test("should not display scorecard for unknown repository", async() => { // assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); // }); - -//this test below converts the above 'commentted out' test using the new MockTracker class test("should retrieve repository within git config", async () => { mock.module('fs', { readFileSync: () => [ @@ -135,8 +133,6 @@ test("should retrieve repository within git config", async () => { // assert.equal(result.err, true); // assert.equal(result.val, "Cannot find origin remote."); // }); - -//this test below converts the above 'commentted out' test using the new MockTracker class test("should not find origin remote", async () => { mock.module('fs', { readFileSync: () => "just one line" diff --git a/test/httpServer.test.js b/test/httpServer.test.js index ffcffcc5..78dbe332 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -86,7 +86,7 @@ describe("httpServer", { concurrency: 1 }, () => { // default: (res, status, { error }) => errors.push(error) // } // }); - + // await module.get({}, ({ // writeHead: () => { @@ -95,32 +95,21 @@ describe("httpServer", { concurrency: 1 }, () => { // })); // assert.deepEqual(errors, ["fake error"]); // }); - - //this converts the above 'commentted out' test using the new MockTracker class test("'/' should fail", async () => { const errors = []; - - // Mock the "@polka/send-type" module using mock.module mock.module('@polka/send-type', { - default: (res, status, { error }) => errors.push(error) // Mock the default export function + default: (res, status, { error }) => errors.push(error) }); - // Import the module after setting up the mock const module = await import("../src/http-server/endpoints/root.js"); - - // Run the test logic await module.get({}, { writeHead: () => { throw new Error("fake error"); }, }); - - // Verify that the error was correctly pushed into the errors array assert.deepEqual(errors, ["fake error"]); }); - - test("'/flags' should return the flags list as JSON", async() => { const result = await get(new URL("/flags", HTTP_URL)); @@ -162,8 +151,6 @@ describe("httpServer", { concurrency: 1 }, () => { // await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); // assert.deepEqual(logs, ["fake error"]); // }); - - //this converts the above 'commentted out' test using the new MockTracker class test("'/flags/description/:title' should fail", async () => { const logs = []; @@ -181,8 +168,6 @@ describe("httpServer", { concurrency: 1 }, () => { assert.deepEqual(logs, ["fake error"]); }); - - test("'/data' should return the fixture payload we expect", async() => { const result = await get(new URL("/data", HTTP_URL)); @@ -394,8 +379,6 @@ describe("httpServer", { concurrency: 1 }, () => { // assert.equal(opened, true); // }); // }); - -//this converts the above 'commentted out' test using the new MockTracker class describe("httpServer without options", () => { let httpServer; let opened = false; From 4a7f915516e1cc66365443db4561a6102c7b8e59 Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Sun, 12 Jan 2025 01:26:37 -0800 Subject: [PATCH 04/10] removed mock.module and replaced it with mock.method instead --- test/commands/scorecard.test.js | 30 +++++++++++++---------- test/httpServer.test.js | 43 +++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index 834dc834..a389e3ee 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -110,16 +110,21 @@ test("should not display scorecard for unknown repository", async() => { // assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); // }); test("should retrieve repository within git config", async () => { - mock.module('fs', { - readFileSync: () => [ - "[remote \"origin\"]", - "\turl = git@github.com:myawesome/repository.git" - ].join("\n") - }); - + const fs = await import('fs'); + const readFileSyncMock = mock.method(fs, 'readFileSync', () => + [ + '[remote "origin"]', + '\turl = git@github.com:myawesome/repository.git', + ].join('\n') + ); const testingModule = await import("../../src/commands/scorecard.js"); - assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); + assert.deepEqual( + testingModule.getCurrentRepository(), + Ok(["myawesome/repository", "github"]) + ); + + readFileSyncMock.mock.restoreAll(); }); // test("should not find origin remote", async() => { @@ -134,14 +139,13 @@ test("should retrieve repository within git config", async () => { // assert.equal(result.val, "Cannot find origin remote."); // }); test("should not find origin remote", async () => { - mock.module('fs', { - readFileSync: () => "just one line" - }); - + const fs = await import('fs'); + const readFileSyncMock = mock.method(fs, 'readFileSync', () => "just one line"); const testingModule = await import("../../src/commands/scorecard.js"); - const result = testingModule.getCurrentRepository(); assert.equal(result.err, true); assert.equal(result.val, "Cannot find origin remote."); + + readFileSyncMock.mock.restoreAll(); }); \ No newline at end of file diff --git a/test/httpServer.test.js b/test/httpServer.test.js index 78dbe332..45299884 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -97,17 +97,21 @@ describe("httpServer", { concurrency: 1 }, () => { // }); test("'/' should fail", async () => { const errors = []; - mock.module('@polka/send-type', { - default: (res, status, { error }) => errors.push(error) - }); - + const sendTypeMock = mock.method( + await import('@polka/send-type'), + 'default', + (res, status, { error }) => errors.push(error) + ); + const module = await import("../src/http-server/endpoints/root.js"); + await module.get({}, { writeHead: () => { throw new Error("fake error"); }, }); assert.deepEqual(errors, ["fake error"]); + sendTypeMock.mock.restoreAll(); }); test("'/flags' should return the flags list as JSON", async() => { @@ -153,19 +157,26 @@ describe("httpServer", { concurrency: 1 }, () => { // }); test("'/flags/description/:title' should fail", async () => { const logs = []; - - mock.module('stream', { - pipeline: (stream, res, err) => err("fake error"), - }); - mock.module('fs', { - createReadStream: () => "foo", - }); + const streamPipelineMock = mock.method( + await import('stream'), + 'pipeline', + (stream, res, err) => err("fake error") + ); + + const createReadStreamMock = mock.method( + await import('fs'), + 'createReadStream', + () => "foo" + ); console.error = (data) => logs.push(data); const module = await import("../src/http-server/endpoints/flags.js"); await module.get({ params: { title: "hasWarnings" } }, { writeHead: () => true }); + assert.deepEqual(logs, ["fake error"]); + streamPipelineMock.mock.restoreAll(); + createReadStreamMock.mock.restoreAll(); }); test("'/data' should return the fixture payload we expect", async() => { @@ -385,14 +396,16 @@ describe("httpServer without options", () => { process.env.NODE_ENV = "test"; before(async () => { - mock.module("../src/http-server/index.js", { - open: () => (opened = true) + const module = await import("../src/http-server/index.js"); + const openMock = mock.method(module, 'open', () => { + opened = true; }); - const module = await import("../src/http-server/index.js"); - httpServer = module.buildServer(JSON_PATH); + httpServer = module.buildServer(JSON_PATH); await once(httpServer.server, "listening"); enableDestroy(httpServer.server); + + openMock.mock.restoreAll(); }); after(async () => { From 464bfa264a75a847fd1a619c1d4851161e9f1a70 Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Sun, 12 Jan 2025 04:55:19 -0800 Subject: [PATCH 05/10] removed all dynamic imports in scorecard.test.js and httpServer.test.js and added the imports at the top of each file --- test/commands/scorecard.test.js | 8 +++--- test/httpServer.test.js | 47 +++++++++++++-------------------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index a389e3ee..d434e158 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -1,4 +1,5 @@ // Import Node.js Dependencies +import fs from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; import { test, mock } from "node:test"; @@ -12,6 +13,7 @@ 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)); @@ -110,15 +112,13 @@ test("should not display scorecard for unknown repository", async() => { // assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); // }); test("should retrieve repository within git config", async () => { - const fs = await import('fs'); const readFileSyncMock = mock.method(fs, 'readFileSync', () => [ '[remote "origin"]', '\turl = git@github.com:myawesome/repository.git', ].join('\n') ); - const testingModule = await import("../../src/commands/scorecard.js"); - + assert.deepEqual( testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"]) @@ -139,9 +139,7 @@ test("should retrieve repository within git config", async () => { // assert.equal(result.val, "Cannot find origin remote."); // }); test("should not find origin remote", async () => { - const fs = await import('fs'); const readFileSyncMock = mock.method(fs, 'readFileSync', () => "just one line"); - const testingModule = await import("../../src/commands/scorecard.js"); const result = testingModule.getCurrentRepository(); assert.equal(result.err, true); diff --git a/test/httpServer.test.js b/test/httpServer.test.js index 45299884..d1097564 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, 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"; @@ -14,10 +15,15 @@ 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"; +import * as indexModule from "../src/http-server/index.js"; + // CONSTANTS const HTTP_PORT = 17049; @@ -95,21 +101,16 @@ describe("httpServer", { concurrency: 1 }, () => { // })); // assert.deepEqual(errors, ["fake error"]); // }); - test("'/' should fail", async () => { + test("'/' should fail", () => { const errors = []; - const sendTypeMock = mock.method( - await import('@polka/send-type'), - 'default', - (res, status, { error }) => errors.push(error) - ); - - const module = await import("../src/http-server/endpoints/root.js"); + 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.restoreAll(); }); @@ -155,26 +156,16 @@ describe("httpServer", { concurrency: 1 }, () => { // await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); // assert.deepEqual(logs, ["fake error"]); // }); - test("'/flags/description/:title' should fail", async () => { + test("'/flags/description/:title' should fail", () => { const logs = []; - - const streamPipelineMock = mock.method( - await import('stream'), - 'pipeline', - (stream, res, err) => err("fake error") - ); - - const createReadStreamMock = mock.method( - await import('fs'), - 'createReadStream', - () => "foo" - ); + 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); - const module = await import("../src/http-server/endpoints/flags.js"); - await module.get({ params: { title: "hasWarnings" } }, { writeHead: () => true }); + flagsModule.get({ params: { title: "hasWarnings" } }, { writeHead: () => true }); assert.deepEqual(logs, ["fake error"]); + streamPipelineMock.mock.restoreAll(); createReadStreamMock.mock.restoreAll(); }); @@ -396,15 +387,13 @@ describe("httpServer without options", () => { process.env.NODE_ENV = "test"; before(async () => { - const module = await import("../src/http-server/index.js"); - const openMock = mock.method(module, 'open', () => { + const openMock = mock.method(indexModule, "open", () => { opened = true; }); - httpServer = module.buildServer(JSON_PATH); + httpServer = indexModule.buildServer(JSON_PATH); await once(httpServer.server, "listening"); enableDestroy(httpServer.server); - openMock.mock.restoreAll(); }); From b572ab9e81442657890a7f0c62083a44d1e9271c Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Thu, 23 Jan 2025 21:35:21 -0800 Subject: [PATCH 06/10] uninstalled esmock, changed test script in package.json, removed esmock imports and previous commented out tests, removed readFileSyncMock.mock.restoreAll() in the last test for scorecard.test.js file, fixed all async spacing, and removed indexModule and used buildServerimport instead --- package.json | 3 +- test/commands/scorecard.test.js | 30 +------------- test/httpServer.test.js | 69 ++------------------------------- 3 files changed, 7 insertions(+), 95 deletions(-) diff --git a/package.json b/package.json index d3a12dbf..153e516c 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" }, @@ -70,7 +70,6 @@ "c8": "^10.1.2", "cross-env": "^7.0.3", "esbuild": "^0.24.0", - "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 d434e158..a316ad37 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -6,7 +6,6 @@ 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"; @@ -99,19 +98,7 @@ 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") -// } -// }); - -// assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); -// }); -test("should retrieve repository within git config", async () => { +test("should retrieve repository within git config", async() => { const readFileSyncMock = mock.method(fs, 'readFileSync', () => [ '[remote "origin"]', @@ -127,23 +114,10 @@ test("should retrieve repository within git config", async () => { readFileSyncMock.mock.restoreAll(); }); -// 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); -// assert.equal(result.val, "Cannot find origin remote."); -// }); -test("should not find origin remote", async () => { +test("should not find origin remote", async() => { const readFileSyncMock = mock.method(fs, 'readFileSync', () => "just one line"); const result = testingModule.getCurrentRepository(); assert.equal(result.err, true); assert.equal(result.val, "Cannot find origin remote."); - - readFileSyncMock.mock.restoreAll(); }); \ No newline at end of file diff --git a/test/httpServer.test.js b/test/httpServer.test.js index d1097564..98727d60 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -13,7 +13,6 @@ 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"; @@ -22,7 +21,6 @@ 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"; -import * as indexModule from "../src/http-server/index.js"; // CONSTANTS @@ -85,22 +83,6 @@ describe("httpServer", { concurrency: 1 }, () => { assert.equal(result.data, templateStr); }); - // test("'/' should fail", async() => { - // const errors = []; - // const module = await esmock("../src/http-server/endpoints/root.js", { - // "@polka/send-type": { - // default: (res, status, { error }) => errors.push(error) - // } - // }); - - - // await module.get({}, ({ - // writeHead: () => { - // throw new Error("fake error"); - // } - // })); - // assert.deepEqual(errors, ["fake error"]); - // }); test("'/' should fail", () => { const errors = []; const sendTypeMock = mock.method(sendType, "default", (res, status, { error }) => errors.push(error)); @@ -141,21 +123,6 @@ 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" - // } - // }); - // const logs = []; - // console.error = (data) => logs.push(data); - - // await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); - // assert.deepEqual(logs, ["fake error"]); - // }); test("'/flags/description/:title' should fail", () => { const logs = []; const streamPipelineMock = mock.method(streamPipeline, "pipeline", (stream, res, err) => err("fake error")); @@ -353,51 +320,23 @@ 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) -// }); - -// httpServer = module.buildServer(JSON_PATH); -// await once(httpServer.server, "listening"); -// enableDestroy(httpServer.server); -// }); - -// after(async() => { -// httpServer.server.destroy(); -// }); - -// test("should listen on random port", () => { -// assert.ok(httpServer.server.address().port > 0); -// }); - -// test("should have openLink to true", () => { -// assert.equal(opened, true); -// }); -// }); describe("httpServer without options", () => { let httpServer; let opened = false; process.env.NODE_ENV = "test"; - before(async () => { - const openMock = mock.method(indexModule, "open", () => { + before(async() => { + const openMock = mock.method(buildServer, "open", () => { opened = true; }); - httpServer = indexModule.buildServer(JSON_PATH); + httpServer = buildServer(JSON_PATH); await once(httpServer.server, "listening"); enableDestroy(httpServer.server); openMock.mock.restoreAll(); }); - after(async () => { + after(async() => { httpServer.server.destroy(); }); From 3f025910b5c7c434bb1a2b2c5519d6da0ec78238 Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Fri, 24 Jan 2025 06:09:46 -0800 Subject: [PATCH 07/10] removed unassigned value readFileSyncMock in the last test of commands/scorecard.test.js file --- test/commands/scorecard.test.js | 14 ++++++-------- test/httpServer.test.js | 13 +++++-------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index a316ad37..fdaf9b11 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -99,13 +99,12 @@ test("should not display scorecard for unknown repository", async() => { }); 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') + 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"]) @@ -115,9 +114,8 @@ test("should retrieve repository within git config", async() => { }); test("should not find origin remote", async() => { - const readFileSyncMock = mock.method(fs, 'readFileSync', () => "just one line"); const result = testingModule.getCurrentRepository(); assert.equal(result.err, true); assert.equal(result.val, "Cannot find origin remote."); -}); \ No newline at end of file +}); diff --git a/test/httpServer.test.js b/test/httpServer.test.js index 98727d60..cb912749 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -22,7 +22,6 @@ 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; const HTTP_URL = new URL(`http://localhost:${HTTP_PORT}`); @@ -86,13 +85,13 @@ describe("httpServer", { concurrency: 1 }, () => { test("'/' should fail", () => { const errors = []; const sendTypeMock = mock.method(sendType, "default", (res, status, { error }) => errors.push(error)); - + rootModule.get({}, { writeHead: () => { throw new Error("fake error"); - }, + } }); - + assert.deepEqual(errors, ["fake error"]); sendTypeMock.mock.restoreAll(); }); @@ -128,9 +127,9 @@ describe("httpServer", { concurrency: 1 }, () => { 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); - + flagsModule.get({ params: { title: "hasWarnings" } }, { writeHead: () => true }); - + assert.deepEqual(logs, ["fake error"]); streamPipelineMock.mock.restoreAll(); @@ -349,8 +348,6 @@ describe("httpServer without options", () => { }); }); - - /** * HELPERS */ From f5a9ec367a534941d4feede375334915d3a8497e Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Fri, 24 Jan 2025 08:19:56 -0800 Subject: [PATCH 08/10] replacing mock.restoreAll with mock.restore in both files scorecard.test.js and httpServer.test.js --- test/commands/scorecard.test.js | 2 +- test/httpServer.test.js | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index fdaf9b11..12258bfb 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -110,7 +110,7 @@ test("should retrieve repository within git config", async() => { Ok(["myawesome/repository", "github"]) ); - readFileSyncMock.mock.restoreAll(); + readFileSyncMock.restore(); }); test("should not find origin remote", async() => { diff --git a/test/httpServer.test.js b/test/httpServer.test.js index cb912749..77ac3651 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -93,7 +93,7 @@ describe("httpServer", { concurrency: 1 }, () => { }); assert.deepEqual(errors, ["fake error"]); - sendTypeMock.mock.restoreAll(); + sendTypeMock.mock.restore(); }); test("'/flags' should return the flags list as JSON", async() => { @@ -127,13 +127,24 @@ describe("httpServer", { concurrency: 1 }, () => { 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); - + flagsModule.get({ params: { title: "hasWarnings" } }, { writeHead: () => true }); - + assert.deepEqual(logs, ["fake error"]); - - streamPipelineMock.mock.restoreAll(); - createReadStreamMock.mock.restoreAll(); + + 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() => { From 54e6409085d71736eff0367b83941bbb566f8557 Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Fri, 24 Jan 2025 08:36:27 -0800 Subject: [PATCH 09/10] reinstalled eslint-plugin-jsdoc: ^50.6.2, and added line 105 --- package.json | 1 + test/commands/scorecard.test.js | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0f1ee70f..cb1edcd2 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "c8": "^10.1.2", "cross-env": "^7.0.3", "esbuild": "^0.24.0", + "eslint-plugin-jsdoc": "^50.6.2", "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 12258bfb..0a0b52c7 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -102,8 +102,7 @@ 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") - ); + ].join("\n")); assert.deepEqual( testingModule.getCurrentRepository(), From ed8096b752cc4061eb2aad85728ce628e6a3bcb9 Mon Sep 17 00:00:00 2001 From: albertshay888 Date: Mon, 3 Feb 2025 04:20:54 -0800 Subject: [PATCH 10/10] replaced restore with mockRestore to see if test will pass --- test/commands/scorecard.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index 0a0b52c7..02b94cd7 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -109,7 +109,7 @@ test("should retrieve repository within git config", async() => { Ok(["myawesome/repository", "github"]) ); - readFileSyncMock.restore(); + readFileSyncMock.mockRestore(); }); test("should not find origin remote", async() => {