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

stubConstructor does not stub base class methods #137

Open
laleksiunas opened this issue Nov 6, 2020 · 6 comments
Open

stubConstructor does not stub base class methods #137

laleksiunas opened this issue Nov 6, 2020 · 6 comments

Comments

@laleksiunas
Copy link

laleksiunas commented Nov 6, 2020

Steps to reproduce:

  • Have a base class with a method named "foo"
  • Extend the base class and add a method named "bar"
  • Use stubConstructor to stub methods of the latter class

Code sample:

class A {
  foo() {
    return 'a';
  }
}

class B extends A {
  bar() {
    return 'b';
  }
}

const stubbedB = stubConstructor(B);

console.log(stubbedB.bar) // [Function: functionStub]
console.log(stubbedB.foo) // undefined
@joshystuart
Copy link

Is there some kind of work around for this? I guess an interface that defines all methods?

Would this be a difficult thing to fix? I may be able to help.

@Emmytobs
Copy link

Is there some kind of work around for this? I guess an interface that defines all methods?

Would this be a difficult thing to fix? I may be able to help.

I'm having a similar problem.

I haven't been able to find a way to stub parent methods in a child class.
A sample code to demonstrate:

import express from 'express';
class BaseController {
  error(res: express.Response, errorMessage: string, statusCode: number) {
    res.status(statusCode).json(errorMessage)
  }
}

class AuthController extends BaseController {
  constructor() { super() }
  verifyAuthToken(req, res, next) { // normal express.js style for controllers
    const token = getToken() // imagine this was a function that returns some auth token
    if (token === undefined) {
      super.error(res, 'Auth token not provided', 400);
    }
  }
}

I had some code similar to what's above and I wanted to write a test to make sure when the token is undefined, the error method on the parent class will be called. But I could not test that the error method was called:

import { stubObject, stubInterface, stubConstructor } from "ts-sinon";
import { expect } from "chai";

const authServiceStub = stubObject<AuthService>(authService);

describe('AuthService', () => {
  describe('verifyAuthToken method', () => {
      it('should call super.error when no getToken returns undefined', () => {
        
        ....

        authServiceStub.verifyAuthToken(req, res, next)
        expect(authServiceStub.error.callCount).to.be.equal(1)
        expect(authServiceStub.error.calledWith('Auth token not provided')).to.be.true
      })
  })  

When I run the test, I get the following error: TypeError: Cannot read property 'callCount' of undefined

Please I really need help on this! Is there a specific way to create stubs for parent methods of a class?

@laleksiunas
Copy link
Author

I guess the workaround might be to use stubInterface and manually stub all the methods you need for a specific test, however, this is not ideal.

@AvantaR
Copy link
Contributor

AvantaR commented Sep 8, 2021

Have the same issue. Any ideas?

@AvantaR
Copy link
Contributor

AvantaR commented Sep 8, 2021

@laleksiunas @Emmytobs @joshystuart

I've dug into it a little, and found out that it works when I transform my typescript code to ES5. Everything above that doesn't work. It's connected with Object.getPrototypeOf() and classes in >ES5. Check your tsconfig.json settings, and please confirm if it works for you as well with ES5.

@AvantaR
Copy link
Contributor

AvantaR commented Sep 10, 2021

I've created a pull request which should resolve this issue. Please check it -> #251

ttarnowski added a commit that referenced this issue Sep 16, 2021
Fix for class inheritence issue #137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants