lundi 11 juillet 2016

ES6 mocha chai JSPM testing with libraries

I'm setting up ES6 unit tests in my project and I am having some trouble making them work with libraries. I thought I'd use jQuery just as a test to try and make it work. Without libraries, the tests work.

Note that jQuery is imported into my main.js file so it is available throughout the project.

My JS file looks like this:

class Test {
  constructor(options) {
    this.init();
  }

  init() {
    $('.test-div').addClass('test');
  }

  sayHello() {
    return 'hello world!';
  }
}

export default Test;

And the test looks like this:

import jsdom from 'mocha-jsdom';
import chai from 'chai';
import Test from './test';

chai.should();

describe('Frequency', () => {

  var $;
  jsdom();

  before(() => {
    $ = require('jquery');
  })

  it('should output hello world', () => {
      const test = new Test();
      test.sayHello().should.equal('hello world!');
  });

});

If I remove the init() function, the test works. However, the before function doesn't seem to import jQuery for the test. The error I receive in the console is as follows:

ReferenceError: $ is not defined
  at Frequency.init

Aucun commentaire:

Enregistrer un commentaire