6

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?

Ps.: I've tried with beforeEach(module('superag')) too without success

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');

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);
  });

});

2 Answers 2

5
+25

This error occurs when you declare a dependency on a module that isn't defined anywhere or hasn't been loaded in the current browser context.

When you receive this error, check that the name of the module in question is correct and that the file in which this module is defined has been loaded (either via <script> tag, loader like require.js, or testing harness like karma).

A less common reason for this error is trying to "re-open" a module that has not yet been defined.

To define a new module, call angular.module with a name and an array of dependent modules, like so:

// When defining a module with no module dependencies,
// the array of dependencies should be defined and empty.
var myApp = angular.module('myApp', []);

To retrieve a reference to the same module for further configuration, call angular.module without the array argument.

var myApp = angular.module('myApp');

Calling angular.module without the array of dependencies when the module has not yet been defined causes this error to be thrown. To fix it, define your module with a name and an empty array, as in the first example above.

Sign up to request clarification or add additional context in comments.

6 Comments

Inside my test or into my service?
Already tried to define into BeforeEach without success... :(
Tried with beforeEach(module('superag')) and beforeEach(angular.module('superag', []))
if you want to mock a module maybe you can try this beforeEach(function(){ module('moduleToMock'); module(function ($provide) { $provide.value('yourService', serviceMock); }); });
without success ... I've created a repo with my tries, could you have a look? I appreciate your effort so far
|
1

You need to load the module under test using the module function provided in angular-mocks, so it is available in your tests, per docs.

beforeEach(module('superag'))

Then you can inject in your service.

3 Comments

Where is the superag module defined... can you provide a fiddle?
That's the case. I need to define superag inside the test, not require another file.
Thanks for your time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.