Лично я нашел HttpClient Apache более чем способным ко всему, что мне нужно было сделать в отношении этого. Здесь - отличный учебник по использованию HttpClient
Прежде всего, ваша функция должна вернуть свое обещание:
const fetch = require("node-fetch");
exports.retrieveFirstUserName = () => {
let title = "";
return fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(response => response.json())
.then(json => {
title = json.title;
console.log(typeof title);
return title;
});
};
Затем, чтобы проверить его, вы должны дождаться обещания, а затем проверить.
describe("fetching function tests using ASSERT interface from CHAI module: ", function () {
describe("Check retrieveFirstUserName Function: ", function () {
it("Check the returned value using: assert.equal(value,'value'): ", function () {
users.retrieveFirstUserName().then(result => {
assert.typeOf(result, "string");
});
})
})
})