Open bederuijter opened 11 months ago
I have the same issue with version 15, i tried another way to use MatTestDialogOpener but i came to the same result.
**Error: NG0202:** This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.
Please check that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.
Here is my MatTestDialogOpener implementation into my before each.
I was wondering that MatTestDialogOpener can wrap my dialog component into a mocked component, so i can test my dialog outside of any parent component context. Thats why I start with calling MatTestDialogOpener.withComponent to create my wrapper component.
Is it the correct way to do this ?
beforeEach(async () => {
DialogWrapperComponent = MatTestDialogOpener.withComponent(ResultsComponent, { data: questionary });
await TestBed.configureTestingModule({
declarations: [
DialogWrapperComponent,
ResultsComponent
],
imports: [
SharedModule //Contians MatDialogModule
]
}).compileComponents();
fixture = TestBed.createComponent(DialogWrapperComponent);
componentWrapper = fixture.componentInstance;
component = componentWrapper.dialogRef.componentInstance;
compiled = fixture.nativeElement;
fixture.detectChanges();
});
Environment Angular: 15.0.4 CDK/Material: 15.0.3 Browser(s): Edge Operating System (e.g. Windows, macOS, Ubuntu): Windows 10
Doing some Googling - and looking at the error message - looks like something that is expected is missing for dependency injection. Not sure where, yet.
Tangentially-related issues: https://github.com/thymikee/jest-preset-angular/issues/467 https://github.com/angular/angular/issues/45155
I also confirmed this behavior - the errors - is consistent in Angular+Material 14.x, 15.x, 16.x, and 17.x when added to an empty, new Angular application created via: ng new
I'm now discovered that if you copy the files: dialog-opener.spec.ts dialog-opener.ts
from the Angular Material directory: src/material/dialog/testing/
and place them in your Angular directory: src/app/
and then modify the imports in dialog-opener.spec.ts to import directly from your local version of dialog-opener.ts like so:
import {MatTestDialogOpenerModule, MatTestDialogOpener} from './dialog-opener';
then the tests in dialog-opener.spec.ts will run successfully locally.
This probably explains why the Angular Material team had no problems running the tests in dialog-opener.spec.ts successfully when run in the Angular Material project.
Is this a regression?
The previous version in which this bug was not present was
No response
Description
The MatDialog API mentions a
MatTestDialogOpener
that can be used when writing tests that involve a mat-dialog.Instead, when using the
MatTestDialogOpener
when writing my test it throws an error:Reproduction
I don't know how to provide a StackBlitz reproduction that involve a test. Using the mat dialog opener test I was still able to reproduce the error (In the hopes of eliminating user error 🙂)
Expected Behavior
To be able to write tests using the
MatTestDialogOpener
Actual Behavior
MatTestDialogOpener
throwing an error related to the Dependency injectionEnvironment