question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

image

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

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
NicBrightcommented, Feb 4, 2020

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?

0reactions
petebacondarwincommented, Feb 4, 2020

That would be marvellous @NicBright ! Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6 Unit Tests: An error was thrown in afterAll ...
Out of 10 times, 8 times it would fail with afterall error. Turns out, in spec.ts files, the imports should have HttpClientTestingModule ...
Read more >
Diagnosing Random Angular Test Failures - WalkingRiver.com
I am using Angular 10 in this specific project, writing unit tests with ... The fact that the new test and the failing...
Read more >
createAngularJSTestingModule - Angular
A helper function to use when unit testing AngularJS services that depend upon downgraded Angular services. See more... createAngularJSTestingModule( ...
Read more >
Testing Components – Testing Angular
Setting up a Component test using Angular's testing Module; Getting familiar with Angular's Component testing abstractions; Accessing the ...
Read more >
Modern Best Practices for Testing in Java - Philipp Hauer's Blog
A usual reflex of a developer is to extract values that are used multiple times to variables. // Don't @Test public void variables()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found