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

79
Vistas
Require Not Working During Mocha Test On Extended Javscript Class

I am attempting to npm mocha test a class that extends another class in another js file.

The problem I'm running into is that the code works fine when running in production but when running npm test ClassTwo cannot see class one regardless that I'm requiring it or not. If I add the definition for class one in the file for class two then it works fine.

I can't put a require in ClassTwo.js because it causes the browser code to explode since require isn't a concept there.

ClassOne.js

class ClassOne{

}

if(typeof module !== 'undefined'){
    module.exports = ClassOne;
}

ClassTwo.js

class ClassTwo extends ClassOne{

}

if(typeof module !== 'undefined'){
    module.exports = ClassTwo;
}

test.js

const ClassOne = require('../js/ClassOne.js');
const ClassTwo = require('../js/ClassTwo.js');

... unit tests ...

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

0

The answer was to create a global variable in the test.js file that was named the same as my extended class and then hydrate it with the require command.

test.js

const { global } = require("@sinonjs/commons");

global.ClassOne = require('../js/ClassOne.js');

const ClassTwo = require('../js/ClassTwo.js');
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