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 broken class parser #54

Merged
merged 2 commits into from
Jul 24, 2024
Merged
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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;
}
}
Loading