Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda