createAngularJSTestingModule() breaks tests when used multiple times across unrelated spec files
See original GitHub issueđ bug report
Affected versions
This bug is present since the very beginning (March, 22th, 2019) when createAngularJSTestingModule() was first added to @angular/upgrade/static/testing (there have only been two commits in that file, angular\packages\upgrade\static\testing\src\create_angularjs_testing_module.ts).
This problem only occurs (I suppose) with Jasmine. When using Jest, I think the test runs are already isolated so the problem might not show up there.
Description
Calling createAngularJSTestingModule() immediately calls (under the hood) angular.module(...), thus it âgloballyâ adds the required angularJSTestingModule to the âglobal storageâ of all angularJS modules. This works fine so long as you always require the same angularModules (like in createAngularJSTestingModule(angularModules)). However, once you have multiple test suites (i.e. different and totally unrelated spec files), youâll start to require different angularModules from time to time.
Now, due to createAngularJSTestingModule() immediately calling angular.module(), all previous invocations get overriden. âIn the endâ, only the last call âwinsâ with its required angularModules. All previous calls are in vane, making their respective test suites fail, because they donât have (or load) the required angularModules.
To understand why this happens, you need to recall that Jasmine will first execute ALL describe blocks for ALL test files that are part of the test run (I guess this is needed so that Jasmine can provide the âfocused specsâ feature). Thus, when you stick to the notation provided in the official docs, youâre lost:

A workaround for the problem is to not call angular.mock.module() directly, but provide a function to beforeEach instead (which will only be called later, just before the test is run):
beforeEach(() => angular.mock.module(createAngularJSTestingModule(angularModules))); // notice the "() =>" to the left
The simplest solution would be to update the docs accordingly. However, when youâre using angular.mock.module.sharedInjector() youâll most likely have a beforeAll instead of an beforeEach. While the workaround looks the same (just replace the beforeEach with beforeAll), you can no longer use a beforeAll in combination with inject(). Those calls need to be replaced instead with a beforeEach. This is due to beforeAll(inject(...)) creating the injector too early which makes TestBed feel uncomfortable (erroring).
Maybe thereâs yet another, better, solution?
When looking at the source code:

Maybe itâs feasible to use an internal counter to create different modules each time?
Or maybe itâs possible to not call angular.module() at all, and provide a simple âmodule functionâ instead, that will be unique to the test suite?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (7 by maintainers)

Top Related StackOverflow Question
Thanks @petebacondarwin @gkalpak for your time and looking at this. If youâre ok with it, I would like to try my idea with that internal counter and provide a PR. What do you think?
That would be marvellous @NicBright ! Thanks