Skip to content

Commit 425b267

Browse files
committed
A bit of renaming.
1 parent b51a7d7 commit 425b267

File tree

7 files changed

+42
-40
lines changed

7 files changed

+42
-40
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The default export is a function that sets up a mock environment, imports the ta
4343

4444
## Hard Mode
4545

46-
If you need more control over the mock lifecycle (like dynamic imports), then you'll need to use the `Mocker` class directly.
46+
If you need more control over the mock lifecycle (like dynamic imports), then you'll need to use the `Pod` class directly.
4747

4848
Let's consider a few files:
4949

@@ -68,18 +68,18 @@ Now we can write a test that handles the dynamic import.
6868
import assert from "node:assert";
6969
import {test, mock} from "node:test";
7070

71-
import {Mocker} from "modpod";
71+
import {Pod} from "modpod";
7272

7373
test("hard mode", async (t) => {
74-
const m = new Mocker({strict: true});
75-
t.after(() => m.dispose()); // Clean up
74+
const p = new Pod({strict: true});
75+
t.after(() => p.dispose()); // Clean up
7676

7777
const dep = mock.fn(() => "mocked");
78-
m.mock("./dep.js", {
78+
p.mock("./dep.js", {
7979
default: dep
8080
});
8181

82-
const instance = await m.import("./target.js");
82+
const instance = await p.import("./target.js");
8383
assert.strictEqual(dep.mock.calls.length, 0);
8484

8585
const result = await instance.doSomething();

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import "./src/register.js";
22
import internal from "./src/internal.js";
3-
import Mocker from "./src/mocker.js";
3+
import Pod from "./src/pod.js";
44

55
async function _mock(module, mocks, opts) {
6-
const m = new Mocker(opts, {[internal]: 3});
6+
const p = new Pod(opts, {[internal]: 3});
77
try {
88
for (const [k, v] of Object.entries(mocks)) {
9-
m.mock(k, v);
9+
p.mock(k, v);
1010
}
11-
const instance = await m.import(module);
11+
const instance = await p.import(module);
1212
return instance;
1313
} finally {
14-
m.dispose();
14+
p.dispose();
1515
}
1616
}
1717

@@ -21,4 +21,4 @@ mock.strict = (module, mocks) => _mock(module, mocks, {strict: true});
2121

2222
export default mock;
2323

24-
export {Mocker};
24+
export {Pod};

src/getModuleUrl.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import {describe, it} from "node:test";
22
import assert from "node:assert/strict";
33
import path from "node:path";
44

5-
import {Mocker} from "../index.js"; // We're gonna test with our own mocker. 😍
5+
import {Pod} from "../index.js"; // We're gonna test using our own mocker. 😍
66

77
describe("getModuleUrl", () => {
88
it("should error if no package.json is found", async (t) => {
9-
const m = new Mocker();
10-
t.after(() => m.dispose());
9+
const p = new Pod();
10+
t.after(() => p.dispose());
1111

12-
m.mock("node:fs/promises", {
12+
p.mock("node:fs/promises", {
1313
default: {
1414
stat: () =>
1515
Promise.resolve({
@@ -18,24 +18,24 @@ describe("getModuleUrl", () => {
1818
}
1919
});
2020

21-
const {default: getModuleUrl} = await m.import("./getModuleUrl.js");
21+
const {default: getModuleUrl} = await p.import("./getModuleUrl.js");
2222

2323
await assert.rejects(() => getModuleUrl("#/lol.js", import.meta.url), {
2424
message: `Can't locate package.json from ${path.dirname(import.meta.url)}`
2525
});
2626
});
2727

2828
it("should error if error is not ENOENT", async (t) => {
29-
const m = new Mocker();
30-
t.after(() => m.dispose());
29+
const p = new Pod();
30+
t.after(() => p.dispose());
3131

32-
m.mock("node:fs/promises", {
32+
p.mock("node:fs/promises", {
3333
default: {
3434
stat: () => Promise.reject(new Error("boom"))
3535
}
3636
});
3737

38-
const {default: getModuleUrl} = await m.import("./getModuleUrl.js");
38+
const {default: getModuleUrl} = await p.import("./getModuleUrl.js");
3939

4040
await assert.rejects(() => getModuleUrl("#/lol.js", import.meta.url), {
4141
message: "boom"

src/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function resolve(module, context, nextResolve) {
3838
throw new Error(`Strict mode missing mock: "${module}"`);
3939
} else if (isMissing) {
4040
throw new Error(
41-
`Late dynamic import: "${module}". Use the Mocker directly and call dispose after your test is done.`
41+
`Late dynamic import: "${module}". Use the Pod class directly and call dispose after your test is done.`
4242
);
4343
}
4444
if (deep && canRewrite(module)) {

src/mocker.js renamed to src/pod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import internal from "./internal.js";
55
import Cache from "./cache.js";
66
import getModuleUrl from "./getModuleUrl.js";
77

8-
export default class Mocker {
8+
export default class Pod {
99
#cache;
1010
#caller;
1111
#options;

tests/mocker.test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import {describe, it} from "node:test";
22
import assert from "node:assert/strict";
33
import {fileURLToPath} from "node:url";
4-
import {Mocker} from "../index.js";
4+
import {Pod} from "../index.js";
55

6-
describe("Mocker", () => {
6+
describe("Pod", () => {
77
[
88
["file://", import.meta.resolve("./scenarios/linear/a.js")],
99
["absolute", fileURLToPath(import.meta.resolve("./scenarios/linear/a.js"))],
1010
["relative", "./scenarios/linear/a.js"],
1111
["package imports", "#root/tests/scenarios/linear/a.js"]
1212
].forEach(([desc, file]) => {
1313
it(`should import from ${desc} path`, async (t) => {
14-
const m = new Mocker();
15-
t.after(() => m.dispose());
14+
const p = new Pod();
15+
t.after(() => p.dispose());
1616

17-
m.mock("./b.js", {
17+
p.mock("./b.js", {
1818
default: {mocked: "default"},
1919
named: {mocked: "named"}
2020
});
2121

22-
const a = await m.import(file);
22+
const a = await p.import(file);
2323

2424
assert.deepEqual(a.default, {
2525
a: true,
@@ -34,38 +34,38 @@ describe("Mocker", () => {
3434
});
3535

3636
it("should mock late dynamic imports", async (t) => {
37-
const m = new Mocker();
38-
t.after(() => m.dispose());
39-
m.mock("./b.js", {
37+
const p = new Pod();
38+
t.after(() => p.dispose());
39+
p.mock("./b.js", {
4040
default: () => "mocked"
4141
});
42-
const a = await m.import("./scenarios/dynamic/a.js");
42+
const a = await p.import("./scenarios/dynamic/a.js");
4343
assert.deepEqual(await a.default(), "mocked");
4444
});
4545

4646
it("should error when trying to deep strict mocks", async () => {
47-
assert.throws(() => new Mocker({deep: true, strict: true}), {
47+
assert.throws(() => new Pod({deep: true, strict: true}), {
4848
message: "Can't have a deep and strict mock"
4949
});
5050
});
5151

5252
it("should error with unknown options", async () => {
53-
assert.throws(() => new Mocker({foo: "bar"}), {
53+
assert.throws(() => new Pod({foo: "bar"}), {
5454
message: 'Unknown options: {"foo":"bar"}'
5555
});
5656
});
5757

5858
it("should error with unused mocks", async () => {
59-
const m = new Mocker();
59+
const p = new Pod();
6060

61-
m.mock("./lol.js", {
61+
p.mock("./lol.js", {
6262
default: {mocked: "default"},
6363
named: {mocked: "named"}
6464
});
6565

66-
await m.import(import.meta.resolve("./scenarios/linear/a.js"));
66+
await p.import(import.meta.resolve("./scenarios/linear/a.js"));
6767

68-
assert.throws(() => m.dispose(), {
68+
assert.throws(() => p.dispose(), {
6969
message: 'Unused mocks: "./lol.js"'
7070
});
7171
});

tests/shallow.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ describe("shallow (default)", () => {
4646

4747
it("should fail with late dynamic import", async () => {
4848
const a = await mock("./scenarios/dynamic/a.js", {});
49-
await assert.rejects(() => a.default());
49+
await assert.rejects(() => a.default(), {
50+
message: 'Late dynamic import: "./b.js". Use the Pod class directly and call dispose after your test is done.'
51+
});
5052
});
5153
});

0 commit comments

Comments
 (0)