diff --git a/package-lock.json b/package-lock.json index 03536b4..5af0aa5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@typestrong/ts-mockito", - "version": "2.7.5", + "version": "2.7.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@typestrong/ts-mockito", - "version": "2.7.5", + "version": "2.7.6", "license": "MIT", "dependencies": { "@babel/parser": "^7.24.7", diff --git a/package.json b/package.json index 602daf0..a4f3783 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@typestrong/ts-mockito", - "version": "2.7.5", + "version": "2.7.6", "description": "Mocking library for TypeScript", "main": "lib/ts-mockito.js", "typings": "lib/ts-mockito", diff --git a/src/utils/ObjectPropertyCodeRetriever.ts b/src/utils/ObjectPropertyCodeRetriever.ts index f184b0e..ad35c10 100644 --- a/src/utils/ObjectPropertyCodeRetriever.ts +++ b/src/utils/ObjectPropertyCodeRetriever.ts @@ -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}`} `; diff --git a/test/mocking.types.spec.ts b/test/mocking.types.spec.ts index 8e6ec2d..7e224bb 100644 --- a/test/mocking.types.spec.ts +++ b/test/mocking.types.spec.ts @@ -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", () => { @@ -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 { @@ -307,8 +314,21 @@ class SampleGeneric { } } -class TestEmitter extends EventEmitter {} +class TestEmitter extends EventEmitter { +} const TestAnonClass = class { private readonly foo = 'abc'; -}; \ No newline at end of file +}; + +export class AsyncClass { + public asyncValueArrowFn = async () => 'value'; + + public asyncValueFn = async function hello() { + return 'value'; + }; + + public async returnAsyncValue(): Promise { + return 0; + } +}