diff --git a/src/Mock.ts b/src/Mock.ts index a1c95cc..537fa9a 100644 --- a/src/Mock.ts +++ b/src/Mock.ts @@ -29,7 +29,7 @@ export class Mocker { this.processFunctionsCode(this.clazz.prototype); } if (typeof Proxy !== "undefined") { - this.mock.__tsmockitoInstance = new Proxy(this.instance, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters()); + this.mock.__tsmockitoInstance = new Proxy(this.instance, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters("instance")); } } @@ -37,17 +37,20 @@ export class Mocker { if (typeof Proxy === "undefined") { return this.mock; } - return new Proxy(this.mock, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters()); + return new Proxy(this.mock, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters("expectation")); } - public createCatchAllHandlerForRemainingPropertiesWithoutGetters(): any { + public createCatchAllHandlerForRemainingPropertiesWithoutGetters(origin: "instance" | "expectation"): any { return { get: (target: any, name: PropertyKey) => { const hasMethodStub = name in target; if (!hasMethodStub) { if (this.mock.__tsmockitoInterface) { - this.createMethodStub(name.toString()); - this.createInstanceActionListener(name.toString(), {}); + if (origin !== "instance" || name !== "then") { + // Don't make this mock object instance look like a Promise instance by mistake, if someone is checking + this.createMethodStub(name.toString()); + this.createInstanceActionListener(name.toString(), {}); + } } else { this.createPropertyStub(name.toString()); this.createInstancePropertyDescriptorListener(name.toString(), {}, this.clazz.prototype); diff --git a/test/stubbing.method.spec.ts b/test/stubbing.method.spec.ts index d03f745..d330d6c 100644 --- a/test/stubbing.method.spec.ts +++ b/test/stubbing.method.spec.ts @@ -1,4 +1,4 @@ -import {anything, instance, mock, when} from "../src/ts-mockito"; +import {anything, imock, instance, mock, when} from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; describe("mocking", () => { @@ -213,6 +213,25 @@ describe("mocking", () => { }) .catch(err => done.fail(err)); }); + + if (typeof Proxy !== "undefined") { + it("resolves with given mock value", done => { + // given + const sampleValue = "abc"; + const expectedResult: Foo = imock(); + + when(mockedFoo.sampleMethodReturningObjectPromise(sampleValue)).thenResolve(instance(expectedResult)); + + // when + foo.sampleMethodReturningObjectPromise(sampleValue) + .then(value => { + // then + expect(value).toEqual(instance(expectedResult)); + done(); + }) + .catch(err => done.fail(err)); + }); + } }); describe("with stubbed promise rejection", () => { diff --git a/test/utils/Foo.ts b/test/utils/Foo.ts index 732e79d..fc5eb3e 100644 --- a/test/utils/Foo.ts +++ b/test/utils/Foo.ts @@ -42,4 +42,8 @@ export class Foo { public sampleMethodReturningVoidPromise(value: string): Promise { return Promise.resolve(); } + + public sampleMethodReturningObjectPromise(value: string): Promise { + return Promise.resolve(this); + } }