1

mymodule.js is defined as:

angular.module('mymodule', []).factory('MyModule', function () { /* my code */ });

I then have a mocha test script (test.js) like:

var chai = require('chai');
chai.use(require("chai-as-promised"));
var angular = require('angular');
require('mymodule.js');

When running test.js with mocha I get an error saying that angular is not defined.

I've instead tried to use angular.injector like:

var angular = require('angular');
var injector = angular.injector(['mymodule']);
var mymodule = injector.get('MyModule');

but then I get the error "Failed to instantiate module" as mymodule.js hasn't been loaded.

Is there a simple way to get this working?

1 Answer 1

1

Try changing var angular = ... in test.js to global.angular = ...

Explanation

Since mymodule.js is in a different scope than test.js, declaring angular as a var within test.js will not make it accessible within mymodule.js. You must attach it to the global object to make it available anywhere without requiring it.

See http://nodejs.org/api/globals.html#globals_global

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.