I'm creating a unit test for nodejs project.
Here, I ran across the following error.
Error Message:
error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'. const result = convertTimeStringToNumber(input);
I need to use 'input'(string) as an argument for convertTimeStringToNumber to get 'result'(number), so that i could compare result(number) with expectedResult(number).
I wanted to iterate over an array using test.each so that I can test many data pairs at once.
Please let me know how I could achieve this.
Here's my code and test code.
timeUil.ts
export function convertTimeStringToNumber(timeString) {
return [timeString]
.map(str => str.replace(':', ''))
.map(parseInt)[0]
}
test.spec.ts
const oldtimes = [['9:30', 930], ['11:00', 1100], ['14:34', 1434], ['19:45', 1945]];
describe("convertTimeStringToNumber", () => {
test.each(oldtimes)(
"given %s, convert to %i", (input, expectedResult) => {
const result = convertTimeStringToNumber(input);
expect(result).toBe(expectedResult);
});
});
Aucun commentaire:
Enregistrer un commentaire