Missing return type on function in Jest / eslint

Viewed 2591

I have this Jest in my project

const action = async () => {
                await hotelService.getByIdAsync(Identifier);
};

await expect(action()).rejects.toThrowError(FunctionalError);

but I have this eslint error

 92:28  error  Missing return type on function  @typescript-eslint/explicit-function-return-type
2 Answers

You need to explicitly specify the return type of the function. As function is async, it returns a Promise wrapped around the data returned by hotelService.getByIdAsync(Identifier)

const action = async (): Promise</*type of data wrapped in promise*/> => {
   return await hotelService.getByIdAsync(Identifier);
};

The action function does not specify a return type, hence the error. In order to get rid of the linting error, you need to set the return type to Promise<void> as the function is async and thus just returns a resolved promise without an actual value:

const action = async () : Promise<void> => {
    await hotelService.getByIdAsync(Identifier);
};
Related