-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ShellyDCMS/initial-commit
Initial commit
- Loading branch information
Showing
8 changed files
with
475 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.