60

I'm getting following error when I try to test an angular component:

Error while running jest tests:

Can't bind to 'matMenuTriggerFor' since it isn't a known property of 'button'.

Here is my html:

<button mat-button [matMenuTriggerFor]="menu">Menu</button>
<mat-menu #menu="matMenu">
  <button mat-menu-item>Item 1</button>
  <button mat-menu-item>Item 2</button>
</mat-menu>`

I'm using "@angular/material": "6.1.0", in my package.json. I've also imported all the required material dependencies in the beforeAll block under TestBed I also tried changing the property of the button from matMenuTriggerFor to mat-menu-trigger-for. It didn't work.

Please suggest how can I fix this test.

Edric
  • 24,639
  • 13
  • 81
  • 91
RV.
  • 2,781
  • 3
  • 29
  • 46

8 Answers8

86

Import MatMenuModule in your feature Module OR the Module where this component resides in.

 import { MatMenuModule} from '@angular/material/menu';

and

imports: [
  MatMenuModule
]
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
7

I think you need to add the MatMenuModule import to your app.module file.

Paul
  • 460
  • 2
  • 7
2

Random but in case someone was searching like me: I was importing a custom angular material library and building the consuming library before building the material library. when I built the material library first then the consuming library was about to see the MatMenuModule and errors went away.

I changed

"library:build": "npm run ng7-common:build" && npm run ng8-material:build

to:

"library:build": " npm run ng8-material:build && npm run ng7-common:build"
Sankofa
  • 600
  • 2
  • 7
  • 19
0

Check the spelling I had one g in word trigger that cost me this error

[matMenuTrigerData] => [matMenuTriggerData] 
Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
0

I got this (and several other errors) because I failed to add the component to the declarations array in the lib.module.ts file.

Trevor
  • 13,085
  • 13
  • 76
  • 99
0

I was getting this error while running jest test. I had to add MatMenuModule in the import array of TestBed.configureTestingModule to resolve this error.

Thirumal
  • 8,280
  • 11
  • 53
  • 103
0

If imports at other module, you need export MatMenuModule.

At your module, place:

... exports: [MatMenuModule], ...

Andre Mesquita
  • 879
  • 12
  • 25
0

import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';