Как смоделировать возвращаемое значение с помощью Jest в Typescript без преобразования as any
?
У меня есть этот фрагмент кода, для которого я пишу тест для
const collection = await getUserCollection()//returns a mongo collection
const cursor = await collection.find<IUser>(query).limit(pageSize)
if(cursor == undefined){
CCLogger.default.error('Failed to lookup users')
res.status(500).send()
return
}
In мой тестовый код, я издеваюсь над ним, чтобы курсор не был определен
const collection = await dbCollection.getUserCollection()
jest.spyOn(collection, 'find').mockImplementationOnce(() => { return {limit: () => undefined} as any })
jest.spyOn(dbCollection, 'getUserCollection').mockImplementationOnce(async () => { return collection })
Моя проблема связана с return {limit: () => undefined} as any
. Поскольку он не возвращает курсор, если я не приведу его, я получу ошибку с eslint. Есть ли другой способ сделать это без трансляции?
Ошибка eslint: Argument of type '() => { limit: () => any; }' is not assignable to parameter of type '(query: FilterQuery<unknown>, options?: FindOneOptions) => Cursor<unknown>'.
Type '{ limit: () => any; }' is missing the following properties from type 'Cursor<unknown>': [Symbol.asyncIterator], sortValue, timeout, readPreference, and 67 more.ts(2345)