$ is undefined in unit test mocha

2

I am using mocha to perform my unit tests of the web layer, and it generates the error:

  

$ is not defined

Can I be missing something else to recognize the $ of jQuery ?

The code of my test is:

var assert = require('assert');
var chai = require('chai');
var expect = chai.expect;
var utils = require('../js/utils');


describe('UtilsSuite', function() {

    describe('#isUsuarioAdministradorExperto c2', function() {
        before(function () {
            global.$ = global.jQuery = require('jquery');
        });

        it('Cuando el estado no pertence entre los disponible retorna vacio ', function (){
            console.log(utils.mostrarFechaSolucion('02/02/2002','SITIO_GSM_NORMAL'));
            assert.equal("",utils.mostrarFechaSolucion('02/02/2002','SITIO_GSM_NORMAL'));
        });
    });


});

and the code of my function is:

    exports.mostrarFechaSolucion = function (fechaSolucion, estadoTecnico) {
    return ($.inArray(estadoTecnico, estadosFechaSolucion) > -1) ?
         "<tr><td class='encabezado'>Fecha soluci&oacute;n:</td><td class='datosSitio'>" + fechaSolucion + "</td></tr>" : "";
};
    
asked by Gustavo 02.01.2017 в 21:34
source

1 answer

1

The problem may be that jQuery is not starting because it can not find the DOM API. Try jsdom-global to inject document, window and other DOM APIs into your Node.js environment.

> this.jsdom = require('jsdom-global')(); 
> global.$ = global.jQuery = require('jquery');

You can also try to put that at the beginning of your mocha test.

    
answered by 24.02.2017 в 20:07