All Projects → phenomnomnominal → ineeda

phenomnomnominal / ineeda

Licence: MIT license
Mocking library for TypeScript and JavaScript using Proxies!

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ineeda

springmock
alternative spring mocking infrastructure
Stars: ✭ 22 (-58.49%)
Mutual labels:  mock, mocking
MockAlamofire
A simple example showing how to override the URLProtocol to return mock data on Alamofire responses. Helpful if you are looking for a simple way to mock an Alamofire response, with out any additional dependencies.
Stars: ✭ 22 (-58.49%)
Mutual labels:  mock, mocking
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (+32.08%)
Mutual labels:  mock, mocking
go-github-mock
A library to aid unittesting code that uses Golang's Github SDK
Stars: ✭ 63 (+18.87%)
Mutual labels:  mock, mocking
Mockaco
🐵 HTTP mock server, useful to stub services and simulate dynamic API responses, leveraging ASP.NET Core features, built-in fake data generation and pure C# scripting
Stars: ✭ 213 (+301.89%)
Mutual labels:  mock, mocking
DataAbstractions.Dapper
A light abstraction around Dapper and Dapper.Contrib that also maintains the behavior IDbConnection.
Stars: ✭ 37 (-30.19%)
Mutual labels:  mock, unit
chrome-extension-mocker
The most convenient tool to mock requests for axios, with built-in Chrome extension support.
Stars: ✭ 37 (-30.19%)
Mutual labels:  mock, mocking
Okhttp Json Mock
Mock your datas for Okhttp and Retrofit in json format in just a few moves
Stars: ✭ 231 (+335.85%)
Mutual labels:  mock, mocking
open-api-mocker
A mock server based in OpenAPI Specification
Stars: ✭ 58 (+9.43%)
Mutual labels:  mock, mocking
FluentSimulator
A fluent syntax .NET REST/HTTP API simulator for automated unit and UI testing.
Stars: ✭ 23 (-56.6%)
Mutual labels:  mocking, unit
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+377.36%)
Mutual labels:  mock, mocking
umock-c
A pure C mocking library
Stars: ✭ 29 (-45.28%)
Mutual labels:  mock, mocking
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+6405.66%)
Mutual labels:  mock, mocking
aem-stubs
Tool for providing sample data for AEM applications in a simple and flexible way. Stubbing server on AEM, no separate needed.
Stars: ✭ 40 (-24.53%)
Mutual labels:  mock, mocking
Faker
Provides fake data to your Android apps :)
Stars: ✭ 234 (+341.51%)
Mutual labels:  mock, mocking
dexopener
An Android library that provides the ability to mock your final classes on Android devices.
Stars: ✭ 112 (+111.32%)
Mutual labels:  mock, mocking
Mockiato
A strict, yet friendly mocking library for Rust 2018
Stars: ✭ 229 (+332.08%)
Mutual labels:  mock, mocking
Axios Mock Adapter
Axios adapter that allows to easily mock requests
Stars: ✭ 2,832 (+5243.4%)
Mutual labels:  mock, mocking
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (+86.79%)
Mutual labels:  mock, mocking
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (-43.4%)
Mutual labels:  mock, mocking

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);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].