testing/network.mjs
41 lines and 5 functions in DrDustinEdwards/devkit, at commit 505cda4 on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| urlOf | 10 to 12 | 3 | 100% | OkCRAP 3 | No data: mutation testing has not run on this repo yet |
| installFetch | 21 to 28 | 1 | 100% | OkCRAP 1 | No data: mutation testing has not run on this repo yet |
| <anonymous:24> | 24 to 27 | 2 | 75% | OkCRAP 2.1 | No data: mutation testing has not run on this repo yet |
| refuseNetwork | 38 to 44 | 1 | 100% | OkCRAP 1 | No data: mutation testing has not run on this repo yet |
| <anonymous:39> | 39 to 43 | 2 | 100% | OkCRAP 2 | No data: mutation testing has not run on this repo yet |
Imports
It imports no other module of this repo. Imported by testing/access.mjs, testing/github.mjs.
Source
// The network guard every test layer writes for itself: a global fetch that refuses, and the swap underneath it.
// No test framework is imported: the swap is a property definition, the same one vitest's stubGlobal makes, so it works in node and workerd.
/**
* The URL a fetch was asked for, from any of the three shapes fetch accepts.
*
* @param {RequestInfo | URL} input
* @returns {string}
*/
export function urlOf(input) {
return typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
}
/**
* Replaces `globalThis.fetch` with `impl` and returns the function that puts back what was there. Restores run in the
* reverse order of installs, as any stack of stubs does.
*
* @param {typeof fetch} impl
* @returns {() => void}
*/
export function installFetch(impl) {
const previous = Object.getOwnPropertyDescriptor(globalThis, "fetch");
Object.defineProperty(globalThis, "fetch", { value: impl, writable: true, configurable: true, enumerable: true });
return () => {
if (previous) Object.defineProperty(globalThis, "fetch", previous);
else delete (/** @type {{ fetch?: unknown }} */ (globalThis)).fetch;
};
}
/**
* Installs a fetch that throws an error naming the URL, so reaching the network is a failure and never a silent call.
* `hint` is appended to the message: where the layer's recorded stubs live. Returns the restore, so it can be a
* vitest `beforeEach` return value.
*
* @param {{ hint?: string }} [options]
* @returns {() => void}
*/
export function refuseNetwork({ hint } = {}) {
return installFetch(async (input) => {
throw new Error(
`this layer does not reach the network, and something asked for ${urlOf(input)}.` + (hint ? ` ${hint}` : ""),
);
});
}