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.

Mocking Angular Material modules do not work

See original GitHub issue

I’m trying to mock Angular Material modules to avoid using NO_ERRORS_SCHEMA when doing shallow components testing, but it’s not working for me.

At first, I tried mocking my material.module.ts which is just a module that re-exports all material individual modules, and it’s not working at all as I get “unknown element <mat-card” and such.

Then I tried mocking all material modules individually with modules.map((module: any) => MockModule(module)); and use that in my shared.module.mock.ts, a module that I import in all my unit tests for convenience. This module imports & exports the mocked material modules. This works better as I don’t get “unknown element” errors, but this time I’m stuck on this error:

 TypeError: Cannot read property 'indexOf' of undefined
            at HammerGesturesPlugin.isCustomEvent (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:1639:102)
            at HammerGesturesPlugin.supports (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:1577:75)
            at EventManager._findPluginFor (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:911:26)
            at EventManager.addEventListener (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:882:27)
            at EmulatedEncapsulationDomRenderer2.DefaultDomRenderer2.listen (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:1190:34)

Obviously, this error is not there when I use the real material modules.

Do you have any idea or workaround for this?

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
getsafcommented, Apr 26, 2019

This is an issue with auto-mocking the HAMMER_GESTURE_CONFIG. You will want to use the “real” version of that provider. MockModule auto-mocks the config which causes HammerJS to crap out when it tries to access a property on the config.

I would recommend enhancing MockModule in a way that allows you to specify which items in the module you would like to avoid mocking.

As a hack, you can re-provide a dummy config on top of your mocked modules like so:

@NgModule({
  imports: [
    // ...
    ...MaterialModulesMocks
  ],
  /* This is a hack.. You really want to avoid doing this at the MockModule level */
  providers: [
    {provide: HAMMER_GESTURE_CONFIG, useValue: {events: []}}
  ],
  // ...
})

Here’s your first StackBlitz with this hack applied: https://stackblitz.com/edit/ng-mock-material-issue-nnceql?file=src/app/shared.module.mock.ts

0reactions
satanTimecommented, Feb 20, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking Angular Material modules do not work #50 - GitHub
I'm trying to mock Angular Material modules to avoid using NO_ERRORS_SCHEMA when doing shallow components testing, but it's not working for ...
Read more >
angular mock `module` resulting in '[Object object] is not a ...
I'm trying to create some unit tests in Angular using Jasmine being run through Teaspoon. The tests are running ...
Read more >
Testing with component harnesses - Angular Material
A component harness is a class that lets a test interact with a component via a supported API. Each harness's API interacts with...
Read more >
The Jest Object
The methods in the jest object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by...
Read more >
packaged angular-mocks - npm
Start using angular-mocks in your project by running `npm i ... There are 212 other projects in the npm registry using angular-mocks.
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