Skip to content

Commit bae3405

Browse files
committed
switch from moxios to nock
1 parent e41b1d1 commit bae3405

4 files changed

Lines changed: 123 additions & 33 deletions

File tree

package-lock.json

Lines changed: 94 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
@@ -28,6 +28,7 @@
2828
},
2929
"devDependencies": {
3030
"@biomejs/biome": "1.5.1",
31-
"esmock": "2.6.2"
31+
"esmock": "2.6.2",
32+
"nock": "^13.4.0"
3233
}
3334
}

src/03.02-notifications.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test("access tests", async () => {
4949
},
5050
);
5151

52-
sendNotification()
52+
sendNotification();
5353

5454
assert.equal(sendNotification.mock.callCount(), 1);
5555
});

src/06.02-hugo.test.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { test, describe, beforeEach, afterEach } from "node:test";
1+
// @ts-nocheck
2+
import { test, describe, before } from "node:test";
23
import assert from "node:assert/strict";
4+
import request from "supertest";
5+
import nock from "nock";
36

47
import express, { Router } from "express";
5-
// note that moxios doesn't work with axios version >1
6-
import moxios from "moxios";
7-
import request from "supertest";
88
import axios from "axios";
99

10-
// @ts-ignore
1110
const hugo = (router = new Router()) => {
1211
router.get("/hugo", async (request_, res) => {
1312
const { data: userData } = await axios.get(
@@ -31,41 +30,38 @@ const initHugo = () => {
3130
};
3231

3332
describe("GET /hugo", () => {
34-
beforeEach(() => {
35-
moxios.install();
36-
});
37-
afterEach(() => {
38-
moxios.uninstall();
33+
before(() => {
34+
nock.disableNetConnect();
35+
nock.enableNetConnect("127.0.0.1");
3936
});
37+
4038
test("It should fetch HugoDF from GitHub", async () => {
41-
moxios.stubRequest(/api.github.com\/users/, {
42-
status: 200,
43-
response: {
44-
blog: "https://codewithhugo.com",
45-
location: "London",
46-
bio: "Developer, JavaScript",
47-
public_repos: 39,
48-
},
39+
const ghMocks = nock("https://api.github.com");
40+
ghMocks.get("/users/HugoDF").reply(200, {
41+
blog: "https://codewithhugo.com",
42+
location: "London",
43+
bio: "Developer, JavaScript",
44+
public_repos: 39,
4945
});
46+
5047
const app = initHugo();
5148
await request(app).get("/hugo");
52-
assert.equal(
53-
moxios.requests.mostRecent().url,
54-
"https://api.github.com/users/HugoDF",
55-
);
49+
assert.equal(ghMocks.isDone(), true);
50+
assert.deepEqual(ghMocks.pendingMocks(), []);
51+
assert.deepEqual(ghMocks.activeMocks(), []);
5652
});
5753
test("It should 200 and return a transformed version of GitHub response", async () => {
58-
moxios.stubRequest(/api.github.com\/users/, {
59-
status: 200,
60-
response: {
61-
blog: "https://codewithhugo.com",
62-
location: "London",
63-
bio: "Developer, JavaScript",
64-
public_repos: 39,
65-
},
54+
const ghMocks = nock("https://api.github.com");
55+
ghMocks.get("/users/HugoDF").reply(200, {
56+
blog: "https://codewithhugo.com",
57+
location: "London",
58+
bio: "Developer, JavaScript",
59+
public_repos: 39,
6660
});
61+
6762
const app = initHugo();
6863
const res = await request(app).get("/hugo");
64+
6965
assert.deepEqual(res.body, {
7066
blog: "https://codewithhugo.com",
7167
location: "London",

0 commit comments

Comments
 (0)