@@ -3,10 +3,13 @@ import { getDocsUrl, ALL_QUERIES_METHODS } from '../utils';
33import { isMemberExpression } from '../node-utils' ;
44
55export const RULE_NAME = 'prefer-explicit-assert' ;
6- export type MessageIds = 'preferExplicitAssert' ;
6+ export type MessageIds =
7+ | 'preferExplicitAssert'
8+ | 'preferExplicitAssertAssertion' ;
79type Options = [
810 {
9- customQueryNames : string [ ] ;
11+ assertion ?: string ;
12+ customQueryNames ?: string [ ] ;
1013 }
1114] ;
1215
@@ -34,12 +37,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
3437 messages : {
3538 preferExplicitAssert :
3639 'Wrap stand-alone `getBy*` query with `expect` function for better explicit assertion' ,
40+ preferExplicitAssertAssertion :
41+ '`getBy*` queries must be asserted with `{{assertion}}`' ,
3742 } ,
3843 fixable : null ,
3944 schema : [
4045 {
4146 type : 'object' ,
47+ additionalProperties : false ,
4248 properties : {
49+ assertion : {
50+ type : 'string' ,
51+ } ,
4352 customQueryNames : {
4453 type : 'array' ,
4554 } ,
@@ -54,7 +63,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5463 ] ,
5564
5665 create : function ( context , [ options ] ) {
57- const { customQueryNames } = options ;
66+ const { customQueryNames, assertion } = options ;
5867 const getQueryCalls : TSESTree . Identifier [ ] = [ ] ;
5968
6069 return {
@@ -74,6 +83,22 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
7483 node : queryCall ,
7584 messageId : 'preferExplicitAssert' ,
7685 } ) ;
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+ }
77102 }
78103 } ) ;
79104 } ,
0 commit comments