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

kissmock

v0.1.2

Published

A simple and quick mocking library for JavaScript written in TypeScript.

Downloads

3

Readme

kissmock

By Joe Fallon

A simple and quick mocking library for JavaScript written in TypeScript.

Installation

The easiest way to install kissmock is with npm.

npm install kissmock --save

Mock

Mock is an extremely simple to understand and extremely fast stubbing and mocking solution. All types of mocking and stubbing needs can be satisfied via its use.

Features

Mock has the following features and benefits:

  • Full suite of unit tests.
  • It can be integrated into any existing project.
  • Can be fully understood in just a few moments.
  • Written in TypeScript.
  • Extremely fast.

Class Functions

functionCalled(functionName:string, args = []):any;
setFunctionReturnValue(functionName:string, returnValue:any, callCount?:number):any;
getFunctionCallCount(functionName:string):number;
getFunctionArgs(functionName:string, callCount = 1):any[];

Overview

The use of Mock requires the cooperation of three different classes:

  1. The class under test.
  2. The unit test class that contains the methods for testing the class under test.
  3. The mock/stub (the same class satisfies both needs) class for the class under test.

Usage

This is a class that has a dependency that will be mocked out.

class ProductionClass {
    public exampleFunction(dependency:DependencyClass) {
        let param1 = 5;
        let param2 = 'abc';
        let result = dependency.timeConsumingCalculation(param1, param2);

        return result;
    }
}

This is the class that will be mocked out:

class DependencyClass {
    public timeConsumingCalculation(param1:number, param2:string):number {
        // perform very time consuming calculations...
        return 42;
    }
}

This is the DependencyClass fully mocked/stubbed out:

var Mock = require('kissmock');

class DependencyClassMock extends DependencyClass {
    public _mock:Mock;

    // Replace the default constructor.
    public construct() {
        this._mock = new Mock();
    }

    public timeConsumingCalculation(param1:number, param2:string):number {
        // The contents every public method is replaced with the following.
        let args = [param1, param2];
        return this._mock.functionCalled('timeConsumingCalculation', args);
    }
}

Finally, here is the unit test that ties it all together:

describe('ExampleClass', () => {
    describe('#exampleFunction', () => {
        it('has correct object interactions', (done) => {
            let dependencyMock = new DependencyClassMock();
            dependencyMock._mock.setFunctionReturnValue('timeConsumingCalculation', 42);
            
            // Create the class under test.
            let productionClass = new ProductionClass();
            let result = productionClass.exampleFunction(dependencyMock);
    
            // Assert the return value matches what is expected.
            assert.equal(42, result);
    
            // Assert the method was called exactly once.
            let callCount = dependencyMock._mock.getFunctionCallCount('timeConsumingCalculation');
            assert.equal(1, callCount);
    
            // Assert the proper arguments were passed to the method.
            let args = dependencyMock._mock.getFunctionArgs('timeConsumingCalculation', 1);
            assert.equal(5, args[0]);
            assert.equal('abc', args[1]);
            
            done();
        });
    });
});

Performance

The mocks can be used as stubs by simply not asserting on any of the functions in the instance of Mock held by the mock/stub class. Since no reflection, eval, or injection is used, the mock/stub classes are extremely fast.