Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
jest-mock-process
Advanced tools
Easily mock NodeJS process properties in Jest.
npm install --save-dev jest-mock-process
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();
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.
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);
FAQs
Easily mock NodeJS process properties in Jest
The npm package jest-mock-process receives a total of 25,063 weekly downloads. As such, jest-mock-process popularity was classified as popular.
We found that jest-mock-process demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.