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

204
Vistas
CommonJS require won't work inside Jest test file

I am testing my backend with Jest, but I keep getting this error

\node_modules\generate-key\lib\generate.coffee:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){rn = (max) ->
                                                                                              ^

SyntaxError: Unexpected token '>'

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
  at Object.<anonymous> (node_modules/generate-key/index.js:2:18)

This is my jest config file:

   module.exports = { clearMocks: true, coverageDirectory: 'coverage', testEnvironment: 'node',moduleFileExtensions: ['ts', 'js', 'json', 'node'] };

My test file:

/* eslint-disable no-undef */
const { getConvertedKind } = require('../common');

test('check', () => {
  expect(3).toBe(3);
});

The imported function:

exports.getConvertedKind = kind => {
  const kinds = {
    cinemaPass: 'cinema',
    hotelPass: 'hotel',
    boardingPass: 'flight',
    coupon: 'coupon',
    eventTicket: 'event',
    transit: 'transit',
    storeCard: 'wallet',
    offer: 'offer'
  };
  return kinds[String(kind)] || null;
};

package.json:

"scripts": {
"start": "nodemon server.js",
"start:prod": "NODE_ENV=production nodemon server.js",
"debug": "ndb server.js",
"test": "jest --config ./jest.config.js"

}

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

0

It seems like you're trying to load a .coffee file and by default jest doesn't transform node_modules.

You can check the transformIgnorePatterns option and try something like:

transformIgnorePatterns: ["node_modules/(?!generate-key)"],
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