Skip to content

Commit

Permalink
Merge pull request #1 from ShellyDCMS/initial-commit
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
ShellyDCMS authored Mar 15, 2024
2 parents 56f0667 + 35c06db commit b690493
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 124 deletions.
27 changes: 27 additions & 0 deletions documents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@shellygo/ts-stubber / [Exports](modules.md)

# ts-stubber

![ts-stubber](https://github.com/ShellyDCMS/ts-stubber/actions/workflows/npm-publish.yml/badge.svg)

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

## [Markdown Documentation](https://github.com/ShellyDCMS/ts-stubber/blob/main/documents/modules.md)

## [HTML Documentation](https://shellydcms.github.io/ts-stubber/modules.html)

## Usage

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

`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`
119 changes: 119 additions & 0 deletions documents/modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
[@shellygo/ts-stubber - v1.0.0](README.md) / Exports

# @shellygo/ts-stubber - v1.0.0

## Table of contents

### Type Aliases

- [StubbedInstance](modules.md#stubbedinstance)
- [StubbedMember](modules.md#stubbedmember)

### Variables

- [defaultExcludedMethods](modules.md#defaultexcludedmethods)

### Functions

- [StubbedInstanceCreator](modules.md#stubbedinstancecreator)

## Type Aliases

### StubbedInstance

Ƭ **StubbedInstance**\<`T`, `StubT`\>: \{ [P in keyof T]: StubbedMember\<T[P], StubT\> }

#### Type parameters

| Name |
| :------ |
| `T` |
| `StubT` |

___

### StubbedMember

Ƭ **StubbedMember**\<`T`, `StubT`\>: `T` extends (...`args`: infer TArgs) => infer TReturnValue ? `StubT` : `T`

Replaces a type with a stub if it's a function.

#### Type parameters

| Name |
| :------ |
| `T` |
| `StubT` |

## Variables

### defaultExcludedMethods

`Const` **defaultExcludedMethods**: `string`[]

## Functions

### StubbedInstanceCreator

**StubbedInstanceCreator**\<`T`, `StubT`\>(`createStub`, `excludedMethods?`): `Object`

#### Type parameters

| Name |
| :------ |
| `T` |
| `StubT` |

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `createStub` | (`prop`: `string`) => `StubT` | `undefined` | method for stub creation, for example: sinon.stub() |
| `excludedMethods` | `string`[] | `defaultExcludedMethods` | methods to exclude from mocking. default is defaultExcludedMethods |

#### Returns

`Object`

a stub creator object with a single method: createStubbedInstance

| Name | Type |
| :------ | :------ |
| `createStubbedInstance` | (`overrides?`: `Partial`\<`T`\>) => [`StubbedInstance`](modules.md#stubbedinstance)\<`T`, `StubT`\> & `T` |

**createStubbedInstance**: (`overrides?`: `Partial`\<`T`\>) => [`StubbedInstance`](modules.md#stubbedinstance)\<`T`, `StubT`\> & `T`

\-

-----

**`Example`**

```ts
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()
```
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@
"dependencies": {},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"@types/chai-subset": "^1.3.3",
"@types/chance": "^1.1.6",
"@types/node": "^20.2.5",
"@types/sinon": "^17.0.3",
"chai-subset": "^1.6.0",
"chance": "^1.1.11",
"cypress": "^13.6.6",
"gh-pages": "^5.0.0",
"mochawesome": "^7.1.3",
"rollup": "^4.9.6",
"rollup-plugin-dts-bundle-generator": "^1.4.0",
"sinon": "^17.0.1",
"typedoc": "^0.24.8",
"typedoc-plugin-markdown": "^3.15.3",
"typedoc-plugin-merge-modules": "^5.0.1",
"typescript": "^4.6.4",
"yarn": "^1.22.19",
"@types/chai-subset": "^1.3.3",
"@types/sinon": "^17.0.3",
"chai-subset": "^1.6.0",
"cypress": "^13.6.6",
"sinon": "^17.0.1"
"yarn": "^1.22.19"
},
"compilerOptions": {
"target": "es5",
Expand Down
Loading

0 comments on commit b690493

Please sign in to comment.