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 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
58 changes: 43 additions & 15 deletions test/commands/scorecard.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = [email protected]: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 = [email protected]: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 = [email protected]: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.");
});
});
132 changes: 105 additions & 27 deletions test/httpServer.test.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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();
});

Expand All @@ -349,6 +425,8 @@ describe("httpServer without options", () => {
});
});



/**
* HELPERS
*/
Expand Down
Loading