@@ -3,10 +3,13 @@ import { getDocsUrl, ALL_QUERIES_METHODS } from '../utils';
3
3
import { isMemberExpression } from '../node-utils' ;
4
4
5
5
export const RULE_NAME = 'prefer-explicit-assert' ;
6
- export type MessageIds = 'preferExplicitAssert' ;
6
+ export type MessageIds =
7
+ | 'preferExplicitAssert'
8
+ | 'preferExplicitAssertAssertion' ;
7
9
type Options = [
8
10
{
9
- customQueryNames : string [ ] ;
11
+ assertion ?: string ;
12
+ customQueryNames ?: string [ ] ;
10
13
}
11
14
] ;
12
15
@@ -34,12 +37,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
34
37
messages : {
35
38
preferExplicitAssert :
36
39
'Wrap stand-alone `getBy*` query with `expect` function for better explicit assertion' ,
40
+ preferExplicitAssertAssertion :
41
+ '`getBy*` queries must be asserted with `{{assertion}}`' ,
37
42
} ,
38
43
fixable : null ,
39
44
schema : [
40
45
{
41
46
type : 'object' ,
47
+ additionalProperties : false ,
42
48
properties : {
49
+ assertion : {
50
+ type : 'string' ,
51
+ } ,
43
52
customQueryNames : {
44
53
type : 'array' ,
45
54
} ,
@@ -54,7 +63,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
54
63
] ,
55
64
56
65
create : function ( context , [ options ] ) {
57
- const { customQueryNames } = options ;
66
+ const { customQueryNames, assertion } = options ;
58
67
const getQueryCalls : TSESTree . Identifier [ ] = [ ] ;
59
68
60
69
return {
@@ -74,6 +83,22 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
74
83
node : queryCall ,
75
84
messageId : 'preferExplicitAssert' ,
76
85
} ) ;
86
+ } else if ( assertion ) {
87
+ const expectation = node . parent . parent . parent ;
88
+
89
+ if (
90
+ expectation . type === 'MemberExpression' &&
91
+ expectation . property . type === 'Identifier' &&
92
+ expectation . property . name !== assertion
93
+ ) {
94
+ context . report ( {
95
+ node : expectation . property ,
96
+ messageId : 'preferExplicitAssertAssertion' ,
97
+ data : {
98
+ assertion,
99
+ } ,
100
+ } ) ;
101
+ }
77
102
}
78
103
} ) ;
79
104
} ,
0 commit comments