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

modpod

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

modpod

Isolate your ES Modules for testing.

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

modpod

Build Status Coverage Status

Isolate your ES Modules for testing.

 npm i -D modpod

Easy Mode

import {test} from "node:test";

import modpod from "modpod";

test("easy mode", async () => {
	const instance = await modpod("./target.js", {
		"./dep.js": {
			// Replaces `export default function(){...}` in ./dep.js
			default: () => "mocked default",
			// Replaces `export function otherThing(){...}` in ./dep.js
			otherThing: () => "mocked named export 'otherThing'"
		}
	});

	// Exercise instance
	// Assert what you expected would happen
});

The default export is a function that sets up a mock environment, imports the target, and cleans up.

  • modpod() is a shallow mock. It'll only replace dependencies one level deep.

  • modpod.strict() will make sure that all dependencies of ./target.js are mocked. If an import is missing a mock, an error will be thrown.

  • modpod.deep() will replace ./dep.js anywhere it is imported. Even if it's a dependency of a dependency.

Note: If a mock is declared, but never used, then an error will be thrown.

Hard Mode

If you need more control over the mock lifecycle (like dynamic imports), then you'll need to use the Pod class directly.

Let's consider a few files:

target.js

export async function doSomething() {
	const dep = await import("./dep.js");
	return dep();
}

dep.js

export default () => "dep";

Now we can write a test that handles the dynamic import.

import assert from "node:assert";
import {test, mock} from "node:test";

import {Pod} from "modpod";

test("hard mode", async (t) => {
	const p = new Pod({strict: true});
	t.after(() => p.dispose()); // Clean up

	const dep = mock.fn(() => "mocked");

	// NOTE: dep will be wrapped {default: dep} because it's a function.
	p.mock("./dep.js", dep);

	const instance = await p.import("./target.js");
	assert.strictEqual(dep.mock.calls.length, 0);

	const result = await instance.doSomething();
	assert.strictEqual(dep.mock.calls.length, 1);
});

Keywords

FAQs

Package last updated on 09 Dec 2023

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