1

I am currently working on writing a simple test case for the driver script using IIFE(Immediately invoked function expression). Here is my driver script.

driver.js

(function() {
"use strict";

var app = angular
    .module("myApp", [
        "ui.bootstrap",
        "ui.sortable"
    ]);
}());

Here is my spec driver.spec.js

describe("application configuration tool driver", function() {
 it("should create an angular module named myTest", function() {
    expect(app).toEqual(angular.module("myApp"));
 });
});

When I run my spec using IIFE. I am getting a ReferenceError: app is not defined.

If I run the driver script without IIFE:

var app = angular
    .module("myApp", [
        "ui.bootstrap",
        "ui.sortable"
    ]);

My spec passes. Any thoughts on passing the spec using IIFE?

12
  • 1
    variable app is local to IIFE, but the test expect(app).toEqual(angular.module("myApp")); makes no sense to me. Commented Oct 20, 2015 at 20:49
  • why do you even need variable app? Commented Oct 20, 2015 at 20:51
  • I was using the variable app for creating custom directives and controllers. Commented Oct 20, 2015 at 20:53
  • @charlietfl some poorly written examples of how to use angular show var app in combination with app.controller ..., app.factory ... and unfortunately people aren't aware this is an anti-pattern. Commented Oct 20, 2015 at 20:54
  • @Claies I was reading about it recently but my intention in here was that all my controllers, directives are wrapped up in IIFE. So for a brief demo I used the variable app. Commented Oct 20, 2015 at 20:57

1 Answer 1

0

You can move app back to the outer scope (if that's an option you can take, of course):

var app;
(function(app) {
    "use strict";

    app = angular
        .module("myApp", [
            "ui.bootstrap",
            "ui.sortable"
        ]);
}(app));
Sign up to request clarification or add additional context in comments.

Comments

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.