1.0.0 • Published 1 year ago

@shellygo/ts-stubber v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

ts-stubber

ts-stubber

A generic stubbed instance creator to lazy stub any interface/class, without calling class's constructor.

Markdown Documentation

HTML Documentation

Usage

This library provides an API to create a stubbed instance of a class or interface, including property functions, allowing overrides and excluded methods.

class MyClass {
  constructor(input: number) {
    throw new Error("Should not be called");
  }
  func(input: number, text: string) {
    console.log(text);
    return input;
  }
  property: number = 3;
  optionalProperty?: number;
  get getter(): number {
    return this.property;
  }
  set setter(value: number) {
    throw new Error("Should not be called");
  }
}

const stubbedInstanceCreator = StubbedInstanceCreator<MyClass, SinonStub>(() =>
  sinon.stub()
);

const mockMyClass = StubbedInstanceCreator<MyClass, SinonStub>(() =>
  sinon.stub()
).createStubbedInstance();

npm i -D @shellygo/ts-stubber

or

yarn add -D @shellygo/ts-stubber

Developing

  1. Set up the repo - yarn
  2. Build the project - npm run build
  3. Running tests - npm run cy:run