Skip to content

Commit cc5c1e6

Browse files
committed
test: Use Jest fake timers for easier resetting of fake date
1 parent 409d8f0 commit cc5c1e6

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

β€Žtests/Task.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,15 @@ describe('toggle done', () => {
10191019
});
10201020

10211021
describe('set correct created date on reccurence task', () => {
1022+
const today = '2023-03-08';
1023+
1024+
beforeEach(() => {
1025+
jest.useFakeTimers();
1026+
jest.setSystemTime(new Date(today));
1027+
});
1028+
10221029
afterEach(() => {
1030+
jest.useRealTimers();
10231031
resetSettings();
10241032
});
10251033

@@ -1066,8 +1074,6 @@ describe('set correct created date on reccurence task', () => {
10661074

10671075
it('set created date with enabled setting', () => {
10681076
// Arrange
1069-
const today = '2023-03-08';
1070-
const todaySpy = jest.spyOn(Date, 'now').mockReturnValue(moment(today).valueOf());
10711077
const line = '- [ ] this is a task πŸ“… 2021-09-12 πŸ” every day';
10721078
updateSettings({ setCreatedDate: true });
10731079

@@ -1085,15 +1091,10 @@ describe('set correct created date on reccurence task', () => {
10851091
const nextTask: Task = tasks[0];
10861092
expect(nextTask.createdDate).not.toBeNull();
10871093
expect(nextTask!.createdDate!.isSame(moment(today, 'YYYY-MM-DD'))).toStrictEqual(true);
1088-
1089-
// cleanup
1090-
todaySpy.mockClear();
10911094
});
10921095

10931096
it('set created date with enabled setting when repeated has created date', () => {
10941097
// Arrange
1095-
const today = '2023-03-08';
1096-
const todaySpy = jest.spyOn(Date, 'now').mockReturnValue(moment(today).valueOf());
10971098
const line = '- [ ] this is a task βž• 2021-09-11 πŸ“… 2021-09-12 πŸ” every day';
10981099
updateSettings({ setCreatedDate: true });
10991100

@@ -1112,9 +1113,6 @@ describe('set correct created date on reccurence task', () => {
11121113
const nextTask: Task = tasks[0];
11131114
expect(nextTask.createdDate).not.toBeNull();
11141115
expect(nextTask!.createdDate!.isSame(moment(today, 'YYYY-MM-DD'))).toStrictEqual(true);
1115-
1116-
// cleanup
1117-
todaySpy.mockClear();
11181116
});
11191117
});
11201118

0 commit comments

Comments
Β (0)