Skip to content

Commit

Permalink
feat: treat mocked function as default export (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalBush authored Dec 9, 2023
1 parent 53e724e commit 2462b79
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ test("hard mode", async (t) => {
t.after(() => p.dispose()); // Clean up

const dep = mock.fn(() => "mocked");
p.mock("./dep.js", {
default: dep
});

// NOTE: dep will be wrapped {default: dep} because it's a function.
p.mock("./dep.js", dep);

const instance = await p.import("./target.js");
assert.strictEqual(dep.mock.calls.length, 0);
Expand Down
6 changes: 5 additions & 1 deletion src/pod.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export default class Pod {
}

mock(module, fake) {
//TODO: do we need to do any normalization of the fake? add a default if missing, etc?
//TODO: do we need to do path resolution for module?
if (typeof fake === "function") {
fake = {default: fake};
}

this.#cache.add(module, fake);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/mocker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe("Pod", () => {
assert.deepEqual(await a.default(), "mocked");
});

it("should treat function mocks as a default export", async (t) => {
const p = new Pod();
t.after(() => p.dispose());
p.mock("./b.js", () => "mocked");
const a = await p.import("./scenarios/dynamic/a.js");
assert.deepEqual(await a.default(), "mocked");
});

it("should error when trying to deep strict mocks", async () => {
assert.throws(() => new Pod({deep: true, strict: true}), {
message: "Can't have a deep and strict mock"
Expand Down

0 comments on commit 2462b79

Please sign in to comment.