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

1.0.0 • Public • Published

mock-cwd

A useful test helper when you’re working with process.cwd().

This helper mocks the value of process.cwd() with a user-specified directory or with a randomly generated folder within the system’s temp folder.

Install

Install using npm or yarn:

npm i mock-cwd —save-dev
yarn i --dev mock-cwd

This package contains TypeScript definitions.

How to Use

With Block

Wraps the mock inside a user-provided block. The block can be an async function, if your operations are asynchronously.

In the following example, it will create a new temporary folder and restore the real cwd after the block has been left.

import { mockCwd } from 'mock-cwd';

mockCwd(() => {
	console.log(process.cwd()); // => /private/var/folders/...
});

console.log(process.cwd()); // => /your/real/cwd

You can also supply a path to an existing directory:

import { mockCwd } from 'mock-cwd';

mockCwd('/your/custom/path', () => {
	console.log(process.cwd()); // => /your/custom/path
});

console.log(process.cwd()); // => /your/real/cwd

Without a Block

Updates process.cwd() to the given or automatically generated path. Returns an object to restore the original path.

import { mockCwd } from 'mock-cwd';

const mock = mockCwd();
console.log(process.cwd()); // => /private/var/folders/...

mock.restore();
console.log(process.cwd()); // => /your/real/cwd

As before, you can also supply a custom path to an existing directory:

import { mockCwd } from 'mock-cwd';

const mock = mockCwd('/your/custom/path');
console.log(process.cwd()); // => /your/custom/path

mock.restore();
console.log(process.cwd()); // => /your/real/cwd

Readme

Keywords

none

Package Sidebar

Install

npm i mock-cwd

Weekly Downloads

82

Version

1.0.0

License

MIT

Unpacked Size

47.9 kB

Total Files

8

Last publish

Collaborators

  • marvinscharle