-
Notifications
You must be signed in to change notification settings - Fork 39
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
base: master
Are you sure you want to change the base?
Switch from esmock to node.js mock tracker #446
Conversation
Hi @albertshay888, can you remove the tests commented (what you migrated) ? Thanks |
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
There was a problem hiding this 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
// 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"])); | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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 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."); | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}); | |
}); | |
|
||
// 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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
// 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"]); | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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("'/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"]); | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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"]); | |
// }); |
// 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); | ||
// }); | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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); | |
// }); | |
// }); |
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
converted all tests that uses esmock to Node.js MockTracker