Skip to content

Commit 2920482

Browse files
authored
test: Move custom Jest matchers to separate file (#1019)
* test: Move toMatchTaskWithDescription() to separate file To allow for re-use. * test: Add comment saying how to use extracted code * test: Move toMatchTaskWithPath() & toBeValid() to separate file * test: Combine 2 'namespace jest' blocks in to 1 * test: Make function order in file consistent The functions are now implemented in the same order that they are used in the 'namespace jest' block at the top of the file. * test: Clarify a comment Now that custom matchers are available. * test: Move custom matchers to tests/CustomMatchers/
1 parent ba9f6e6 commit 2920482

File tree

3 files changed

+106
-101
lines changed

3 files changed

+106
-101
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import type { FilterOrErrorMessage } from '../../src/Query/Filter/Filter';
2+
import { fromLine } from '../TestHelpers';
3+
import { TaskBuilder } from '../TestingTools/TaskBuilder';
4+
5+
/* Example usage (shown for toMatchTaskWithDescription(), but other matchers are available.
6+
7+
import { toMatchTaskWithDescription } from '<relative-path>/CustomMatchersForFilters';
8+
9+
expect.extend({
10+
toMatchTaskWithDescription,
11+
});
12+
13+
*/
14+
15+
declare global {
16+
namespace jest {
17+
interface Matchers<R> {
18+
toBeValid(): R;
19+
toMatchTaskWithDescription(description: string): R;
20+
toMatchTaskWithPath(path: string): R;
21+
}
22+
23+
interface Expect {
24+
toBeValid(): any;
25+
toMatchTaskWithDescription(description: string): any;
26+
toMatchTaskWithPath(path: string): any;
27+
}
28+
29+
interface InverseAsymmetricMatchers {
30+
toBeValid(): any;
31+
toMatchTaskWithDescription(description: string): any;
32+
toMatchTaskWithPath(path: string): any;
33+
}
34+
}
35+
}
36+
37+
export function toBeValid(filter: FilterOrErrorMessage) {
38+
if (filter.filter === undefined) {
39+
return {
40+
message: () =>
41+
'unexpected null filter: check your instruction matches your filter class',
42+
pass: false,
43+
};
44+
}
45+
46+
if (filter.error !== undefined) {
47+
return {
48+
message: () =>
49+
'unexpected error message in filter: check your instruction matches your filter class',
50+
pass: false,
51+
};
52+
}
53+
54+
return {
55+
message: () => 'filter is unexpectedly valid',
56+
pass: true,
57+
};
58+
}
59+
60+
export function toMatchTaskWithDescription(
61+
filter: FilterOrErrorMessage,
62+
description: string,
63+
) {
64+
const task = fromLine({
65+
line: description,
66+
});
67+
68+
const matches = filter.filter!(task);
69+
if (!matches) {
70+
return {
71+
message: () => `unexpected failure to match task: ${description}`,
72+
pass: false,
73+
};
74+
}
75+
76+
return {
77+
message: () => `filter should not have matched task: ${description}`,
78+
pass: true,
79+
};
80+
}
81+
82+
export function toMatchTaskWithPath(
83+
filter: FilterOrErrorMessage,
84+
path: string,
85+
) {
86+
const builder = new TaskBuilder();
87+
const task = builder.path(path).build();
88+
89+
const matches = filter.filter!(task);
90+
if (!matches) {
91+
return {
92+
message: () => `unexpected failure to match task: ${path}`,
93+
pass: false,
94+
};
95+
}
96+
97+
return {
98+
message: () => `filter should not have matched task: ${path}`,
99+
pass: true,
100+
};
101+
}

tests/Query/Filter/DescriptionField.test.ts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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';
67

78
function testDescriptionFilter(
89
filter: FilterOrErrorMessage,
@@ -15,44 +16,6 @@ function testDescriptionFilter(
1516
testTaskFilter(filter, task, expected);
1617
}
1718

18-
declare global {
19-
namespace jest {
20-
interface Matchers<R> {
21-
toMatchTaskWithDescription(description: string): R;
22-
}
23-
24-
interface Expect {
25-
toMatchTaskWithDescription(description: string): any;
26-
}
27-
28-
interface InverseAsymmetricMatchers {
29-
toMatchTaskWithDescription(description: string): any;
30-
}
31-
}
32-
}
33-
34-
export function toMatchTaskWithDescription(
35-
filter: FilterOrErrorMessage,
36-
description: string,
37-
) {
38-
const task = fromLine({
39-
line: description,
40-
});
41-
42-
const matches = filter.filter!(task);
43-
if (!matches) {
44-
return {
45-
message: () => `unexpected failure to match task: ${description}`,
46-
pass: false,
47-
};
48-
}
49-
50-
return {
51-
message: () => `filter should not have matched task: ${description}`,
52-
pass: true,
53-
};
54-
}
55-
5619
expect.extend({
5720
toMatchTaskWithDescription,
5821
});

tests/Query/Filter/PathField.test.ts

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { PathField } from '../../../src/Query/Filter/PathField';
22
import type { FilterOrErrorMessage } from '../../../src/Query/Filter/Filter';
33
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
44
import { testFilter } from '../../TestingTools/FilterTestHelpers';
5+
import {
6+
toBeValid,
7+
toMatchTaskWithPath,
8+
} from '../../CustomMatchers/CustomMatchersForFilters';
59

610
function testTaskFilterForTaskWithPath(
711
filter: FilterOrErrorMessage,
@@ -12,69 +16,6 @@ function testTaskFilterForTaskWithPath(
1216
testFilter(filter, builder.path(path), expected);
1317
}
1418

15-
declare global {
16-
namespace jest {
17-
interface Matchers<R> {
18-
toMatchTaskWithPath(path: string): R;
19-
toBeValid(): R;
20-
}
21-
22-
interface Expect {
23-
toMatchTaskWithPath(path: string): any;
24-
toBeValid(): any;
25-
}
26-
27-
interface InverseAsymmetricMatchers {
28-
toMatchTaskWithPath(path: string): any;
29-
toBeValid(): any;
30-
}
31-
}
32-
}
33-
34-
export function toBeValid(filter: FilterOrErrorMessage) {
35-
if (filter.filter === undefined) {
36-
return {
37-
message: () =>
38-
'unexpected null filter: check your instruction matches your filter class',
39-
pass: false,
40-
};
41-
}
42-
43-
if (filter.error !== undefined) {
44-
return {
45-
message: () =>
46-
'unexpected error message in filter: check your instruction matches your filter class',
47-
pass: false,
48-
};
49-
}
50-
51-
return {
52-
message: () => 'filter is unexpectedly valid',
53-
pass: true,
54-
};
55-
}
56-
57-
export function toMatchTaskWithPath(
58-
filter: FilterOrErrorMessage,
59-
path: string,
60-
) {
61-
const builder = new TaskBuilder();
62-
const task = builder.path(path).build();
63-
64-
const matches = filter.filter!(task);
65-
if (!matches) {
66-
return {
67-
message: () => `unexpected failure to match task: ${path}`,
68-
pass: false,
69-
};
70-
}
71-
72-
return {
73-
message: () => `filter should not have matched task: ${path}`,
74-
pass: true,
75-
};
76-
}
77-
7819
expect.extend({
7920
toMatchTaskWithPath,
8021
});

0 commit comments

Comments
 (0)