1- import { test , describe , beforeEach , afterEach } from "node:test" ;
1+ // @ts -nocheck
2+ import { test , describe , before } from "node:test" ;
23import assert from "node:assert/strict" ;
4+ import request from "supertest" ;
5+ import nock from "nock" ;
36
47import 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" ;
88import axios from "axios" ;
99
10- // @ts -ignore
1110const 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
3332describe ( "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 ( / a p i .g i t h u b .c o m \/ u s e r s / , {
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 ( / a p i .g i t h u b .c o m \/ u s e r s / , {
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