@atcodes/mockable

1.1.3 • Public • Published

Mockable

Based on: https://nalanj.dev/posts/mocking-without-loaders/

tl;dr: it's still too hard/annoying to mock ESM modules. Export mockable-wrapped functions, and your tests can override them as needed, with near-zero overhead in production.

🚨 🚨 NOTE: Only works on functions. 🚨 🚨

Install

npm install @atcodes/mockable

Usage

1A: Export mockable resources

When writing your own libraries and utilities, export them as mockable resources:

import { mockable } from '@atcodes/mockable';

export const getDBConnection = mockable(getDBConnection);

async function getDBConnection(){ /* ... */ };

1B: Proxy 3rd party libraries as mockable

Need to mock a 3rd party library that you don't control? Proxy it as a mockable resource:

import { somethingUseful } from 'something-useful';
import { mockable } from '@atcodes/mockable';

export const getSomethingUseful = mockable(somethingUseful);

2: Override in tests

import { test } from "node:test";
import { assert } from "node:assert/strict";

//the function that we want to mock
import { getDBConnection } from "./get-db.js";

//the module that we're going to execute and which uses the mocked function
import { handleRequest } from "./handle-request.js";

test("success", async (t) => {
	// override findUser for this test
	getDBConnection.override(() => {
		return {
			query: (id) => {
				return {
					id,
					name: "Test Tester",
					email: "test@test.com",
					createdAt: new Date(),
					updatedAt: new Date(),
				};
			}
		};
	});

	// clear the override after the test runs
	t.after(() => getDBConnection.clear());

	const resp = await handleRequest("/users/12");
	assert.equal(resp.statusCode, 200);
});

Readme

Keywords

Package Sidebar

Install

npm i @atcodes/mockable

Weekly Downloads

88

Version

1.1.3

License

MIT

Unpacked Size

4.85 kB

Total Files

6

Last publish

Collaborators

  • atuttle