angular - Jest mockResolvedValue nu funcționează cu array-uri în Typescript
Avem aici o problemă derutantă. De ce Jest mockResolvedValue nu funcționează la simularea unei valori rezolvate de tip array, în timp ce aceeași valoare funcționează în mockImplementation.
export interface Car {
name: string;
key: string;
count?: number;
}
async getCars(): Promise<Car[]> {
const results = await someLongCall() // async call
const c: Car[] = [...results]; // return is of type Car[]
return c;
}
în testele pe care le avem
const c: Car[] = [
{name: 'aaaaa', key: 'aaaaa', count: 10},
{name: 'bbbbb', key: 'bbbbb', count: 20}
];
// this fails
_wSpy1 = jest.spyOn(CarSevice.prototype, 'getCars')
.mockResolvedValue(c); <= Error here ?
// but this works
_wSpy1 = jest.spyOn(CarSevice.prototype, 'getCars')
.mockImplementation(() => Promise.resolve(c));
Eroarea pe care o primim este:
// We get this error:
Error: Argument of type 'Car[]' is not assignable to parameter of
type 'Promise<Car[]> | PromiseLike<Promise<Car[]>>'.
Property 'then' is missing in type 'Car[]' but required in type
'PromiseLike<Promise<Car[]>>'.
[2345] lib.es5.d.ts(1383, 5): 'then' is declared here.
semnătura lui mockResolvedValue este mai jos. Nu poate valoarea să nu fie de tip array?
/**
* Simple sugar function for: `jest.fn().mockImplementation(() =>
Promise.resolve(value));`
*/
mockResolvedValue(value: T | PromiseLike<T>): Mock<Promise<T>, Y>;