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 5 commits into
base: master
Choose a base branch
from

Conversation

albertshay888
Copy link

converted all tests that uses esmock to Node.js MockTracker

@fraxken
Copy link
Member

fraxken commented Jan 10, 2025

Hi @albertshay888, can you remove the tests commented (what you migrated) ? Thanks

test/httpServer.test.js Outdated Show resolved Hide resolved
test/commands/scorecard.test.js Show resolved Hide resolved
@PierreDemailly
Copy link
Member

mock.module is for v23 only. Please use mock.method instead; it'll work for mocking Node.js APIs. We'll probably migrate to mock.module when v24 will be active (Nov 2025) (we don't support odd-numbered releases as they don't enter LTS).

Also, you don't need to do dynamic imports after mocking, you can drop these and import what needs to be tested classically at the top of the file.

…js and added the imports at the top of each file
Copy link
Member

@PierreDemailly PierreDemailly left a comment

Choose a reason for hiding this comment

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

You can also uninstall esmock (npm un esmock) and change the test script from
glob -c \"node --loader=esmock --no-warnings --test-concurrency 1 --test\" \"test/**/*.test.js\"
to
glob -c \"node --test-concurrency 1 --test\" \"test/**/*.test.js\"

Note that there are little conflicts, you can sync your fork and rebase with the main branch

Comment on lines +102 to +113
// 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"]));
// });
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
// 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"]));
// });

Comment on lines +130 to +140
// 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.");
// });
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
// 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.");
// });

});

readFileSyncMock.mock.restoreAll();
});
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
});
});


// 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";
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
import * as indexModule from "../src/http-server/index.js";

There is already the import { buildServer } from "../src/http-server/index.js";
So you can use buildServer() instead of indexModule.buildServer()

});

after(async() => {
after(async () => {
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
after(async () => {
after(async() => {

Make sure you have the linter configured in your IDE, you can also run npm run lint-fix (there are some lint errors in the code)

Comment on lines +88 to +103
// 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"]);
// });
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
// 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"]);
// });

Comment on lines +144 to +158
// 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"]);
// });
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
// 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"]);
// });

Comment on lines +356 to +383
// 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);
// });
// });
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
// 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);
// });
// });

Comment on lines +413 to +414


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants