Skip to content

Commit 9ed6f50

Browse files
committed
[skip ci] add esmock, migrate more 03 and 06
migrate 03.01 ESM default and named export interception migrate 06.02
1 parent 90cd829 commit 9ed6f50

10 files changed

Lines changed: 148 additions & 83 deletions

package-lock.json

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"supertest": "^6.3.3"
2828
},
2929
"devDependencies": {
30-
"@biomejs/biome": "1.5.1"
30+
"@biomejs/biome": "1.5.1",
31+
"esmock": "2.6.2"
3132
}
3233
}

src/03.01-esm-default-mock.test.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/03.01-esm-named-mock.test.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/migrated/02.02-not-to-be-have-been-called.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test("mock.fn() mock.callCount()/mock.calls.length", () => {
1212
assert.equal(stub.mock.callCount(), 0);
1313
assert.equal(stub.mock.calls.length, 0);
1414
});
15+
1516
test("mock.method() mock.callCount()/mock.calls.length", () => {
1617
const somethingSpy = mock.method(myObject, "doSomething", () => {});
1718
assert.equal(somethingSpy.mock.callCount(), 0);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
import esmock from "esmock";
4+
5+
const mockDb = {
6+
get: mock.fn(),
7+
set: mock.fn(),
8+
};
9+
10+
test("ESM Default Export > addTodo > inserts with new id", async () => {
11+
const { default: lib } = await esmock("./03.01-lib.esm", {
12+
"./03.01-db.js": {
13+
...mockDb,
14+
default: mockDb,
15+
},
16+
});
17+
await lib.addTodo({ name: "new todo" });
18+
assert.deepEqual(mockDb.set.mock.calls[0].arguments, [
19+
"todos:1",
20+
{
21+
name: "new todo",
22+
id: 1,
23+
},
24+
]);
25+
});
26+
27+
test("ESM Default Export > getTodo > returns output of db.get", async () => {
28+
mockDb.get.mock.mockImplementationOnce(() => ({
29+
id: 1,
30+
name: "todo-1",
31+
}));
32+
33+
const expected = {
34+
id: 1,
35+
name: "todo-1",
36+
};
37+
38+
const { default: lib } = await esmock("./03.01-lib.esm", {
39+
"./03.01-db.js": {
40+
...mockDb,
41+
default: mockDb,
42+
},
43+
});
44+
const actual = await lib.getTodo(1);
45+
46+
assert.deepEqual(mockDb.get.mock.calls[0].arguments, ["todos:1"]);
47+
assert.deepEqual(actual, expected);
48+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
import esmock from "esmock";
4+
5+
const mockDb = {
6+
get: mock.fn(),
7+
set: mock.fn(),
8+
};
9+
10+
test("ESM Named Export > addTodo > inserts with new id", async () => {
11+
const { addTodo } = await esmock("./03.01-lib.esm", {
12+
"./03.01-db.js": {
13+
...mockDb,
14+
default: mockDb,
15+
},
16+
});
17+
await addTodo({ name: "new todo" });
18+
assert.deepEqual(mockDb.set.mock.calls[0].arguments, [
19+
"todos:1",
20+
{
21+
name: "new todo",
22+
id: 1,
23+
},
24+
]);
25+
});
26+
27+
test("ESM Named Export > getTodo > returns output of db.get", async () => {
28+
mockDb.get.mock.mockImplementationOnce(() => ({
29+
id: 1,
30+
name: "todo-1",
31+
}));
32+
33+
const expected = {
34+
id: 1,
35+
name: "todo-1",
36+
};
37+
38+
const { getTodo } = await esmock("./03.01-lib.esm", {
39+
"./03.01-db.js": {
40+
...mockDb,
41+
default: mockDb,
42+
},
43+
});
44+
const actual = await getTodo(1);
45+
46+
assert.deepEqual(mockDb.get.mock.calls[0].arguments, ["todos:1"]);
47+
assert.deepEqual(actual, expected);
48+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import db from "./03.01-db";
1+
import db from "./03.01-db.js";
22

33
const keyPrefix = "todos";
44
const makeKey = (key) => `${keyPrefix}:${key}`;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const express = require("express");
2-
const request = require("supertest");
1+
// @ts-nocheck
2+
import express, { Router } from "express";
3+
import request from "supertest";
34

4-
const blobStore = (redisClient, router = new express.Router()) => {
5+
const blobStore = (redisClient, router = new Router()) => {
56
router.get("/store/:key", async (req, res) => {
67
const { key } = req.params;
78
const value = req.query;
@@ -16,12 +17,17 @@ const blobStore = (redisClient, router = new express.Router()) => {
1617
return router;
1718
};
1819

19-
const initBlobStore = (
20-
mockRedisClient = {
21-
getAsync: jest.fn(() => Promise.resolve()),
22-
setAsync: jest.fn(() => Promise.resolve()),
23-
},
24-
) => {
20+
import { describe, test, mock } from "node:test";
21+
import assert from "node:assert/strict";
22+
23+
const defaultRedisClient = {
24+
getAsync: mock.fn(),
25+
setAsync: mock.fn(),
26+
};
27+
defaultRedisClient.getAsync.mock.mockImplementation(() => Promise.resolve());
28+
defaultRedisClient.setAsync.mock.mockImplementation(() => Promise.resolve());
29+
30+
const initBlobStore = (mockRedisClient = defaultRedisClient) => {
2531
const app = express();
2632
app.use(blobStore(mockRedisClient));
2733
return app;
@@ -30,32 +36,41 @@ const initBlobStore = (
3036
describe("GET /store/:key with params", () => {
3137
test("It should call redisClient.setAsync with key route parameter as key and stringified params as value", async () => {
3238
const mockRedisClient = {
33-
setAsync: jest.fn(() => Promise.resolve()),
39+
setAsync: mock.fn(),
3440
};
41+
mockRedisClient.setAsync.mock.mockImplementation(() => Promise.resolve());
3542
const app = initBlobStore(mockRedisClient);
3643
await request(app).get("/store/my-key?hello=world&foo=bar");
37-
expect(mockRedisClient.setAsync).toHaveBeenCalledWith(
44+
assert.deepEqual(mockRedisClient.setAsync.mock.calls[0].arguments, [
3845
"my-key",
3946
'{"hello":"world","foo":"bar"}',
40-
);
47+
]);
4148
});
4249
});
4350

4451
describe("GET /:key", () => {
4552
test("It should call redisClient.getAsync with key route parameter as key", async () => {
4653
const mockRedisClient = {
47-
getAsync: jest.fn(() => Promise.resolve("{}")),
54+
getAsync: mock.fn(),
4855
};
56+
mockRedisClient.getAsync.mock.mockImplementation(() =>
57+
Promise.resolve("{}"),
58+
);
4959
const app = initBlobStore(mockRedisClient);
5060
await request(app).get("/my-key");
51-
expect(mockRedisClient.getAsync).toHaveBeenCalledWith("my-key");
61+
assert.deepEqual(mockRedisClient.getAsync.mock.calls[0].arguments, [
62+
"my-key",
63+
]);
5264
});
5365
test("It should return output of redisClient.getAsync with key route parameter as key", async () => {
5466
const mockRedisClient = {
55-
getAsync: jest.fn(() => Promise.resolve("{}")),
67+
getAsync: mock.fn(),
5668
};
69+
mockRedisClient.getAsync.mock.mockImplementation(() =>
70+
Promise.resolve("{}"),
71+
);
5772
const app = initBlobStore(mockRedisClient);
5873
const response = await request(app).get("/my-key");
59-
expect(response.body).toEqual({});
74+
assert.deepEqual(response.body, {});
6075
});
6176
});

0 commit comments

Comments
 (0)