In my current scenario, I am faced with a challenge where I need to test a function with a specific use of this
. However, typescript poses constraints by either disallowing me from adding the method to the object, or if I define it as any
, then my interface remains unused.
For example, consider the following code snippets:
import getMostPeersTorrentsFromDateRange from './index'
import Db from '../Db'
test('getMostPeersTorrentsFromDateRange', async () => {
const db = Db() as any
db.getMostPeersTorrentsFromDateRange = getMostPeersTorrentsFromDateRange
const torrents = await db.getMostPeersTorrentsFromDateRange({startDate: '2019-03-03', endDate: '2019-03-08', limit: 100})
})
and in another file:
import {Torrent} from '../interfaces'
interface GetMostPeersTorrentsFromDateRange {
(GetMostPeersTorrentsFromDateRangeSettings: {
startDate: string,
endDate: string,
limit: number
}): Promise<Torrent[]>
}
const getMostPeersTorrentsFromDateRange: GetMostPeersTorrentsFromDateRange = async function ({startDate, endDate, limit}) {
return []
}
export default getMostPeersTorrentsFromDateRange
I am seeking a solution that minimizes code repetition. Is there a way to achieve this without defining
getMostPeersTorrentsFromDateRange
twice and without importing/exporting it again?