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

shallow-instance

v1.1.1

Published

A test utility to instantiate your class, mocking all it's constructor parameters

Downloads

277

Readme

Shallow Test Utility

On systems that rely on dependency injection, test can sometimes be hard to setup, and if you forget to mock a dependency of your class, you may end up instantiating it and making your test not be "Unitary" anymore.

Tht's what this package aims to solde, inspired on angular Shallow Renderer. You can easly, just with a ENUM that represents your class constructor parameters order, instantiate it with the certainty that all of your constructor dependencies will be empty, ready to mock.

How to Use

Supose you'll be testing this scenario:

// index.ts

class ServiceToTest {
    constructor(private depA: DependencyA, private depB: DependencyB) {}

    methodToTest() {
        const aResult = this.depA.methodA();
        const bResult = this.depB.methodB(aResult);
    }
}

class DependencyA {
    methodA() {
        return Math.floor(Math.random() * 10 + 1)
    }

}

class DependencyB {
    methodB(number: number) {
        return number * 10
    }
}

Your test file need to Shallow instantiate your ServiceToTest, and you can do it this way:

// index.spec.ts

import { ServiceToTest } from '.'
import { CreateShallowInstance, TGetDependency } from 'shallow-instance';

// The first step is to create a Enum with your
// class constructor parameters in order, for
// easy access of those values in your test file

enum Deps {
    DependencyA,
    DependencyB,
}

describe('an easier way to instantiate my class', () => {
    // My service instance
    let service: ServiceToTest;

    // The dependency getter instance, to create mocks, spy functions and other things
    let dependencies: TGetDependency<typeof ServiceToTest>
    
    beforeEach(() => {
        // We create a shallow instance and retrieve the instance and the getDependency FUnction
        const { serviceInstance, getDependency } = CreateShallowInstance(ServiceToTest, DEPS);
        
        // We assign them to the upper scope variables, so we can access them in our test blocks
        service = serviceInstance;
        dependencies = getDependency; 

        // We can mock default values here on BeforeEach, 
        dependencies(DEPS.DEP_A).methodA = jest.fn().mockReturnValue(5);
    });
    
    it('should work flawlessly',  () => {
        // Or we can mock values here on our test block.
        // All with perfect type infering!
        dependencies(DEPS.DEP_B).methodB = jest.fn();

        service.methodToTest();

        // And we can access those dependencies on assertions as well
        expect(dependecies(DEPS.DEP_B).methodB).toBeCalledWith(5)
    })
})

Passing Default Mocks

If you want, you can provide your mocks in the moment that CreateShallowInstance is called:

// Create your premake mock object
const premadeMocks: TDefaultMocks<typeof ServiceToTest, DEPS> = {
    [DEPS.DEP_A]: {
        methodA: () => 999
    }
}

// Call passing the object as the last parameter
const { 
    serviceInstance, 
    getDependency 
} = CreateShallowInstance(ServiceToTest, DEPS, premadeMocks);