Giter Club home page Giter Club logo

modpod's Introduction

modpod

Build Status codecov

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

modpod's People

Contributors

digitalbush avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.