Package Exports
- revertable-globals
- revertable-globals/package
- revertable-globals/package.json
Readme
revertable-globals
Sets globals in a JavaScript environment that can be easily reverted to restore the original environment; useful for testing code that relies on the presence of certain globals.
Setup
To install with npm, run:
npm install revertable-globals --save-devAPI
Table of contents
function revertableGlobals
Sets globals that can be easily reverted to restore the original environment.
| Parameter | Type | Description |
|---|---|---|
globals |
object | Map of globals to set. |
namespace |
object? = globalThis | Namespace for the globals, defaulting to globalThis. |
Returns: Function — Reverts the globals.
Examples
How to import.
import revertableGlobals from 'revertable-globals';
How to set and revert fetch related globals for a test.
import fetch, { Request, Response } from 'node-fetch'; import revertableGlobals from 'revertable-globals'; const revertGlobals = revertableGlobals({ fetch, Request, Response, }); try { // Test assertions here… } finally { revertGlobals(); }
How to set and revert an environment variable at runtime for a test.
import revertableGlobals from 'revertable-globals'; const revertEnv = revertableGlobals({ FORCE_COLOR: '1' }, process.env); try { // Test assertions here… } finally { revertEnv(); }