JSPM

revertable-globals

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 23
  • Score
    100M100P100Q79108F
  • License MIT

Sets globals that can be easily reverted to restore the original environment; useful for testing code that relies on the presence of certain globals.

Package Exports

  • revertable-globals
  • revertable-globals/package
  • revertable-globals/package.json

Readme

revertable-globals

npm version CI status

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-dev

API

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();
}