Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!