1

I stumble on a problem that I cannot solve. I tried to spy on a sub-modules method but got the error that the moduleSpy is not a spy with the following error message. The module is an npm package.

Error: <toHaveBeenCalled> : Expected a spy, but got undefined.

import * as module from 'package';
import { TOKEN } from 'injectionToken.ts'

describe('ExampleComponent', () => {
    let component: ExampleComponent;
    let fixture: ComponentFixture<ExampleComponent>;
    let moduleSpy;

    beforeEach(async(() => {
        moduleSpy = spyOn(module.sub, 'subModuleMethod');

        TestBed.configureTestingModule({
            declarations [ExampleComponent],
            providers: [
                { provide: TOKEN, useValue: module }
            ]
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(ExampleComponent);
        component = fixture.componentInstance;
    });

    it('test specific function call', () => {
        // press button to test the sub module method
        expect(moduleSpy.subModuleMethod).toHaveBeenCalled();
    });

})

Does anyone have an idea why the spy is not recognized?

john doe
  • 11
  • 3

1 Answers1

0

What you have worked on earlier versions of Angular/TypeScript but not on later versions of it.

This is a long thread explaining it: https://github.com/jasmine/jasmine/issues/1414

These are some solutions: Can webpack 4 modules be configured as to allow Jasmine to spy on their members?

The github thread has some solutions but none of those solutions worked for me.

My favourite solution was this one though:

Error: supportsScrollBehavior is not declared configurable

AliF50
  • 16,947
  • 1
  • 21
  • 37