tlvince / eslint-plugin-jasmine

ESLint rules for Jasmine
https://www.npmjs.com/package/eslint-plugin-jasmine
MIT License
95 stars 58 forks source link

False positive with jasmine/no-suite-callback-args when using typescript and specifying a "this" type in a function definition. #343

Open IDarlingAS opened 2 years ago

IDarlingAS commented 2 years ago

Describe the bug

When writing code using typescript, you can specify a type for "this" as part of a function declaration. This isn't an actual parameter to the function, but is defined like one.

(See https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function for details)

You can use this in a jasmine test suite to specify things like spies, etc that are assigned to "this" in a type safe way.

When using eslint-plugin-jasmine with code written using this style, no-suite-callback-args is raised as a false-positive.

To Reproduce

Have a test.spec.ts file like the following. You can see the outer "describe" block has "this" specified as a type.

interface IService {
    call(param: string): void;
}

class Component {
    constructor(private service: IService) {
    }

    run() {
        this.service.call('Test');
    }
}

interface TestSuiteContext {
    spyService: jasmine.SpyObj<IService>;
}

describe('Example Test', function(this: TestSuiteContext) {
    beforeEach(() => {
        this.spyService = jasmine.createSpyObj('service', ['call']);
    });

    it('must call the service when you run the component', ()=> {
        const component = new Component(this.spyService);
        component.run();

        expect(this.spyService.call).toHaveBeenCalledWith('Test');
    });
});

Run eslint, and see the following error raised:

C:\......\test.spec.ts
  18:1  error  Unexpected argument in suite's callback  jasmine/no-suite-callback-args

Expected behaviour

No linting issue raised with jasmine/no-suite-callback-args raised for a "this" specification.

Context

IDarlingAS commented 2 years ago

The fix in my fork above requires tests - I've tried to add them, but it requires switching the tests to use the typescript parser, etc, which was more time than I have available at present.