Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with a mock resolving a mocked value with Promises #192

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Mocker {
private mockableFunctionsFinder = new MockableFunctionsFinder();
private objectPropertyCodeRetriever = new ObjectPropertyCodeRetriever();
private excludedPropertyNames: string[] = ["hasOwnProperty"];
private defaultedPropertyNames: string[] = ["Symbol(Symbol.toPrimitive)", "then", "catch"];

constructor(private clazz: any, public instance: any = {}) {
this.mock.__tsmockitoInstance = this.instance;
Expand All @@ -39,6 +40,9 @@ export class Mocker {
const hasMethodStub = name in target;

if (!hasMethodStub) {
if (this.defaultedPropertyNames.indexOf(name.toString()) >= 0) {
return undefined;
}
return this.createActionListener(name.toString());
}
return target[name];
Expand All @@ -64,6 +68,9 @@ export class Mocker {

const hasMethodStub = name in target;
if (!hasMethodStub) {
if (this.defaultedPropertyNames.indexOf(name.toString()) >= 0) {
return undefined;
}
this.createMethodStub(name.toString());
this.createInstanceActionListener(name.toString(), {});
}
Expand Down
68 changes: 68 additions & 0 deletions test/mocking.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,64 @@ describe("mocking", () => {
// then
expect(foo.sampleString).toBe("42");
});

it("does not create then() descriptor", () => {
// given
mockedFoo = mock(SampleAbstractClass);
const woof: any = instance(mockedFoo);

// when

// then
expect(woof.then == null).toBe(true);
});

it("does create then() descriptor", () => {
// given
const mockedThenable = mock(SampleThenable);
const thenable = instance(mockedThenable);

// when
when(mockedThenable.then()).thenReturn("42");

// then
expect(thenable.then()).toEqual("42");
});

it("does not create catch() descriptor", () => {
// given
mockedFoo = mock(SampleAbstractClass);
const woof: any = instance(mockedFoo);

// when

// then
expect(woof.catch == null).toBe(true);
});

it("does create catch() descriptor", () => {
// given
const mockedThenable = mock(SampleThenable);
const thenable = instance(mockedThenable);

// when
when(mockedThenable.catch()).thenReturn("42");

// then
expect(thenable.catch()).toEqual("42");
});

it("formats as [object Object]", () => {
// given
const mockedThenable = mock(SampleThenable);
const thenable = instance(mockedThenable);

// when

// then
const str = `"${thenable}"`;
expect(str).toEqual('"[object Object]"');
});
});

describe("mocking class with hasOwnProperty", () => {
Expand Down Expand Up @@ -231,3 +289,13 @@ class SampleGeneric<T> {
return null;
}
}

abstract class SampleThenable {
public then(): string {
return "bob";
}

public catch(): string {
return "bob";
}
}