Created
November 19, 2020 01:42
-
-
Save humphd/89794de465e0c9baff1faff688c150a6 to your computer and use it in GitHub Desktop.
Testing code that uses node-fetch using Jest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple module that uses fetch to do a HEAD request | |
const fetch = require('node-fetch'); | |
module.exports.fn = async (url) => { | |
try { | |
const response = await fetch(url, { method: "HEAD" }); | |
return response.ok; | |
} catch(err) { | |
return false; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { fn } = require("./index.js"); | |
const nock = require("nock"); | |
test('testing nock with node-fetch', async () => { | |
const host = "https://www.youtube.com"; | |
const path = "/"; | |
nock(host).head(path).reply(200); | |
const url = `${host}${path}`; | |
const result = await fn(url); | |
expect(result).toBe(true); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment