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

449
Vistas
reactjs jest jQuery is not defined

I am using jest to test my reactJS component. In my reactJS component, I need to use jquery UI, so I added this in the component:

var jQuery = require('jquery');
require('jquery-ui/ui/core');
require('jquery-ui/ui/draggable');
require('jquery-ui/ui/resizable');

And it worked fine. But, now, I need to used jest to do testing, but I immediately meet this issue when I load the component into testutils,

Test suite failed to run
ReferenceError: jQuery is not defined 

Has anyone met this issue if you are using jQuery in your app?

Thanks

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can add jQuery dependency in your global object. Do the following

  1. In your package.json, under jest key add setupFiles like this "setupFiles": ["test-env.js"] For example:
{
  "jest": {
    "setupFiles": [ "<rootDir>/tests/test-env.js" ]
  }
}
  1. Where the contents on test-env.js look like this
import $ from 'jquery';
global.$ = global.jQuery = $;

Make sure the path to test-env.js is correct from your root directory. <rootDir> is available in Jest.

over 4 years ago · Santiago Trujillo Denunciar

0

If you are using Jest 27+, node is now the default testEnvironment. If you need to use jQuery in jest setup script, make sure to first change the testEnvironment back to jsdom.

in jest.config.js:

module.exports = {
  setupFilesAfterEnv: ['./jest.setup.js'],
  testEnvironment: 'jsdom'
}

in jest.setup.js:

import $ from 'jquery';
global.$ = $;
global.jQuery = $;

// If you want to mock bootstrap
global.$.fn.modal = jest.fn(() => $());
global.$.fn.carousel = jest.fn(() => $());
global.$.fn.tooltip = jest.fn(() => $());
over 4 years ago · Santiago Trujillo 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