Instead of repeating test cases with minor adjustments, I have implemented an Array and iterated through it.
However, I am encountering a TS error in test
when passed from the Array instead of as a string testLink
Error:
No overload matches this call.
Overload 1 of 4, '(object: { registerLink: () => void; }, method: "registerLink"): SpyInstance<void, []>', gave the following error.
Argument of type 'string' is not assignable to parameter of type '"registerLink"'.
Overload 2 of 4, '(object: { registerLink: () => void; }, method: never): SpyInstance<never, never>', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'never'.ts(2769)
Component.test.tsx
for (let i = 0; i < testProp.length; i++) {
it(`test default props of ${testProp[i].name}`, () => {
const test = testProp[i].name
jest.spyOn(ComponentDefaultProp, test)
})
}
But when I pass it as a string, no errors occur
for (let i = 0; i < testProp.length; i++) {
it(`test default props of ${testProp[i].name}`, () => {
jest.spyOn(ComponentDefaultProp, 'testLink')
})
}
The array being looped over is:
const testProp = [
{
name: 'testLink',
},
]
What worked but I prefer not to use any
:
Test 1
for (let i = 0; i < testProp.length; i++) {
it(`test default props of ${testProp[i].name}`, () => {
const test: any = testProp[i].name
jest.spyOn(ComponentDefaultProp, test)
})
}
Test 2
for (let i = 0; i < testProp.length; i++) {
it(`test default props of ${testProp[i].name}`, () => {
const test = testProp[i].name
jest.spyOn<any, string>(ComponentDefaultProp, test)
})
}
Screenshot showing spyOn: https://i.sstatic.net/4HDeZ.png
The objective is to utilize an array without encountering TS error and avoiding any
if possible