Skip to content

Instantly share code, notes, and snippets.

@reebalazs
Last active August 29, 2015 14:07
Show Gist options
  • Save reebalazs/88060bec6e5be51f0efc to your computer and use it in GitHub Desktop.
Save reebalazs/88060bec6e5be51f0efc to your computer and use it in GitHub Desktop.
Karma unit testing AngularJS submodules with config(), run()
// DRAFT, UNTESTED
// Originally you have an Angular app that
// is decomposed to submodules. Each submodule has its own
// config and run handlers. One submodule (lib3)
// also depends on another submodule.
module('myapp', ['lib1', 'lib2', 'lib3'])
.config(...)
.run(...);
module('lib1', ['ext1', 'ext2', 'ext3'])
.config(...)
.run(...);
module('lib2', ['ext4', 'ext5', 'ext6'])
.config(...)
.run(...);
module('lib3', ['lib2', 'ext7', 'ext8', 'ext9'])
.config(...)
.run(...);
// Now, you want to unittest each of your submodules
// independently However, the tests should not load
// any of the config and run handlers.
// A solution is to reorganize your modules in the following way:
module('myapp', ['lib1', 'lib2', 'lib3']);
module('myapp-run', ['lib1-run', 'lib2-run', 'lib3-run'])
.config(...)
.run(...);
module('lib1', ['ext1', 'ext2', 'ext3']);
module('lib1-run', ['lib1']);
.config(...)
.run(...);
module('lib2', ['ext4', 'ext5', 'ext6']);
module('lib2-run', ['lib1']);
.config(...)
.run(...);
// If modules depend on each other, they should
// depend on the base module.
module('lib3', ['lib2', 'ext7', 'ext8', 'ext9']);
module('lib3-run', ['lib1', 'lib2-run']);
.config(...)
.run(...);
// When the application is started, it must be bootstrapped as 'myapp-run',
// instead of just 'myapp', as previously.
// Then, a submodule can be tested in the following way, avoiding config()
// and run() to kick off:
beforeEach(function(){
// use one or more submodules
module('lib1');
module(function ($provide) {
// Optionally, mock a few components in the module
// $provide.value('yourService', serviceMock);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment