Skip to content

Commit

Permalink
fixed async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rezoled committed Jul 24, 2024
1 parent f2abe9d commit ed1c6b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/utils/ObjectPropertyCodeRetriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ObjectPropertyCodeRetriever {
];
} else if (typeof object[prop] === 'function') {
const fnStr = String(object[prop]);
const isMethod = fnStr.startsWith(prop);
const isMethod = fnStr.startsWith(prop) || fnStr.startsWith(`async ${prop}`);
return `
${isMethod ? fnStr : `${prop}: ${fnStr}`}
`;
Expand Down
38 changes: 29 additions & 9 deletions test/mocking.types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MethodToStub } from "../src/MethodToStub";
import { instance, mock, when } from "../src/ts-mockito";
import { Bar } from "./utils/Bar";
import { ThenableClass } from "./utils/Thenable";
import { EventEmitter } from "events";
import {MethodToStub} from "../src/MethodToStub";
import {instance, mock, when} from "../src/ts-mockito";
import {Bar} from "./utils/Bar";
import {ThenableClass} from "./utils/Thenable";
import {EventEmitter} from "events";

describe("mocking", () => {
describe("mocking abstract class", () => {
Expand Down Expand Up @@ -228,19 +228,26 @@ describe("mocking", () => {
});
});

describe("mocking native class", () =>{
describe("mocking native class", () => {
it("should mock", () => {
const mocked = mock(TestEmitter);
expect(mocked).toBeDefined();
});
});

describe("mocking anon class", () =>{
describe("mocking anon class", () => {
it("should mock", () => {
const mocked = mock(TestAnonClass);
expect(mocked).toBeDefined();
});
});

describe("mocking async class", () => {
it("should mock", () => {
const mocked = mock(AsyncClass);
expect(mocked).toBeDefined();
});
});
});

abstract class SampleAbstractClass {
Expand Down Expand Up @@ -307,8 +314,21 @@ class SampleGeneric<T> {
}
}

class TestEmitter extends EventEmitter {}
class TestEmitter extends EventEmitter {
}

const TestAnonClass = class {
private readonly foo = 'abc';
};
};

export class AsyncClass {
public asyncValueArrowFn = async () => 'value';

public asyncValueFn = async function hello() {
return 'value';
};

public async returnAsyncValue(): Promise<number> {
return 0;
}
}

0 comments on commit ed1c6b0

Please sign in to comment.