Socket
Socket
Sign inDemoInstall

mock-cwd

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-cwd

Mocks the current working directory to a given path. Helpful to be used inside tests.


Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 04 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc