inreplace
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

inreplace

npm npm type definitions npm modules type npm bundle size

What?

The inreplace package provides a function to replace an object in-place with another and the capability to restore it later. It does so by mutating the target object to look like the source object.

Why?

The packaged was developed with the intention to be used during testing to swap out globals with mocked versions. For example you might want to swap out node's fs module with a mocked or in memory file system during testing.

How?

Like this:

import fs from 'fs/promises';
import inreplace from 'inreplace';

const replacedFs = inreplace(fs, {
  readFile: async () => Buffer.from('asdf1234')
});

const data = await fs.readFile('./some-file')
  .then(it => it.toString());

console.log(data);

replacedFs.restore();

This will replace the global fs/promises module with the mocked version. Since inreplace works by mutating the object, this will take effect across the entire runtime and not just the file it was called in.

It is worth noting that inreplace replaces the entire object, meaning any functions you do not include in your mocked version will be undefined.

Options

You may pass an options object to the inreplace functions. The currently available options are:

  • allowNonConfigurable - By default the inreplace function will throw when encountering a non-configurable property in the target object. With this option set to true the function will instead leave those properties untouched.

Package Sidebar

Install

npm i inreplace

Weekly Downloads

236

Version

1.2.0

License

MIT

Unpacked Size

10.2 kB

Total Files

13

Last publish

Collaborators

  • white-autumn