Skip to content

Commit cb50e0a

Browse files
authored
fix false postive issues (#617)
* fix false postive issues * lint
1 parent 339b919 commit cb50e0a

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/rules/valid-expect.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ export default createEslintRule<[
249249
else if (vitestFnCall?.type !== 'expect') {
250250
return
251251
}
252+
else if (vitestFnCall.modifiers.some(mod => mod.type === AST_NODE_TYPES.Identifier && mod.name == 'to')) {
253+
return
254+
}
252255

253256
const { parent: expect } = vitestFnCall.head.node
254257

@@ -373,7 +376,7 @@ export default createEslintRule<[
373376

374377
if (alwaysAwait && returnStatement) {
375378
const sourceCodeText
376-
= context.sourceCode.getText(returnStatement)
379+
= context.sourceCode.getText(returnStatement)
377380
const replacedText = sourceCodeText.replace('return', 'await')
378381

379382
fixes.push(fixer.replaceText(returnStatement, replacedText))

src/utils/parse-vitest-fn-call.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,27 @@ const findModifiersAndMatcher = (
155155

156156
if (modifiers.length === 0) {
157157
// the first modifier can be any of the three modifiers
158-
159158
if (!ModifierName.hasOwnProperty(name))
160159
return 'modifier-unknown'
161160
}
162161
else if (modifiers.length === 1) {
163-
// the second modifier can only be "not"
164-
if (name !== ModifierName.not)
162+
// the second modifier can only either be "not" or "have"
163+
if (name !== ModifierName.not && name != ModifierName.have)
165164
return 'modifier-unknown'
166165

167166
const firstModifier = getAccessorValue(modifiers[0])
168167

169-
// and the first modifier has to be either "resolves" or "rejects"
168+
// and the first modifier has to be either "resolves" or "rejects" or "to"
170169
if (
171170
firstModifier !== ModifierName.resolves
172171
&& firstModifier !== ModifierName.rejects
172+
&& firstModifier !== ModifierName.to
173173
)
174174
return 'modifier-unknown'
175175
}
176176
else {
177177
return 'modifier-unknown'
178178
}
179-
180179
modifiers.push(member)
181180
}
182181

src/utils/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export enum HookName {
2323
}
2424

2525
export enum ModifierName {
26+
to = 'to',
27+
have = 'have',
2628
not = 'not',
2729
rejects = 'rejects',
2830
resolves = 'resolves',

tests/valid-expect.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ ruleTester.run(RULE_NAME, rule, {
8686
'expect(value, "message").toBe(1);',
8787
'expect(value, `message`).toBe(1);',
8888
'const message = "message"; expect(value, `${message}`).toBe(1);',
89+
`it('example', () => {
90+
expect("foo bar").to.include("foo");
91+
});
92+
`,
93+
`it('example', () => {
94+
expect("hey").to.have.property("foo", "bar")
95+
});
96+
`,
8997
{
9098
code: 'expect(1).toBe(2);',
9199
options: [{ maxArgs: 2 }]

0 commit comments

Comments
 (0)