mardi 20 mars 2018

Angularjs test with Jest module injection

Trying to test angular services with Jest and got this error:

[$injector:nomod] Module 'superag' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

How do I mock my module 'superag' and make available to mathService?

Do I have to import the app.js file with the module declaration every test I make?

package.json

"jest": {
    "collectCoverageFrom": [
      "**/*.{js}",
      "!**/node_modules/**"
    ]
  },
  "devDependencies": {
    "angular-mocks": "^1.6.9",
    "jest-cli": "^23.0.0-alpha.0"
  },
  "dependencies": {
    "angular": "^1.6.9"
  }
}

math.service.js

function MathService(){

    var addTwoNumbers = function(x, y){
      return x + y;
    };

    return {
      addTwoNumbers
    };
  }
angular.module('superag').factory('mathservice', MathService);

math.service.test.js

require('angular');
require('angular-mocks');
require('./math.service.js');

angular = window.angular;

describe('Math service - addTwoNumbers', () => {

  beforeEach(
    angular.mock.module('superag')
  );

  var _mathservice;

  beforeEach(inject((mathservice) => {
    _mathservice = mathservice;
  }));

  it('1 + 1 should equal 2', () => {
    var actual = _mathservice.addTwoNumbers(1,1);
    expect(actual).toEqual(2);
  });

});

Aucun commentaire:

Enregistrer un commentaire