Skip to content

Commit

Permalink
Merge pull request #251 from AvantaR/master
Browse files Browse the repository at this point in the history
Fix for class inheritence issue #137
  • Loading branch information
ttarnowski authored Sep 16, 2021
2 parents addffe7 + 98776c2 commit 052ab08
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-sinon",
"version": "2.0.1",
"version": "2.0.2",
"description": "sinon library extension to stub whole object and interfaces",
"author": {
"name": "Tomasz Tarnowski",
Expand Down Expand Up @@ -53,4 +53,4 @@
"ts-node": "^9.0.0",
"typescript": "^4.0.2"
}
}
}
36 changes: 36 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,41 @@ describe('ts-sinon', () => {
expect(stub.method2(222)).to.equal(expectedNewMethod2Value);
expect(stub.method2).to.have.been.calledWith(222);
});

it('stubs all object methods with inheritance', () => {
class B {
methodB(): string {
return 'B';
}
}
class A extends B{
method1(): string {
return 'value1';
}
method2(x: number): number {
return 13;
}
}
const expectedNewMethod1Value = 'new value';
const expectedNewMethod2Value = 43;
const expectedMethodBValue = 'new B value';
const expectedMethod2Argument = 111;

const stub = stubConstructor(A);

expect(stub.method1()).to.be.undefined;
expect(stub.method2(expectedMethod2Argument)).to.be.undefined;
expect(stub.methodB()).to.be.undefined;

stub.method1.returns(expectedNewMethod1Value);
stub.method2.returns(expectedNewMethod2Value);
stub.methodB.returns(expectedMethodBValue);
expect(stub.method2).to.have.been.calledWith(expectedMethod2Argument);

expect(stub.method1()).to.equal(expectedNewMethod1Value);
expect(stub.method2(222)).to.equal(expectedNewMethod2Value);
expect(stub.methodB()).to.equal(expectedMethodBValue);
expect(stub.method2).to.have.been.calledWith(222);
});
});
});
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type ObjectMethodsMap<T> = {

export function stubObject<T extends object>(object: T, methods?: ObjectMethodsKeys<T> | ObjectMethodsMap<T>): StubbedInstance<T> {
const stubObject = Object.assign(<sinon.SinonStubbedInstance<T>> {}, object);
const objectMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(object));
const objectMethods = getObjectMethods(object)
const excludedMethods: string[] = [
'__defineGetter__', '__defineSetter__', 'hasOwnProperty',
'__lookupGetter__', '__lookupSetter__', 'propertyIsEnumerable',
Expand Down Expand Up @@ -75,6 +75,20 @@ export function stubInterface<T extends object>(methods: ObjectMethodsMap<T> = {
})
}

function getObjectMethods(object: object): Array<string> {
const methods: Array<string> = [];
while ((object = Reflect.getPrototypeOf(object))) {
const keys = Reflect.ownKeys(object);
keys.forEach((key) => {
if (typeof key === 'string') {
methods.push(key);
}
});
}

return methods;
}

sinon['stubObject'] = stubObject;
sinon['stubInterface'] = stubInterface;

Expand Down

0 comments on commit 052ab08

Please sign in to comment.