ineeda
Create stub objects from TypeScript interfaces!
Last updated 2 years ago by phenomnomnominal .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ineeda 
SYNC missed versions from official npm registry.

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);

Current Tags

  • 0.11.3                                ...           latest (2 years ago)

46 Versions

  • 0.11.3                                ...           2 years ago
  • 0.12.0-alpha.0                                ...           2 years ago
  • 0.11.2                                ...           2 years ago
  • 0.11.1                                ...           2 years ago
  • 0.11.0                                ...           3 years ago
  • 0.11.0-beta.2                                ...           3 years ago
  • 0.11.0-beta.1                                ...           3 years ago
  • 0.11.0-beta.0                                ...           3 years ago
  • 0.10.0-beta.2                                ...           3 years ago
  • 0.10.0-beta.1                                ...           3 years ago
  • 0.10.0-beta.0                                ...           3 years ago
  • 0.9.0-beta.1                                ...           3 years ago
  • 0.9.0-beta.0                                ...           3 years ago
  • 0.8.0-beta.1                                ...           3 years ago
  • 0.8.0-beta.0                                ...           3 years ago
  • 0.7.0-beta.0                                ...           3 years ago
  • 0.6.0                                ...           3 years ago
  • 0.6.0-alpha.1                                ...           3 years ago
  • 0.6.0-alpha.0                                ...           3 years ago
  • 0.5.2-beta.0                                ...           3 years ago
  • 0.5.1-beta.0                                ...           3 years ago
  • 0.5.0-beta.0                                ...           3 years ago
  • 0.4.1-beta.1                                ...           3 years ago
  • 0.4.1-beta.0                                ...           3 years ago
  • 0.4.0-beta.10                                ...           3 years ago
  • 0.4.0-beta.9                                ...           3 years ago
  • 0.4.0-beta.8                                ...           3 years ago
  • 0.4.0-beta.7                                ...           3 years ago
  • 0.4.0-beta.6                                ...           3 years ago
  • 0.4.0-beta.5                                ...           3 years ago
  • 0.4.0-beta.4                                ...           3 years ago
  • 0.4.0-beta.3                                ...           3 years ago
  • 0.4.0-beta.2                                ...           3 years ago
  • 0.4.0-beta.1                                ...           3 years ago
  • 0.4.0-beta.0                                ...           3 years ago
  • 0.3.3-beta.0                                ...           3 years ago
  • 0.3.2-beta.0                                ...           3 years ago
  • 0.3.1-beta.0                                ...           3 years ago
  • 0.3.0-beta.3                                ...           3 years ago
  • 0.3.0-beta.2                                ...           3 years ago
  • 0.3.0-beta.1                                ...           3 years ago
  • 0.3.0-beta.0                                ...           3 years ago
  • 0.2.2-beta.3                                ...           3 years ago
  • 0.2.2-beta.2                                ...           3 years ago
  • 0.2.2-beta.1                                ...           3 years ago
  • 0.2.2-beta.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 47
Last Day 0
Last Week 46
Last Month 5
Dependencies (0)
None
Dev Dependencies (27)

Copyright 2014 - 2016 © taobao.org |