Repository: angular
Issue: createAngularJSTestingModule() breaks tests when used multiple times across unrelated spec files
Labels: type: bug/fix,workaround1: obvious,area: upgrade,state: confirmed,P5

# 🐞 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:

![image](https://user-images.githubusercontent.com/1225659/71955749-79461b00-31e9-11ea-8eeb-9aded7cbe27b.png)

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:
![image](https://user-images.githubusercontent.com/1225659/71956179-c1b20880-31ea-11ea-9cc0-5f3123d83dde.png)

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?

@petebacondarwin