Skip to content

Commit

Permalink
fix false postive issues (#617)
Browse files Browse the repository at this point in the history
* fix false postive issues

* lint
  • Loading branch information
veritem authored Dec 26, 2024
1 parent 339b919 commit cb50e0a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/rules/valid-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ export default createEslintRule<[
else if (vitestFnCall?.type !== 'expect') {
return
}
else if (vitestFnCall.modifiers.some(mod => mod.type === AST_NODE_TYPES.Identifier && mod.name == 'to')) {
return
}

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

Expand Down Expand Up @@ -373,7 +376,7 @@ export default createEslintRule<[

if (alwaysAwait && returnStatement) {
const sourceCodeText
= context.sourceCode.getText(returnStatement)
= context.sourceCode.getText(returnStatement)
const replacedText = sourceCodeText.replace('return', 'await')

fixes.push(fixer.replaceText(returnStatement, replacedText))
Expand Down
9 changes: 4 additions & 5 deletions src/utils/parse-vitest-fn-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,27 @@ const findModifiersAndMatcher = (

if (modifiers.length === 0) {
// the first modifier can be any of the three modifiers

if (!ModifierName.hasOwnProperty(name))
return 'modifier-unknown'
}
else if (modifiers.length === 1) {
// the second modifier can only be "not"
if (name !== ModifierName.not)
// the second modifier can only either be "not" or "have"
if (name !== ModifierName.not && name != ModifierName.have)
return 'modifier-unknown'

const firstModifier = getAccessorValue(modifiers[0])

// and the first modifier has to be either "resolves" or "rejects"
// and the first modifier has to be either "resolves" or "rejects" or "to"
if (
firstModifier !== ModifierName.resolves
&& firstModifier !== ModifierName.rejects
&& firstModifier !== ModifierName.to
)
return 'modifier-unknown'
}
else {
return 'modifier-unknown'
}

modifiers.push(member)
}

Expand Down
2 changes: 2 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export enum HookName {
}

export enum ModifierName {
to = 'to',
have = 'have',
not = 'not',
rejects = 'rejects',
resolves = 'resolves',
Expand Down
8 changes: 8 additions & 0 deletions tests/valid-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ ruleTester.run(RULE_NAME, rule, {
'expect(value, "message").toBe(1);',
'expect(value, `message`).toBe(1);',
'const message = "message"; expect(value, `${message}`).toBe(1);',
`it('example', () => {
expect("foo bar").to.include("foo");
});
`,
`it('example', () => {
expect("hey").to.have.property("foo", "bar")
});
`,
{
code: 'expect(1).toBe(2);',
options: [{ maxArgs: 2 }]
Expand Down

0 comments on commit cb50e0a

Please sign in to comment.