Skip to content

Commit d754f5a

Browse files
committed
added a new way of catching error by throwing and testing for a thrown error
1 parent f04aba1 commit d754f5a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Sprint-3/2-practice-tdd/repeat.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ function repeat(str, count) {
44
if (count === 0) {
55
return newStr;
66
} else if (count === 1) {
7-
return (newStr += str);
7+
return str;
88
} else if (count > 1) {
99
for (let i = 0; i < count; i++) {
1010
newStr += str;
1111
}
1212
return newStr;
1313
} else {
14-
return "Error invalid count used, please use integers from 0 upwards.";
14+
throw new Error(
15+
"Error invalid count used, please use integers from 0 upwards."
16+
);
17+
//return "Error invalid count used, please use integers from 0 upwards.";
1518
}
19+
return true;
1620
}
1721

1822
module.exports = repeat;

Sprint-3/2-practice-tdd/repeat.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ test("should return an empty string", () => {
4545
test("should throw an error for invalid count", () => {
4646
const str = "hello";
4747
const count = -4;
48-
const repeatedStr = repeat(str, count);
49-
expect(repeatedStr).toEqual(
48+
function repeatedStr() {
49+
repeat(str, count);
50+
}
51+
expect(repeatedStr).toThrow(
5052
"Error invalid count used, please use integers from 0 upwards."
5153
);
5254
});

0 commit comments

Comments
 (0)