{"id":755,"hash":"bda86e59546a9623a71b4ab69cf31150611611b4a022e1ff9d50557516c9c840","pattern":"Karma unit testing error: Unexpected value imported by the module. Please add a @NgModule annotation","full_message":"I've created a fresh new component via:\n\nng g mytest1\n\nThen I changed the constructor line to this:\n\nconstructor(private dialogRef: MatDialogRef<Mytest1Component>) { }\n\n, and added the required import:\n\nimport { MatDialogRef } from '@angular/material';\n\nAfter that I ran the Karma unit test project via:\n\nng test\n\nThe test failed. I got this error message:\n\n  Error: StaticInjectorError(DynamicTestModule)[Mytest1Component -> MatDialogRef]: StaticInjectorError(Platform: core)[Mytest1Component -> MatDialogRef]: NullInjectorError: No provider for MatDialogRef!\n\nTo fix that I added the Import statement in the beforeEach section:\n\nimport { MatDialogRef } from '@angular/material';\n\n//...\n\nbeforeEach(async(() => {\n    TestBed.configureTestingModule({\n        declarations: [ Mytest1Component ],\n        imports: [MatDialogRef],\n    })\n    .compileComponents();\n}));\n\nNow I got this new error, which I am not able to fix:\n\n  Failed: Unexpected value 'MatDialogRef' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.\n\nCan someone clarify where I should be adding the @NgModule annotation, or if I had done something completely wrong?\n\nThank you.","ecosystem":"npm","package_name":"angular","package_version":null,"solution":"You are injecting MatDialogRef in component:\n\nconstructor(private dialogRef: MatDialogRef<Mytest1Component>) { }\n\nSo the testBed expects the same to be injected as provider to the TestBed. Or you can also provide a MockDialogueService to it.\n\nbeforeEach(async(() => {\n    TestBed.configureTestingModule({\n        declarations: [ Mytest1Component ],\n        providers: [ MatDialogRef ],\n    })\n    .compileComponents();\n}));","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/54772990/karma-unit-testing-error-unexpected-value-imported-by-the-module-please-add-a","votes":37,"created_at":"2026-04-19T04:51:36.213835+00:00","updated_at":"2026-04-19T04:51:36.213835+00:00"}