Add async logic with unit test example
This commit is contained in:
Родитель
4b3c8eb58a
Коммит
2cc84f5108
|
@ -0,0 +1,8 @@
|
|||
const { fetchQuote, fetchZen } = require("./lib/utils");
|
||||
const https = require("https");
|
||||
|
||||
(async () => {
|
||||
console.log("Fetching remote message... please wait.");
|
||||
const msg = await fetchZen();
|
||||
console.log(msg);
|
||||
})().catch(err => console.log("Oh noes!", err));
|
18
lib/utils.js
18
lib/utils.js
|
@ -1,3 +1,5 @@
|
|||
const fetch = require("node-fetch");
|
||||
|
||||
const utils = {
|
||||
greetings: to => {
|
||||
if (!to) {
|
||||
|
@ -7,7 +9,21 @@ const utils = {
|
|||
return `Hello ${to} 👋👋`;
|
||||
},
|
||||
|
||||
makeHeading: text => (text ? `<h1>${String(text).trim()}</h1>` : "")
|
||||
makeHeading: text => (text ? `<h1>${String(text).trim()}</h1>` : ""),
|
||||
|
||||
fetchQuote: async function() {
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
setTimeout(() => resolve("Hey there bud"), 500);
|
||||
});
|
||||
let result = await promise;
|
||||
return result;
|
||||
},
|
||||
|
||||
fetchZen: async function() {
|
||||
let response = await fetch("https://api.github.com/octocat");
|
||||
let quote = await response.text();
|
||||
return quote;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = utils;
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -14,8 +14,8 @@
|
|||
"dependencies": {
|
||||
"codecov": "^3.1.0",
|
||||
"mocha": "^5.2.0",
|
||||
"nyc": "^13.1.0",
|
||||
"request": "2.46.0"
|
||||
"node-fetch": "^2.5.0",
|
||||
"nyc": "^13.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "5.5.0",
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
var assert = require("assert");
|
||||
var utils = require("../lib/utils");
|
||||
|
||||
var mochaAsync = fn => {
|
||||
return done => {
|
||||
fn.call().then(done, err => {
|
||||
done(err);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
describe("Utils", function() {
|
||||
describe("greetings", function() {
|
||||
it("Says Hello World", function() {
|
||||
|
@ -40,4 +48,14 @@ describe("Utils", function() {
|
|||
// assert.equal(utils.makeHeading(), "");
|
||||
// });
|
||||
});
|
||||
|
||||
describe("fetchQuote", function() {
|
||||
it(
|
||||
"returns a nice msg",
|
||||
mochaAsync(async () => {
|
||||
const msg = await utils.fetchQuote();
|
||||
assert.equal(msg, "Hey there bud");
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче