Skip to content

Commit

Permalink
Merge pull request #51 from roypeled/fix-broken-class-parser
Browse files Browse the repository at this point in the history
Fix broken class parser
  • Loading branch information
roypeled authored Jul 18, 2024
2 parents 3efba95 + c748a6a commit db1a8a6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
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.1",
"version": "2.7.2",
"description": "Mocking library for TypeScript",
"main": "lib/ts-mockito.js",
"typings": "lib/ts-mockito",
Expand Down
6 changes: 4 additions & 2 deletions src/utils/ObjectPropertyCodeRetriever.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

export class ObjectPropertyCodeRetriever {
public static getObject(object: any) {
if(object.constructor.name === 'Object') return '';
const props = Object.getOwnPropertyNames(object);
return `class ${object.constructor.name} {
${props.map(prop => {
Expand All @@ -17,10 +18,11 @@ export class ObjectPropertyCodeRetriever {
`;
}
if (!descriptor?.get && !descriptor?.set && typeof object[prop] === 'function') {
const propName = prop === 'constructor' ? 'mock_constructor' : '';
const propName = prop === 'constructor' ? 'mock_constructor' : prop;
const fnStr = String(object[prop]);
const addAssignment = prop === 'constructor' || fnStr.startsWith('function ');
result += `
${propName ? `${propName}=` : ''}${fnStr}
${addAssignment ? `${propName}=` : ''}${fnStr}
`;
}
Expand Down
10 changes: 10 additions & 0 deletions test/mocking.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -226,6 +227,13 @@ describe("mocking", () => {

});
});

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

abstract class SampleAbstractClass {
Expand Down Expand Up @@ -291,3 +299,5 @@ class SampleGeneric<T> {
return null as unknown as T;
}
}

class TestEmitter extends EventEmitter {}

0 comments on commit db1a8a6

Please sign in to comment.