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

127
Vistas
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 Respuestas
Responde la pregunta

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 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