Skip to content

Instantly share code, notes, and snippets.

@humphd
Created Nov 19, 2020
Embed
What would you like to do?
Testing code that uses node-fetch using Jest
// 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;
}
};
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