browserify compatibility should allow code to be run server-side #11449
Description
Thanks everyone for #10732 and especially @linclark for documenting it.
I don't know why anyone would want to run their AngularJS application or library on the server, but running tests headlessly in that context is very convenient.
For anyone interested who wishes to test AngularJS code via NodeJS (say via Mocha or something), you'll need the jsdom
module, with some sort of fixture such as this in your spec file(s):
// AngularJS needs document and window globals to run
global.document = require('jsdom').jsdom('<html><head></head><body></body></html>');
global.window = global.document.parentWindow;
// because AngularJS' index.js expects angular to be in the global namespace,
// we need to fake it a bit.
// we make this an object so we can easily extend it with the real angular object below
global.angular = {};
// returns the empty object above
var angular = require('angular');
// extend the local angular object with the contents of global.window.angular,
// which is the real angular object.
global.window.angular.extend(angular, global.window.angular);
I'd be interested if we could somehow avoid this boilerplate by modifying index.js
. Adding jsdom
as an npm dependency may not be feasible (angular-mocks
might want to do this, however--I'd like to hear opinions on that), but perhaps index.js
could detect the presence of global.window
and global.document
and instead export global.window.angular
?