Skip to content

Commit 1240140

Browse files
authored
test: Rename custom matcher and add motivating docs (#1020)
* test: Rename toMatchTaskWithDescription() to toMatchTaskFromLine(), to better explain its behaviour. * test: Rename description parameter to line * docs: Add motivating docs for CustomMatchersForFilters.ts
1 parent 11744ff commit 1240140

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

tests/CustomMatchers/CustomMatchersForFilters.ts

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,73 @@ import type { FilterOrErrorMessage } from '../../src/Query/Filter/Filter';
22
import { fromLine } from '../TestHelpers';
33
import { TaskBuilder } from '../TestingTools/TaskBuilder';
44

5-
/* Example usage (shown for toMatchTaskWithDescription(), but other matchers are available.
5+
/**
6+
@summary
7+
This file contains Jest custom matchers, for idiomatic testing of filtering
8+
via Field classes.
69
7-
import { toMatchTaskWithDescription } from '<relative-path>/CustomMatchersForFilters';
10+
@description
11+
These matchers are a more idiomatic way of testing custom objects via
12+
the Jest test framework than the helper functions in tests/TestingTools/
13+
and various testing helpers in individual x.test.ts files.
14+
<br>
815
9-
expect.extend({
10-
toMatchTaskWithDescription,
16+
When they fail, they show the line number at the call site, rather
17+
than some line buried down in the helper function, in other words, a much
18+
more useful call stack/traceback.
19+
<br>
20+
21+
They can also generate much more informative error messages describing
22+
the failure.
23+
<br>
24+
25+
Example usage (shown for {@link toMatchTaskFromLine}, but other matchers are available:
26+
<br>
27+
28+
@example
29+
30+
// Setup:
31+
import { toMatchTaskFromLine } from '<relative-path>/CustomMatchersForFilters';
32+
33+
expect.extend({
34+
toMatchTaskFromLine,
1135
});
1236
37+
// Inside it() and describe() blocks:
38+
it('works negating regexes', () => {
39+
// Arrange
40+
const filter = new DescriptionField().createFilterOrErrorMessage(
41+
'description regex does not match /^task/',
42+
);
43+
44+
// Assert
45+
expect(filter).toMatchTaskFromLine(
46+
'- [ ] this does not start with the pattern',
47+
);
48+
expect(filter).not.toMatchTaskFromLine(
49+
'- [ ] task does start with the pattern',
50+
);
51+
});
52+
1353
*/
1454

1555
declare global {
1656
namespace jest {
1757
interface Matchers<R> {
1858
toBeValid(): R;
19-
toMatchTaskWithDescription(description: string): R;
59+
toMatchTaskFromLine(line: string): R;
2060
toMatchTaskWithPath(path: string): R;
2161
}
2262

2363
interface Expect {
2464
toBeValid(): any;
25-
toMatchTaskWithDescription(description: string): any;
65+
toMatchTaskFromLine(line: string): any;
2666
toMatchTaskWithPath(path: string): any;
2767
}
2868

2969
interface InverseAsymmetricMatchers {
3070
toBeValid(): any;
31-
toMatchTaskWithDescription(description: string): any;
71+
toMatchTaskFromLine(line: string): any;
3272
toMatchTaskWithPath(path: string): any;
3373
}
3474
}
@@ -57,24 +97,24 @@ export function toBeValid(filter: FilterOrErrorMessage) {
5797
};
5898
}
5999

60-
export function toMatchTaskWithDescription(
100+
export function toMatchTaskFromLine(
61101
filter: FilterOrErrorMessage,
62-
description: string,
102+
line: string,
63103
) {
64104
const task = fromLine({
65-
line: description,
105+
line: line,
66106
});
67107

68108
const matches = filter.filter!(task);
69109
if (!matches) {
70110
return {
71-
message: () => `unexpected failure to match task: ${description}`,
111+
message: () => `unexpected failure to match task: ${line}`,
72112
pass: false,
73113
};
74114
}
75115

76116
return {
77-
message: () => `filter should not have matched task: ${description}`,
117+
message: () => `filter should not have matched task: ${line}`,
78118
pass: true,
79119
};
80120
}

tests/Query/Filter/DescriptionField.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getSettings, updateSettings } from '../../../src/config/Settings';
33
import { testTaskFilter } from '../../TestingTools/FilterTestHelpers';
44
import { fromLine } from '../../TestHelpers';
55
import type { FilterOrErrorMessage } from '../../../src/Query/Filter/Filter';
6-
import { toMatchTaskWithDescription } from '../../CustomMatchers/CustomMatchersForFilters';
6+
import { toMatchTaskFromLine } from '../../CustomMatchers/CustomMatchersForFilters';
77

88
function testDescriptionFilter(
99
filter: FilterOrErrorMessage,
@@ -17,7 +17,7 @@ function testDescriptionFilter(
1717
}
1818

1919
expect.extend({
20-
toMatchTaskWithDescription,
20+
toMatchTaskFromLine,
2121
});
2222

2323
describe('description', () => {
@@ -76,10 +76,10 @@ describe('description', () => {
7676
);
7777

7878
// Assert
79-
expect(filter).not.toMatchTaskWithDescription(
79+
expect(filter).not.toMatchTaskFromLine(
8080
'- [ ] this does not start with the pattern',
8181
);
82-
expect(filter).toMatchTaskWithDescription(
82+
expect(filter).toMatchTaskFromLine(
8383
'- [ ] task does start with the pattern',
8484
);
8585
});
@@ -91,10 +91,10 @@ describe('description', () => {
9191
);
9292

9393
// Assert
94-
expect(filter).toMatchTaskWithDescription(
94+
expect(filter).toMatchTaskFromLine(
9595
'- [ ] this does not start with the pattern',
9696
);
97-
expect(filter).not.toMatchTaskWithDescription(
97+
expect(filter).not.toMatchTaskFromLine(
9898
'- [ ] task does start with the pattern',
9999
);
100100
});

0 commit comments

Comments
 (0)