npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@missing-comma/jest-class-stub-maker

v1.0.2

Published

With yarn:

Downloads

9

Readme

jest-stub-maker

Installation

With yarn:

	yarn add -D @missing-comma/jest-class-stub-maker

With npm:

	npm i --save-dev @missing-comma/jest-class-stub-maker

Description

An easy solution to quickly mock an implementation of an interface with it's methods properly mocked as well.

Usage

Let's say we are creating a controller for some HTTP Request. This controller requires a check for authentication. The implementation to this check is passed to the controller as a dependecy.

Let's say our "check if authenticated" implementation should implement the following interface:

export interface IsAuthenticated {
	check(request: any): Promise<boolean>;
}

and your controller implementation looks something like this:

export class RequestController {
	constructor(private readonly isAuthenticated: IsAuthenticated) {}

	async handle(request: any): Promise<any> {
		const authenticated = await this.isAuthenticated.check(request);

		if (!authenticated) {
			throw new Error("unauthenticated");
		}

		// do something
	}
}

To make a proper unit-test for this class, we should mock "isAuthenticated" inside the controller. And this package provides the tools for mocking such interface's implementation easily

Here's how your test to check if "isAuthenticated" is being properly used would look like:

import { makeClassLikeStub } from "@missing-comma/jest-class-stub-maker";
import { IsAuthenticated, RequestController } from "...";

describe("RequestController Test", () => {
	test("Should throw error if user has no permission", async () => {
		const isAuthenticatedStub = makeClassLikeStub<IsAuthenticated>("check");
		const controller = new RequestController(isAuthenticatedStub);

		const request = "valid_request";
		isAuthenticatedStub.check.mockResolvedValueOnce(false);

		await expect(controller.handle(request)).rejects.toThrow("unauthenticated");
	});

	test("Should not throw error if user has permission", async () => {
		const isAuthenticatedStub = makeClassLikeStub<IsAuthenticated>("check");
		const controller = new RequestController(isAuthenticatedStub);

		const request = "valid_request";
		isAuthenticatedStub.check.mockResolvedValueOnce(true);

		await expect(controller.handle(request)).resolves.not.toThrow();
	});
});

Disclaimer

The name of the package and it's methods are all open for discussion. My bad if they are not great :c