@@ -2,33 +2,73 @@ import type { FilterOrErrorMessage } from '../../src/Query/Filter/Filter';
2
2
import { fromLine } from '../TestHelpers' ;
3
3
import { TaskBuilder } from '../TestingTools/TaskBuilder' ;
4
4
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.
6
9
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>
8
15
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,
11
35
});
12
36
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
+
13
53
*/
14
54
15
55
declare global {
16
56
namespace jest {
17
57
interface Matchers < R > {
18
58
toBeValid ( ) : R ;
19
- toMatchTaskWithDescription ( description : string ) : R ;
59
+ toMatchTaskFromLine ( line : string ) : R ;
20
60
toMatchTaskWithPath ( path : string ) : R ;
21
61
}
22
62
23
63
interface Expect {
24
64
toBeValid ( ) : any ;
25
- toMatchTaskWithDescription ( description : string ) : any ;
65
+ toMatchTaskFromLine ( line : string ) : any ;
26
66
toMatchTaskWithPath ( path : string ) : any ;
27
67
}
28
68
29
69
interface InverseAsymmetricMatchers {
30
70
toBeValid ( ) : any ;
31
- toMatchTaskWithDescription ( description : string ) : any ;
71
+ toMatchTaskFromLine ( line : string ) : any ;
32
72
toMatchTaskWithPath ( path : string ) : any ;
33
73
}
34
74
}
@@ -57,24 +97,24 @@ export function toBeValid(filter: FilterOrErrorMessage) {
57
97
} ;
58
98
}
59
99
60
- export function toMatchTaskWithDescription (
100
+ export function toMatchTaskFromLine (
61
101
filter : FilterOrErrorMessage ,
62
- description : string ,
102
+ line : string ,
63
103
) {
64
104
const task = fromLine ( {
65
- line : description ,
105
+ line : line ,
66
106
} ) ;
67
107
68
108
const matches = filter . filter ! ( task ) ;
69
109
if ( ! matches ) {
70
110
return {
71
- message : ( ) => `unexpected failure to match task: ${ description } ` ,
111
+ message : ( ) => `unexpected failure to match task: ${ line } ` ,
72
112
pass : false ,
73
113
} ;
74
114
}
75
115
76
116
return {
77
- message : ( ) => `filter should not have matched task: ${ description } ` ,
117
+ message : ( ) => `filter should not have matched task: ${ line } ` ,
78
118
pass : true ,
79
119
} ;
80
120
}
0 commit comments