ts-stubber
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -10,7 +10,5 @@ // Generated by dts-bundle-generator v7.2.0 | ||
export type StubbedMember<T, StubT> = T extends (...args: infer TArgs) => infer TReturnValue ? StubT : T; | ||
export declare const defaultExcludedMethods: string[]; | ||
/** | ||
* | ||
* @param createStub - method for stub creation, for example: sinon.stub() | ||
* @param excludedMethods - methods to exclude from mocking. default is defaultExcludedMethods | ||
* @returns a stub creator object with a single method: createStubbedInstance | ||
@@ -51,3 +49,3 @@ * | ||
*/ | ||
export declare const StubbedInstanceCreator: <T, StubT>(createStub: (prop: string) => StubT, excludedMethods?: string[]) => { | ||
export declare const StubbedInstanceCreator: <T, StubT>(createStub: (prop: string) => StubT) => { | ||
createStubbedInstance: (overrides?: Partial<T>) => StubbedInstance<T, StubT> & T; | ||
@@ -54,0 +52,0 @@ }; |
@@ -1,19 +0,4 @@ | ||
const defaultExcludedMethods = [ | ||
"__defineGetter__", | ||
"__defineSetter__", | ||
"hasOwnProperty", | ||
"__lookupGetter__", | ||
"__lookupSetter__", | ||
"propertyIsEnumerable", | ||
"toString", | ||
"valueOf", | ||
"__proto__", | ||
"toLocaleString", | ||
"isPrototypeOf", | ||
"then" | ||
]; | ||
/** | ||
* | ||
* @param createStub - method for stub creation, for example: sinon.stub() | ||
* @param excludedMethods - methods to exclude from mocking. default is defaultExcludedMethods | ||
* @returns a stub creator object with a single method: createStubbedInstance | ||
@@ -54,3 +39,3 @@ * | ||
*/ | ||
const StubbedInstanceCreator = (createStub, excludedMethods = defaultExcludedMethods) => { | ||
const StubbedInstanceCreator = (createStub) => { | ||
const createStubbedInstance = (overrides = {}) => { | ||
@@ -60,5 +45,4 @@ let overrideValues = overrides; | ||
const createStubIfNeeded = (target, prop) => { | ||
if (!target[prop] && !excludedMethods.includes(prop)) { | ||
const stub = createStub(prop); | ||
target[prop] = stub; | ||
if (!target[prop]) { | ||
target[prop] = createStub(prop); | ||
} | ||
@@ -81,2 +65,2 @@ }; | ||
export { StubbedInstanceCreator, defaultExcludedMethods }; | ||
export { StubbedInstanceCreator }; |
{ | ||
"name": "ts-stubber", | ||
"description": "Lazy Stubbing a TypeScript Class or Interface with any Mocking Framework for testing in Isolation", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Shelly Goldblit", | ||
@@ -6,0 +6,0 @@ "private": false, |
@@ -0,1 +1,3 @@ | ||
ts-stubber / [Exports](modules.md) | ||
# ts-stubber | ||
@@ -5,3 +7,4 @@ | ||
A generic stubbed instance creator to lazy stub any interface/class, without calling class's constructor. | ||
A generic stubbed instance creator to lazy stub any interface/class, while completely avoiding calling class's constructor. | ||
Thus, enabling both avoiding side effects that may occur while class constructor is activated and mocking Classes with no default constructor. | ||
@@ -12,5 +15,13 @@ ## [Markdown Documentation](https://github.com/ShellyDCMS/ts-stubber/blob/main/documents/modules.md) | ||
## Installation | ||
`npm i -D @ts-stubber` | ||
or | ||
`yarn add -D @ts-stubber` | ||
## Usage | ||
This library provides an API to create a stubbed instance of a class or interface, including property functions, allowing overrides and excluded methods. | ||
This library provides an API to create a stubbed instance of a class or interface, including property functions, allowing overrides of methods, setters and getters.. | ||
@@ -49,12 +60,31 @@ ```ts | ||
`npm i -D @ts-stubber` | ||
## Caveats, Known Issues, and Limitations | ||
or | ||
Some of JS built-in function such as `hasOwnProperty` will act differently when applied on the mocked Class. | ||
`yarn add -D @ts-stubber` | ||
```ts | ||
class MyClass { | ||
property: number = 3; | ||
} | ||
## Developing | ||
it("should not override build in methods", () => { | ||
const mockMyClass = StubbedInstanceCreator<MyClass, jest.Mock>(() => | ||
jest.fn() | ||
).createStubbedInstance(); | ||
const myClass = new MyClass(5); | ||
expect(myClass.hasOwnProperty("property")).toBe(true); | ||
// NOTE! mockMyClass does not have a property called "property" | ||
expect((<MyClass>mockMyClass).hasOwnProperty("property")).toBe(false); | ||
}); | ||
``` | ||
## Testing | ||
1. Set up the repo - `yarn` | ||
2. Build the project - `npm run build` | ||
3. Running tests - `npm run cy:run` | ||
3. Running Jest tests - `npm run test` | ||
4. Running Cypress tests - `npm run cy:run` | ||
## License | ||
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
Sorry, the diff of this file is not supported yet
9301
88
112