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

123
Views
Nodejs: import class and its constructor in jest tests

I am noob with JS and I can't figure how to instantiate one of my objects in this jest unit test (backend / nodejs project)

project structure:

appi/
    src/
        configFactory.js
        ...
    test/
        configFactory.test.js
        ...
    package.json

Using require

configFactory.js

class ConfigFactory {
     constructor(index_mapping){
        this.mapping = index_mapping
    }
}

configFactory.test.js

const ConfigFactory = require('../src/configFactory.js')
var fs = require('fs');


test('some test', () => {
    fs.readFile(__dirname +'/__mock-data__/Mappings/mappings_ac.json', 'utf8', function(err, data) {
        if (err) throw err;
        const factory = new ConfigFactory(data);
    });
});

This ends up with a TypeError: ConfigFactory is not a constructor

Using import

class ConfigFactory {
     constructor(index_mapping){
        this.mapping = index_mapping
    }
}

export default ConfigFactory
import ConfigFactory from "../src/configFactory"

ends up with SyntaxError: Cannot use import statement outside a module. I tried to add "type": "module" to package.json but I feel that I am missing an important point

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Looks like I was not properly exporting my class for CJS:

class ConfigFactory {
     constructor(index_mapping){
        this.mapping = index_mapping
    }
}

module.exports = ConfigFactory
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!