Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
Sinon: calling a method from the child object but spying the one from the grand parent class only

Having in javascript

class GrandParent {
  myfunc() {
    console.log('Parent myfunc');
  }
}


class Parent extends GrandParent {
  myfunc() {
    // do something else here
    for (let i = 0; i < 3; i++) {
      super.myfunc()
    }
  }
}

class Child extends Parent {
  mymeth() {
    // do something here
      this.myfunc();
    }
}

let spy = sinon.spy(Child.<?>, "myfunc")

let child = new Child();
child.myfunc();

console.log(spy.callCounts); --> 3 expected

I have only access to Child class (through a require which exports only this class, this must not be changed) and I would like to spy the myfunc() method from GrandParent class in order to get spy.callCounts === 3 finally.

Is it possible to do and how?

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to stub on GrandParent.prototype.

Also, for safety reasons, it's better to first install spies/stubs, then create objects:

If it's not accessible by imports, you can use reflection (in this case "twice":

const parentConstructor = Reflect.getPrototypeOf(Child)
const grandpaConstructor = Reflect.getPrototypeOf(parentConstructor);

let spy = sinon.spy(grandpaConstructor.prototype, "myfunc")
let child = new Child();
child.myfunc();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda