1.0.1 • Published 5 months ago

mockli v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Mockli

npm version License: MIT TypeScript

Simple, chainable mock creation test kit for TypeScript with zero dependencies.

Features

  • 🏗️ Builder pattern API
  • 🔗 Method chaining
  • 🧩 Composability with multiple test kits
  • 🛡️ Full TypeScript support
  • 📦 Zero external dependencies

Installation

npm install mockli --save-dev

Requires TypeScript 4.1+ (comes with type definitions)

Usage

Basic Example

// user.mockli.ts
import Mockli from 'mockli';

interface User {
  id: number;
  name: string;
  email?: string;
}

class UserMock extends Mockli {
  withUser(id: number, user?: Partial<User>) {
    this.data.users = { 
      ...this.data.users, 
      [id]: { id, name: `User ${id}`, ...user }
    };
    return this;
  }
}

// In your test
const mock = new UserMock()
  .withUser(1, { name: 'Alice' })
  .withUser(2)
  .build();

console.log(mock.users);
/* Output:
{
  '1': { id: 1, name: 'Alice' },
  '2': { id: 2, name: 'User 2' }
}
*/

Composable Mocks

// product.mockli.ts
import Mockli from 'mockli';

interface Product {
  id: number;
  name: string;
  price: number;
}

class ProductMock extends Mockli {
  withProduct(id: number, product?: Partial<Product>) {
    this.data.products = {
      ...this.data.products,
      [id]: { id, name: `Product ${id}`, price: 9.99, ...product }
    };
    return this;
  }
}

// Combined usage
const mockli = Mockli.withMocklis([new UserMock(), new ProductMock()]);

const mock = mockli
  .withUser(123)
  .withProduct(456, { userId: 123, price: 14.99 })
  .build();

API

Mockli

Core class providing builder pattern functionality.

Methods:

MethodDescription
.build()Returns final mock data object
Static .withMocklis(mocklis)Combines multiple mocks

Custom Methods:

  • Define chainable methods in your mocks that return this

Type Safety

interface MockData {
  users: Record<number, User>;
  products: Record<number, Product>;
}

class TypedMocklis extends Mockli<MockData> {
  withUser(id: number) {
    this.data.users[id] = { id, name: `User ${id}` };
    return this;
  }
  
  withProduct(id: number) {
    this.data.products[id] = { id, name: `Product ${id}`, price: 9.99 };
    return this;
  }
}

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Submit a PR with tests and documentation

License

MIT © Adir Ben Yosef

1.0.1

5 months ago

1.0.0

5 months ago