Skip to content

Instantly share code, notes, and snippets.

@humphd
Created November 19, 2020 01:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humphd/89794de465e0c9baff1faff688c150a6 to your computer and use it in GitHub Desktop.
Save humphd/89794de465e0c9baff1faff688c150a6 to your computer and use it in GitHub Desktop.
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