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 4 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
66 changes: 47 additions & 19 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")
}
});

assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"]));
// 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"]));
// });
test("should retrieve repository within git config", async () => {
const fs = await import('fs');
const readFileSyncMock = mock.method(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"])
);

readFileSyncMock.mock.restoreAll();
});

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.");
// });
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);
assert.equal(result.val, "Cannot find origin remote.");
});

readFileSyncMock.mock.restoreAll();
});
126 changes: 100 additions & 26 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,20 +79,39 @@ 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"]);
// });
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)
}
});
const sendTypeMock = mock.method(
await import('@polka/send-type'),
'default',
(res, status, { error }) => errors.push(error)
);

await module.get({}, ({
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() => {
Expand Down Expand Up @@ -121,20 +140,43 @@ 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"]);
// });
test("'/flags/description/:title' should fail", async () => {
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"
);
console.error = (data) => logs.push(data);

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

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() => {
Expand Down Expand Up @@ -320,23 +362,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);
// });
// });
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 () => {
const module = await import("../src/http-server/index.js");
const openMock = mock.method(module, 'open', () => {
opened = true;
});

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

openMock.mock.restoreAll();
});

after(async() => {
after(async () => {
httpServer.server.destroy();
});

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



/**
* HELPERS
*/
Expand Down