Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: supportsScrollBehavior is not declared configurable

I am trying to spy on a function supportsScrollBehavior of angular platform service like below -

import * as platform from '@angular/cdk/platform';
  describe('Supporting Scroll Behaviour', () => {
    beforeEach(() => {
      const funcSpy = jasmine.createSpy('supportsScrollBehavior').and.returnValue(true);
      spyOnProperty(platform, 'supportsScrollBehavior', 'get').and.returnValue(funcSpy);
    });
  });
});

But it is giving me an error like below -

Error: supportsScrollBehavior is not declared configurable

In angular 8 it was working fine, but in Angular 9 version it is giving this error. Any pointers will be really helpful.

like image 497
Himanshu Avatar asked Oct 16 '25 03:10

Himanshu


1 Answers

It is not possible anymore to spy on individually exported functions. https://github.com/jasmine/jasmine/issues/1414

There are some workarounds that might work, but there isn't a "works for all" solution.

Quoting from above link:

Actually setting "module": "commonjs" in tsconfig.json for tests fixes this issue and you can use spyOn again.

For me, this didn't work. Jasmine needs a place to put the spy in, so I created a Wrapper class so that the spy is installed on that class instead of the module.

import { supportsScrollBehavior as cdkSupportsScrollBehavior} from '@angular/cdk/platform';

export class CdkWrapper {
    public static supportsScrollBehavior(...args) {
        return cdkSupportsScrollBehavior(...args);
    }
}

Which you use like this in the spec file:

spyOn(CdkWrapper , 'supportsScrollBehavior').and.returnValue(true);

Remember to also use that wrapper in the component you are testing!

CdkWrapper.supportsScrollBehavior()
like image 76
Annenarg Avatar answered Oct 18 '25 22:10

Annenarg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!