Error: Jasmine is unable to find the module that is being referenced

When running SpecRunner.html, an error is encountered

ReferenceError: module is not defined

The Controller code is as follows:

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

    }
  ])

The associated spec file looks like this:

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

The content of 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>

Note: This is the first Unit test in jasmine.

Answer №1

After struggling with this issue all day, I finally discovered the solution to why it wasn't working even though I had included the angular mock script in my spec runner file. The key factor turned out to be the order of the scripts, and here is the configuration that 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>

This configuration proved successful for me after hours of troubleshooting, and I genuinely hope it can assist others in similar situations in the future.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery technique for loading images

Here is the issue at hand: I am attempting to utilize jQuery to accelerate image loading on my webpage. To achieve this, I have a code snippet specifying an initial image that should be replaced with another image once the page has finished loading. < ...

What is the process for choosing a text area over regular text?

I am struggling with positioning the selected text in a textarea. I have successfully implemented it for regular text (paragraphs), but I need to adapt it for a textarea, and I am encountering some issues as I keep getting null for the text node. Below is ...

Resolving the Problem of Unidentified Usernames in Django Channels WebSocket Consumer

I have developed a django consumers.py file and a frontend using HTML and CSS to showcase messages sent by users, along with the profile picture and username of the sender. However, whenever I access the browser, the message displays correctly but the user ...

The gif loader persists even after subscribing

Looking to incorporate the SendGrid subscription widget into my site, but struggling with the implementation. The code provided by SendGrid is partially functional - the loader appears and a success message is displayed upon sign up, but the GIF loader doe ...

Having trouble reaching an element within a successful Ajax call

I have encountered an issue where the element is not being recognized when putting an ajax call inside another ajax call. Here is an example of the code: $.ajax({ url: 'controleFatAcoes.php', type: 'post', dataType: 'h ...

Utilizing JavaScript to Load and Showcase Images from a Temporary Image to imgsrc

How can I dynamically display images from the temporary image array 'tempimages' onto the 'img' element with the 'src' property? I have written a code that attempts to display temporary images from 'tempimages[]' on ...

selenium.common.exceptions.WebDriverException: Message: IndexError: array index out of bounds

I've been working on creating a web scraping tool that involves a Python script and a JavaScript code. The Python script is designed to call the JavaScript code, which then extracts relevant content from a web page and sends it back to the Python scri ...

Why Promise.all() isn't working as expected - where am I going wrong?

Currently, I am in the process of developing a script that utilizes the GitHub API. The script includes a function that takes a list of usernames as input. Each username triggers an API request to fetch that user's starred repositories. For each starr ...

$set { "array.variable.value" :"newvalue"} utilize a different term besides "variable" or implement a new variable

When working with mongoose to store user data, one of the attributes is an array called items. In my additems.js file: const User = require('../models/User'); var array = user.items; array.indexOfObject = function (property, value) { ...

Loading screen while all files and audio are being loaded

My JavaScript code is responsible for displaying and playing audio on my website. Unfortunately, the load time of the page is quite slow. To address this issue, I decided to follow a tutorial on installing a preloader, which can be found at . Although I ...

Troubleshooting MongoDB aggregate lookup failure when using multiple parameters

In my data retrieval process from the comments table, everything is functioning properly. However, I am aiming to optimize performance by performing a join equivalent on the users collection to fetch additional user details associated with each comment. B ...

Exploring the endless possibilities of querying MongoDB with ever-changing variables

I'm currently working on using node.js to retrieve json results from MongoDB. var http = require('http'); var mongo = require('mongoskin'); http.createServer(function (req, res) { var args = req.url.split("/"); console.log(ar ...

Unable to create a clickable button within a CSS3DObject using Three.js

How can I create an interactive button on a CSS3DObject within a cube made up of 6 sides? The button is located on the yellow side, but I'm facing issues with clicking on it. Whenever I attempt to click on the button, the event.target always points to ...

Assign local variable in Mongoose using query results

I created a function to query MongoDB for a credential (simplified): var query = Credential.findOne({token: token}); return query.exec(function (err, credential) { if (err) return null; return credential; }); The next step is to use this credent ...

javascript - increase counter by milliseconds

I am looking to enhance my JavaScript quiz counter by adding milliseconds. Currently, the quiz only tracks and displays time in seconds (stored in MySQL as a number of seconds, like 120 for 2 minutes displayed as 02:00). I now want to include milliseconds ...

Increase the size of the Bootstrap select input area to exceed the dimensions of

Is there a way to ensure that the selection area of my bootstrap select is not larger than its parent container on small screens (mobile)? My issue can be seen in the image below and is recreated in the snippet provided. The black bar represents the edge ...

Retrieve a PDF file from an HTTP response using Axios

I'm currently working on a project that involves integrating Vue with a Laravel API as the back-end. One of the requirements is to be able to download PDF files from the server when a certain link is clicked. I am using axios for making GET requests, ...

Sending JavaScript objects from React.js to Node.js

I send the object to the backend (Node js) when making an API call, but I am logging the objects for verification. Initially, I used POSTMAN to check and it worked perfectly. However, when attempting to pass an object in the frontend (Reactjs), it appear ...

Certain JavaScript code might fail to load on WordPress

I've been struggling to troubleshoot an issue with a JavaScript accordion menu on my WordPress site. When I try to click the accordion button, it doesn't reveal the hidden content beneath it. I've attempted various solutions, but none have s ...

"Enhancing user interaction with Vue.js through the stunning ripple effect on

Is there a way to customize the ripple effect from this source so that it doesn't always have to be a DIV? Here's an example: Currently, when I implement it like this: <ripple class="btn">send</ripple> It works as expected, but as ...