Skip to content

Commit

Permalink
test: Add jest custom matcher toEqualMoment() (#1980)
Browse files Browse the repository at this point in the history
* test: Add toBeSameMoment() custom matcher

* test: Rework tests to use toBeSameMoment() custom matcher

To give meaningful output upon test failure

* test: Rework tests to use toBeSameMoment() custom matcher

To give meaningful output upon test failure

* test: .not.toBeSameMoment() now gives meaningful message

* test: Extract variables in toBeSameMoment() before adding support for received being null

* test: toBeSameMoment() is now useful if received "date" is null

* test: Simplify moment test, now that toBeSameMoment() handles nulls

* test: Rename toBeSameMoment() to toEqualMoment()
  • Loading branch information
claremacrae authored May 28, 2023
1 parent f3e4760 commit 3156647
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
26 changes: 26 additions & 0 deletions tests/CustomMatchers/CustomMatchersForDates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type moment from 'moment';
import { diff } from 'jest-diff';

declare global {
namespace jest {
interface Matchers<R> {
toEqualMoment(expected: moment.Moment): CustomMatcherResult;
}
}
}

// Based on https://stackoverflow.com/a/60229956/104370
export function toEqualMoment(received: moment.Moment | null, expected: moment.Moment): jest.CustomMatcherResult {
const pass: boolean = expected.isSame(received);
const expectedAsText = expected.toISOString();
const receivedAsText = received ? received.toISOString() : 'null';
const message: () => string = () =>
pass
? `Received moment should not be ${expectedAsText}`
: `Received moment is not the same as expected: ${diff(expectedAsText, receivedAsText)}`;

return {
message,
pass,
};
}
8 changes: 8 additions & 0 deletions tests/CustomMatchers/jest.custom_matchers.setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// ---------------------------------------------------------------------
// CustomMatchersForDates
// ---------------------------------------------------------------------
import { toEqualMoment } from './CustomMatchersForDates';
expect.extend({
toEqualMoment,
});

// ---------------------------------------------------------------------
// CustomMatchersForFilters
// ---------------------------------------------------------------------
Expand Down
18 changes: 7 additions & 11 deletions tests/DateFallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ describe('extract date from filename', () => {

// Assert
if (testCase.expectedDate !== null) {
expect(parsedDate).not.toBeNull();
expect(parsedDate!.isSame(date(testCase.expectedDate))).toStrictEqual(true);
expect(parsedDate).toEqualMoment(moment(testCase.expectedDate));
} else {
expect(parsedDate).toBeNull();
}
Expand Down Expand Up @@ -208,8 +207,7 @@ describe('parse task with date fallback', () => {
const task = constructTaskFromLine(line, '2022-10-22');

// Assert
expect(task!.scheduledDate).not.toBeNull();
expect(task!.scheduledDate!.isSame(date('2022-10-22'))).toStrictEqual(true);
expect(task!.scheduledDate).toEqualMoment(moment('2022-10-22'));
expect(task!.scheduledDateIsInferred).toBe(true);
});

Expand Down Expand Up @@ -245,8 +243,7 @@ describe('parse task with date fallback', () => {
const task = constructTaskFromLine(line, '2022-10-22');

// Assert
expect(task!.scheduledDate).not.toBeNull();
expect(task!.scheduledDate!.isSame(date('2022-11-22'))).toStrictEqual(true);
expect(task!.scheduledDate).toEqualMoment(moment('2022-11-22'));
expect(task!.scheduledDateIsInferred).toBe(false);
});

Expand All @@ -258,8 +255,7 @@ describe('parse task with date fallback', () => {
const task = constructTaskFromLine(line, '2022-10-22');

// Assert
expect(task!.scheduledDate).not.toBeNull();
expect(task!.scheduledDate!.isSame(date('2022-10-22'))).toStrictEqual(true);
expect(task!.scheduledDate).toEqualMoment(moment('2022-10-22'));
expect(task!.scheduledDateIsInferred).toBe(true);
});

Expand All @@ -285,8 +281,7 @@ describe('parse task with date fallback', () => {

// Assert
expect(toggled.scheduledDateIsInferred).toBe(true);
expect(toggled.scheduledDate).not.toBeNull();
expect(toggled.scheduledDate!.isSame(date('2022-10-22'))).toStrictEqual(true);
expect(toggled.scheduledDate).toEqualMoment(moment('2022-10-22'));
expect(toggled.toFileLineString()).toBe('- [ ] this is a task');
});
});
Expand Down Expand Up @@ -376,7 +371,8 @@ describe('update fallback date when path is changed', () => {
if (expectedScheduledDate === null) {
expect(updatedTask.scheduledDate).toBeNull();
} else {
expect(updatedTask.scheduledDate!.isSame(expectedScheduledDate)).toStrictEqual(true);
expect(updatedTask.scheduledDate).toEqualMoment(expectedScheduledDate);
true;
}

expect(updatedTask.scheduledDateIsInferred).toBe(expectedIsInferred);
Expand Down
16 changes: 8 additions & 8 deletions tests/Recurrence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Recurrence', () => {
// Assert
expect(next!.startDate).toBeNull();
expect(next!.scheduledDate).toBeNull();
expect(next!.dueDate!.isSame(moment('2022-02-28'))).toStrictEqual(true);
expect(next!.dueDate).toEqualMoment(moment('2022-02-28'));
});

it('creates a recurrence 3 months in', () => {
Expand All @@ -62,7 +62,7 @@ describe('Recurrence', () => {
// Assert
expect(next!.startDate).toBeNull();
expect(next!.scheduledDate).toBeNull();
expect(next!.dueDate!.isSame(moment('2022-04-30'))).toStrictEqual(true);
expect(next!.dueDate).toEqualMoment(moment('2022-04-30'));
});

it('creates a recurrence the next month, even across years', () => {
Expand All @@ -80,7 +80,7 @@ describe('Recurrence', () => {
// Assert
expect(next!.startDate).toBeNull();
expect(next!.scheduledDate).toBeNull();
expect(next!.dueDate!.isSame(moment('2024-02-29'))).toStrictEqual(true);
expect(next!.dueDate).toEqualMoment(moment('2024-02-29'));
});

it('creates a recurrence in 2 years, even on Feb 29th', () => {
Expand All @@ -98,7 +98,7 @@ describe('Recurrence', () => {
// Assert
expect(next!.startDate).toBeNull();
expect(next!.scheduledDate).toBeNull();
expect(next!.dueDate!.isSame(moment('2026-02-28'))).toStrictEqual(true);
expect(next!.dueDate).toEqualMoment(moment('2026-02-28'));
});

it('creates a recurrence in 11 months, even on March 31', () => {
Expand All @@ -116,7 +116,7 @@ describe('Recurrence', () => {
// Assert
expect(next!.startDate).toBeNull();
expect(next!.scheduledDate).toBeNull();
expect(next!.dueDate!.isSame(moment('2021-02-28'))).toStrictEqual(true);
expect(next!.dueDate).toEqualMoment(moment('2021-02-28'));
});

it('creates a recurrence in 13 months, even on Jan 31', () => {
Expand All @@ -134,7 +134,7 @@ describe('Recurrence', () => {
// Assert
expect(next!.startDate).toBeNull();
expect(next!.scheduledDate).toBeNull();
expect(next!.dueDate!.isSame(moment('2021-02-28'))).toStrictEqual(true);
expect(next!.dueDate).toEqualMoment(moment('2021-02-28'));
});
});

Expand Down Expand Up @@ -178,8 +178,8 @@ describe('Recurrence - with invalid dates in tasks', () => {
// The original scheduled date was an illegal/invalid date.
// So it is simply given the new value of the same date as the reference date,
// which here is the due date.
expect(next!.scheduledDate!.isSame(moment('2022-02-28'))).toStrictEqual(true); // date was invalid, so is given the value of highst oriority supplied date
expect(next!.dueDate!.isSame(moment('2022-02-28'))).toStrictEqual(true);
expect(next!.scheduledDate).toEqualMoment(moment('2022-02-28')); // date was invalid, so is given the value of highest priority supplied date
expect(next!.dueDate).toEqualMoment(moment('2022-02-28'));
});
});

Expand Down
36 changes: 13 additions & 23 deletions tests/Task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ describe('parsing', () => {
expect(task!.listMarker).toEqual('-');
expect(task!.description).toEqual('this is a done task');
expect(task!.status).toStrictEqual(Status.DONE);
expect(task!.createdDate).not.toBeNull();
expect(task!.createdDate!.isSame(moment('2023-03-07', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.dueDate).not.toBeNull();
expect(task!.dueDate!.isSame(moment('2021-09-12', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.doneDate).not.toBeNull();
expect(task!.doneDate!.isSame(moment('2021-06-20', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.createdDate).toEqualMoment(moment('2023-03-07'));
expect(task!.dueDate).toEqualMoment(moment('2021-09-12'));
expect(task!.doneDate).toEqualMoment(moment('2021-06-20'));
expect(task!.originalMarkdown).toStrictEqual(line);
expect(task!.lineNumber).toEqual(0);
});
Expand Down Expand Up @@ -132,10 +129,8 @@ describe('parsing', () => {
expect(task).not.toBeNull();
expect(task!.description).toEqual('this is a ✅ done task');
expect(task!.status).toStrictEqual(Status.DONE);
expect(task!.dueDate).not.toBeNull();
expect(task!.dueDate!.isSame(moment('2021-09-12', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.doneDate).not.toBeNull();
expect(task!.doneDate!.isSame(moment('2021-06-20', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.dueDate).toEqualMoment(moment('2021-09-12'));
expect(task!.doneDate).toEqualMoment(moment('2021-06-20'));
});
// end-snippet

Expand All @@ -152,10 +147,8 @@ describe('parsing', () => {
expect(task).not.toBeNull();
expect(task!.description).toEqual('this is a ✅ done task');
expect(task!.status).toStrictEqual(Status.DONE);
expect(task!.dueDate).not.toBeNull();
expect(task!.dueDate!.isSame(moment('2021-09-12', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.doneDate).not.toBeNull();
expect(task!.doneDate!.isSame(moment('2021-06-20', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.dueDate).toEqualMoment(moment('2021-09-12'));
expect(task!.doneDate).toEqualMoment(moment('2021-06-20'));
expect(task!.blockLink).toEqual(' ^my-precious');
});

Expand All @@ -172,8 +165,7 @@ describe('parsing', () => {
expect(task).not.toBeNull();
expect(task!.description).toEqual('this is a task due #inside_tag #some/tags_with_underscore');
expect(task!.tags).toEqual(['#inside_tag', '#some/tags_with_underscore']);
expect(task!.dueDate).not.toBeNull();
expect(task!.dueDate!.isSame(moment('2021-09-12', 'YYYY-MM-DD'))).toEqual(true);
expect(task!.dueDate).toEqualMoment(moment('2021-09-12'));
expect(task!.priority).toEqual(Priority.High);
});

Expand All @@ -190,10 +182,10 @@ describe('parsing', () => {
// Assert
expect(task).not.toBeNull();
expect(task!.description).toEqual('Wobble #tag1 #tag2 #tag3 #tag4 #tag5 #tag6 #tag7 #tag8 #tag9 #tag10');
expect(task!.dueDate!.isSame(moment('2022-07-02', 'YYYY-MM-DD'))).toEqual(true);
expect(task!.doneDate!.isSame(moment('2022-07-02', 'YYYY-MM-DD'))).toEqual(true);
expect(task!.startDate!.isSame(moment('2022-07-02', 'YYYY-MM-DD'))).toEqual(true);
expect(task!.scheduledDate!.isSame(moment('2022-07-02', 'YYYY-MM-DD'))).toEqual(true);
expect(task!.dueDate).toEqualMoment(moment('2022-07-02'));
expect(task!.doneDate).toEqualMoment(moment('2022-07-02'));
expect(task!.startDate).toEqualMoment(moment('2022-07-02'));
expect(task!.scheduledDate).toEqualMoment(moment('2022-07-02'));
expect(task!.priority).toEqual(Priority.High);
expect(task!.tags).toStrictEqual([
'#tag1',
Expand Down Expand Up @@ -594,7 +586,6 @@ describe('toggle done', () => {
// Assert
expect(toggled).not.toBeNull();
expect(toggled!.status).toStrictEqual(Status.DONE);
expect(toggled!.doneDate).not.toBeNull();
expect(toggled!.status.symbol).toStrictEqual('x');
expect(toggled!.blockLink).toEqual(' ^my-precious');
});
Expand Down Expand Up @@ -1000,8 +991,7 @@ describe('toggle done', () => {

// Assert
expect(task).not.toBeNull();
expect(task!.dueDate).not.toBeNull();
expect(task!.dueDate!.isSame(moment('2021-09-12', 'YYYY-MM-DD'))).toStrictEqual(true);
expect(task!.dueDate).toEqualMoment(moment('2021-09-12'));

const tasks = task!.toggle();
expect(tasks.length).toEqual(2);
Expand Down

0 comments on commit 3156647

Please sign in to comment.