Skip to content

Commit c51db1d

Browse files
committed
test(counter): add unit tests for counter function with Jest
1 parent cb03281 commit c51db1d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

__tests__/counter.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const createCounter = require('../easy/counter');
2+
3+
describe('createCounter', () => {
4+
const testCases = [
5+
{ startValue: 5, expected: [5, 6, 7] },
6+
{ startValue: 16, expected: [16, 17, 18] },
7+
{ startValue: 0, expected: [0, 1, 2] },
8+
];
9+
testCases.forEach(({ startValue, expected }) => {
10+
test(`should start from ${startValue} and increment correctly`, () => {
11+
const counter = createCounter(startValue);
12+
expected.forEach((value) => {
13+
expect(counter()).toBe(value);
14+
});
15+
});
16+
});
17+
});

easy/counter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ const createCounter = (n) => {
99
let counter = n;
1010
return () => counter++;
1111
};
12+
13+
module.exports = createCounter;

0 commit comments

Comments
 (0)