0.2.0 • Published 5 months ago

modpod v0.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

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);
});
0.2.0

5 months ago

0.1.0

5 months ago