Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dialog is not removed during tests, leading to order-dependent tests #23385

Open
jbeder opened this issue Aug 16, 2021 · 11 comments
Open

Dialog is not removed during tests, leading to order-dependent tests #23385

jbeder opened this issue Aug 16, 2021 · 11 comments
Labels
area: material/dialog G This is is related to a Google internal issue P2 The issue is important to a large percentage of users, with a workaround

Comments

@jbeder
Copy link
Contributor

jbeder commented Aug 16, 2021

I have a set of screenshot tests using Angular Catalyst that pass when run A, B but fail when run B, A; the failure is that the screenshot for A, when run after B, shows the shadow of the dialog that was created for B. (I'm not linking to them because they're internal to Google.)

If I add:

afterEach(async () => {
  const dialog = await getHarness(MatDialogHarness, {useDocumentRoot: true});
  await dialog.close();
});

to test B, then they pass in either order. The test infrastructure should clean this up automatically to keep the tests hermetic.

@josephperrott indicated that "it is created by an injector which doesn't belong to a specific NgModule so it isn't torn down".

@josephperrott josephperrott added area: material/dialog G This is is related to a Google internal issue labels Aug 18, 2021
@crisbeto crisbeto added the P2 The issue is important to a large percentage of users, with a workaround label Aug 18, 2021
@crisbeto
Copy link
Member

It's because TestBed doesn't destroy providedIn: 'root' providers between tests. We have a workaround for it in our own tests: https://github.com/angular/components/blob/master/src/material/dialog/dialog.spec.ts#L86. We're also currently discussing how to handle this.

@crisbeto
Copy link
Member

@jbeder have you tried using the new teardown flag in TestBed? We have a test in the Framework repo that verifies something similar to what you're describing. The new flag should be exposed in Catalyst as well.

@jbeder
Copy link
Contributor Author

jbeder commented Aug 19, 2021 via email

@crisbeto
Copy link
Member

I spent some more time trying to reproduce it, but the only way I could get it to happen was to turn off the teardown flag. Maybe the teardown flag isn't being passed through Catalyst correctly? Have you tried writing the same test using TestBed directly?

@jbeder
Copy link
Contributor Author

jbeder commented Aug 23, 2021

If you'd like a reproducible example, see CL/392502204.

@mmalerba
Copy link
Contributor

@jbeder I think the issue may be tests from other files that do not set the new flag and wind up getting bundled in with the tests in the file you edited. I changed the describe blocks in your CL to describe.only and it passed.

I think there still might be some kind of bug here though, because it shouldn't matter if other describe blocks set different teardown settings. To solve your immediate problem though, you might try just making sure that all of the describe blocks in that compilation unit set the new teardown flag

@jbeder
Copy link
Contributor Author

jbeder commented Aug 23, 2021

@mmalerba First, there is indeed one test in a different file that doesn't set the teardown flag, and I set it to true, and sure enough, the test passes. (The strange thing is that it's a nearly trivial test - it doesn't do anything other than instantiate a very simple injectible class, which is why I didn't add the teardown flag to it.)

But second, I'm confused by your analysis - won't describe.only not even run the other tests (which are the ones failing)? The particular interaction here is that the tests in that one file pass, but when they happen to be executed before tests in a different file, those tests don't pass.

@mmalerba
Copy link
Contributor

mmalerba commented Aug 23, 2021

Ah I didn't know that. I thought the tests in the CL were the only ones you were concerned with, so I just used describe.only as a way of ensuring that only tests with the flag set were being run. The reason I suspected this is that Catalyst apparently works differently than TestBed externally in that it calls initTestEnvironment before every test (thanks to @crisbeto for pointing this out). I thought it was possible that it wasn't actually updating the behavior with the new flag settings on subsequent calls, so I wanted to make sure all the tests were specifying the flag.

Obviously if that is what's happening, it's still a bug of some sort, but you could at least work around it by making sure that all of your tests have the flag

@jbeder
Copy link
Contributor Author

jbeder commented Aug 27, 2021 via email

@jbeder
Copy link
Contributor Author

jbeder commented Aug 27, 2021

Also, related: is the plan for this flag to be automatically set for all tests? (If not, why not?)

@crisbeto
Copy link
Member

It'll be turned on by default in v13, but we'll have an automated migration that opts existing users out of it explicitly. We can't turn it on by default, because there's a possibility of breaking existing tests. E.g. when we enabled it on our own repo, it revealed some test failures that took a while to track down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/dialog G This is is related to a Google internal issue P2 The issue is important to a large percentage of users, with a workaround
4 participants