ReferenceError: module is not defined in jasmine

13,829

Solution 1

You're referencing window.module from the test code. You need to load angular-mocks in your spec-runner to do that, see https://docs.angularjs.org/api/ngMock/function/angular.mock.module

Solution 2

Ok so I've been on this issue all day and finally found why it wasn't working even though I had angular mock script in my spec runner. The order matters here, and this is what worked for me.

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.4.0/jasmine_favicon.png">
  <link rel="stylesheet" href="lib/jasmine-2.4.0/jasmine.css">

    <script src="lib/jasmine-2.4.0/jasmine.js"></script>
    <script src="lib/jasmine-2.4.0/jasmine-html.js"></script>
    <script src="lib/jasmine-2.4.0/boot.js"></script>

  <script src="../../lib/angular/angular.js"></script>
  <script src="../../lib/angular/angular-animate.js"></script>
  <script src="../../lib/angular/angular-route.js"></script>
  <script src="../../lib/angular/angular-touch.js"></script>
  <script src="../../lib/angular/angular-sanitize.js"></script>
  <script src="../../lib/angular/angular-mocks.js"></script>

  <!-- include source files here... -->
  <script src="../student/data/classesData.js"></script>

  <!-- include spec files here... -->
  <script src="spec/LMSClassesSpec.js"></script>

I really hope this helps someone in the future

Share:
13,829
F11
Author by

F11

Technical Artist

Updated on June 12, 2022

Comments

  • F11
    F11 about 2 years

    When i am running SpecRunner.html, I am getting the following error

    ReferenceError: module is not defined

    My Controller is

    angular.module('mymodule', [])
      .controller('mycontroller', ['$scope',
        function($scope) {
          $scope.employees = [{
            name: 'Dick',
            address: 'Mumbai'
          }, {
            name: 'Tom',
            address: 'US'
          }];
          $scope.addEmployee = function() {
            $scope.employees.push({
              name: $scope.name,
              address: $scope.address
            });
          }
    
        }
      ])

    and my spec is

    describe('Employee', function() {
      var mycontroller, scope;
      beforeEach(module('mymodule'));
    
      beforeEach(inject(function($controller, $scope) {
        scope = $rootScope;
        mycontroller = $controller('mycontroller', {
          $scope: scope
    
        });
      }));
      it("when employee gets added", function() {
        var employeecount = $scope.employees.count;
        $scope.addEmployee('xxxx', 'yyy');
        var employeecount1 = $scope.employees.count;
        expect(employeecount + 1).toBe(employeecount1);
      });
    });

    My SpecRunner.html is

        <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.2.0/jasmine_favicon.png">
      <link rel="stylesheet" href="lib/jasmine-2.2.0/jasmine.css">
    
      <script src="lib/jasmine-2.2.0/jasmine.js"></script>
      <script src="lib/jasmine-2.2.0/jasmine-html.js"></script>
      <script src="lib/jasmine-2.2.0/boot.js"></script>
    
    
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script src="../Controller.js"></script>
        <script src="spec/myspec.js"></script>
    

    PS: Its my first Unit test in jasmine.