-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be2ebd9
commit 8c2a4c3
Showing
4 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// sum.test.js | ||
const vitest = require('vitest'); | ||
const { describe, expect, it } = require('vitest'); | ||
|
||
describe('vitest test cases', () => { | ||
it('no assertion', () => { // Noncompliant {{Add at least one assertion to this test case.}} | ||
alert('msg'); | ||
}); | ||
|
||
it('expect', () => { // Compliant | ||
expect(1).toEqual(2); | ||
}); | ||
|
||
it('vitest.expect', () => { // Compliant | ||
vitest.expect(1).toEqual(2); | ||
}); | ||
|
||
it('transitive assertion', () => { // Compliant | ||
check(); | ||
}); | ||
|
||
function check() { | ||
expect(1).toEqual(2); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
import { Rule } from 'eslint'; | ||
import * as estree from 'estree'; | ||
import { getFullyQualifiedName, getImportDeclarations, getRequireCalls } from '.'; | ||
|
||
export namespace Vitest { | ||
export function isImported(context: Rule.RuleContext): boolean { | ||
return ( | ||
getRequireCalls(context).some( | ||
r => r.arguments[0].type === 'Literal' && r.arguments[0].value === 'vitest', | ||
) || getImportDeclarations(context).some(i => i.source.value === 'vitest') | ||
); | ||
} | ||
|
||
export function isAssertion(context: Rule.RuleContext, node: estree.Node): boolean { | ||
return isExpectUsage(context, node); | ||
} | ||
|
||
function isExpectUsage(context: Rule.RuleContext, node: estree.Node) { | ||
// expect(), vitest.expect() | ||
return extractFQNforCallExpression(context, node) === 'vitest.expect'; | ||
} | ||
|
||
function extractFQNforCallExpression(context: Rule.RuleContext, node: estree.Node) { | ||
if (node.type !== 'CallExpression') { | ||
return undefined; | ||
} | ||
return getFullyQualifiedName(context, node); | ||
} | ||
} |