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

ineeda

v0.11.3

Published

Create stub objects from TypeScript interfaces!

Downloads

2,682

Readme

ineeda

npm version

Auto-mocking with Proxies! Works best with TypeScript, but works just as well with JavaScript!

Installation:

npm install ineeda --save-dev

Mocking:

To get a mock of a concrete class:

import { Hero } from './Hero';

import { ineeda } from 'ineeda';

let hero: Hero = ineeda<Hero>();
console.log(hero.age); // [IneedaProxy] (truthy!)
console.log(hero.weapon.isMagic); // [IneedaProxy] (truthy!)
console.log(hero.weapon.sharpen()); // Error('"sharpen" is not implemented.');

let bonnie: Hero = ineeda<Hero>({ name: 'Bonnie' });
console.log(bonnie.name); // 'Bonnie'

To get a mock of an interface:

import { IHorse } from './IHorse';

import { ineeda } from 'ineeda';

let horse: IHorse = ineeda<IHorse>();
horse.hero.weapon.sharpen(); // Error('"sharpen" is not implemented.');

To get a mock of a concrete class that is an actual instance of that class:

import { Hero } from './Hero';

import { ineeda } from 'ineeda';

let realHero: Hero = ineeda.instanceof<Hero>(Hero);
console.log(realHero instanceof Hero); // true;

To get a factory that produces mocks of a concrete class:

import { Hero } from './Hero';

import { ineeda, IneedaFactory } from 'ineeda';

let heroFactory: IneedaFactory<Hero> = ineeda.factory<Hero>();
let heroMock: Hero = heroFactory();

Intercepting:

Overriding proxied values:

Since the result of a call to ineeda is a proxy, it will happily pretend to be any kind of object you ask it to be! That can cause some issues, such as when dealing with Promises or Observables. To get around that, you can use intercept.

When you need a fake Promise or Observable:

let mockObject = ineeda<MyObject>();

function looksLikePromise (obj) {
    return !!obj.then;
}

looksLikePromise(mockObject); // true;
looksLikePromise(mockObject.intercept({ then: null })); // false;

let mockObject = ineeda<MyObject>();
let myObservable$ = Observable.of(mockObject.intercept({ schedule: null }));

Remembering which properties need to be intercepted can be a pain, and rather error prone. Alternatively, you can assign a key, which you can use to set up specific values that should be intercepted. In your test config you might do something like the following:

// Prevent Bluebird from thinking ineeda mocks are Promises:
ineeda.intercept<Promise<any>>(Promise, { then: null });

// Prevent RxJS from thinking ineeda mocks are Schedulers:
ineeda.intercept<Scheduler>(Observable, { schedule: null });

Then later, in your tests, you could do the following:

let mockObject = ineeda<MyObject>();

function looksLikePromise (obj) {
    return !!obj.then;
}

looksLikePromise(mockObject); // true;
looksLikePromise(mockObject.intercept(Promise)); // false;

let mockObject = ineeda<MyObject>();
let myObservable$ = Observable.of(mockObject.intercept(Observable));

You can also globally intercept something on all objects, by using the intercept method without the key:

ineeda.intercept({
    // Prevent zone.js from thinking ineeda mocks are unconfigurable:
    __zone_symbol__unconfigurables: null
});

Adding behaviour to proxied values:

intercept can also be used to augment the behaviour of all mocks. One example might be to make every mocked function a spy.

// Prevent sinon from thinking ineeda mocks are already spies:
ineeda.intercept({
    restore: null,
    calledBefore: null
});

// Intercept all values that are functions and turn it into a stub:
ineeda.intercept((value, key: string, values, target) => {
    if (value instanceof Function) {
        target[key] = () => { };
        return sinon.stub(target, key, values[key]);
    }
    return value;
});

let mockObject = ineeda<MyObject>();
mockObject.someMethod(1, 2, 3);

// Using sinon-chai:
expect(mockObject.someMethod).to.have.been.calledWith(1, 2, 3);