Skip to content

prefer-spy-on: skip over private methods as linted code doesn't compile #1812

@rnveach

Description

@rnveach

original test:

    const privateMethodSpy = component['privateMethod'] = jest.fn();

    component.publicMethod();

    expect(privateMethodSpy).toHaveBeenCalledTimes(1);

The original test runs without issue.

linted test:

    const privateMethodSpy = jest.spyOn(component, 'privateMethod').mockImplementation();

    component.publicMethod();

    expect(privateMethodSpy).toHaveBeenCalledTimes(1);

The linted code fails to compile with "No overload matches this call".

No overload matches this call.
  Overload 1 of 3, '(object: Component, method: never): SpyInstance<never, never, any>', gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'never'.
  Overload 2 of 3, '(object: Component, method: "ngOnInit" | "ngOnChanges" | "ngOnDestroy"): SpyInstance<void, [], any>', gave the following error.
    Argument of type '"privateMethod"' is not assignable to parameter of type '"ngOnInit" | "ngOnChanges" | "ngOnDestroy"'.
ts(2769)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions