Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-mock-process

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-process

Easily mock NodeJS process properties in Jest

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
37K
increased by27.98%
Maintainers
1
Weekly downloads
 
Created
Source

jest-mock-process npm version CircleCI Coverage Status

Easily mock NodeJS process properties in Jest.

Installation

npm install --save-dev jest-mock-process

Usage

TypeScript example.

import {
  mockProcessExit,
  mockProcessStdout,
  mockProcessStderr,
  mockProcessUptime,
  mockConsoleLog,
} from "jest-mock-process";

let mockExit = mockProcessExit();
process.exit(1);
expect(mockExit).toHaveBeenCalledWith(1);
mockExit = mockProcessExit(new Error("Mock"));
expect(() => process.exit(0)).toThrowError("Mock");

const mockStdout = mockProcessStdout();
process.stdout.write("Hello, world!");
expect(mockStdout).toHaveBeenCalledWith("Hello, world!");

const mockStderr = mockProcessStderr();
process.stderr.write("Error");
expect(mockStderr).toHaveBeenCalledWith("Error");

const mockUptime = mockProcessUptime(3.14159);
const uptimeValue = process.uptime();
expect(uptimeValue).toEqual(3.14159);

const mockLog = mockConsoleLog();
console.log("Browser log");
expect(mockLog).toHaveBeenCalledWith("Browser log");

mockExit.mockRestore();
mockStdout.mockRestore();
mockStderr.mockRestore();
mockLog.mockRestore();

Advanced usage

  • You can use mockedRun (or asyncMockedRun) to set-up a virtual environment that will automatically create and restore provided mocks:
import { mockedRun, MockedRunResult } from "jest-mock-process";

const mockRun = mockedRun({
  stdout: mockProcessStdout,
  stderr: mockProcessStderr,
  exit: mockProcessExit,
  log: mockConsoleLog,
});
const mockEnvironment = mockRun(() => {
  process.stdout.write("stdout payload");
  process.stderr.write("stderr payload");
  process.exit(-1);
  console.log("log payload");
  return 10;
});
expect(mockEnvironment.result).toEqual(10);
expect(mockEnvironment.error).toBeUndefined();
expect(mockEnvironment.mocks.stdout).toHaveBeenCalledTimes(1);
expect(mockEnvironment.mocks.log).toHaveBeenCalledWith("log payload");

NOTE: The above is a breaking change in version 2.0.0, as the provided mocks are now limited to the mocks object.

  • You can mock generic methods not supported by default in jest-mock-process with the spyOnImplementing function:
import { spyOnImplementing } from "jest-mock-process";

const mockStdin = spyOnImplementing(process.stdin, "read", () => "");
process.stdin.read(1024);
expect(mockStdin).toHaveBeenCalledWith(1024);

Keywords

FAQs

Package last updated on 30 Jun 2022

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