Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create rule S2187: Test files should contain at least one test case #4442

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ant-design:tests/dekko/dist.test.js": [
0
],
"ant-design:tests/dekko/lib.test.js": [
0
]
}
20 changes: 20 additions & 0 deletions packages/jsts/src/rules/S2187/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/
export { rule } from './rule';
124 changes: 124 additions & 0 deletions packages/jsts/src/rules/S2187/rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.
*/
// https://sonarsource.github.io/rspec/#/rspec/S2187/javascript

import { Rule } from 'eslint';
import { Node } from 'estree';

const APIs = new Set([
// Jasmine
'it',
'fit',
'xit',
// Jest
'it',
'it.concurrent',
'it.concurrent.each',
'it.concurrent.only.each',
'it.concurrent.skip.each',
'it.each',
'it.failing',
'it.failing.each',
'it.only.failing',
'it.skip.failing',
'it.only',
'it.only.each',
'it.skip',
'it.skip.each',
'it.todo',
'test',
'test.concurrent',
'test.concurrent.each',
'test.concurrent.only.each',
'test.concurrent.skip.each',
'test.each',
'test.failing',
'test.failing.each',
'test.only.failing',
'test.skip.failing',
'test.only',
'test.only.each',
'test.skip',
'test.skip.each',
'test.todo',
ilia-kebets-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
// Mocha
'it',
'it.skip',
'it.only',
'test',
'test.skip',
'test.only',
// Node.js
'it',
'it.skip',
'it.todo',
'it.only',
'test',
'test.skip',
'test.todo',
'test.only',
]);

export const rule: Rule.RuleModule = {
meta: {
messages: {
missingTest: 'Add some tests to this file or delete it.',
},
},
create(context: Rule.RuleContext) {
const { filename } = context;
if (!/\.spec\.|\.test\./.exec(filename)) {
return {};
}

let hasTest = false;
return {
CallExpression(node) {
if (hasTest) {
return;
}

const fqn = fullyQualifiedName(node.callee);
if (APIs.has(fqn)) {
hasTest = true;
}
},
'Program:exit'() {
if (!hasTest) {
context.report({
messageId: 'missingTest',
loc: { line: 0, column: 0 },
});
}
},
};
},
};

function fullyQualifiedName(node: Node): string {
switch (node.type) {
case 'Identifier':
return node.name;
case 'MemberExpression':
return `${fullyQualifiedName(node.object)}.${fullyQualifiedName(node.property)}`;
default:
return '';
}
}
94 changes: 94 additions & 0 deletions packages/jsts/src/rules/S2187/unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 { RuleTester } from 'eslint';
import { rule } from './';

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
ruleTester.run('Test files should contain at least one test case', rule, {
valid: [
{
code: `/* empty main file */`,
filename: 'foo.js',
},
{
code: `
/* a test file using 'it' */
it('1 + 2 should give 3', () => {
expect(1 + 2).toBe(3)
});`,
filename: 'foo.test.js',
},
{
code: `
/* a test file using 'it.only' */
it.only('1 + 2 should give 3', () => {
expect(1 + 2).toBe(3)
});`,
filename: 'foo.test.js',
},
{
code: `
/* a test file using 'test' */
test('1 + 2 should give 3', () => {
expect(1 + 2).toBe(3)
});`,
filename: 'foo.test.js',
},
{
code: `
/* a test file using 'test.only' */
test.only('1 + 2 should give 3', () => {
expect(1 + 2).toBe(3)
});`,
filename: 'foo.test.js',
},
{
code: `
/* a spec file using 'it' */
it('1 + 2 should give 3', () => {
expect(1 + 2).toBe(3)
});`,
filename: 'foo.spec.js',
},
],
invalid: [
{
code: `/* empty test file */`,
filename: 'foo.test.js',
errors: [
{
message: 'Add some tests to this file or delete it.',
line: 0,
column: 1,
},
],
},
{
code: `/* empty spec file */`,
filename: 'foo.spec.js',
errors: 1,
},
{
code: `it['coverage']();`,
filename: 'foo.spec.js',
errors: 1,
},
],
});
2 changes: 2 additions & 0 deletions packages/jsts/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ import { rule as S6323 } from './S6323'; // no-empty-alternatives
import { rule as S1186 } from './S1186'; // no-empty-function
import { rule as S6331 } from './S6331'; // no-empty-group
import { rule as S4023 } from './S4023'; // no-empty-interface
import { rule as S2187 } from './S2187'; // no-empty-test-file
import { rule as S888 } from './S888'; // no-equals-in-for-termination
import { rule as S6426 } from './S6426'; // no-exclusive-tests
import { rule as S6643 } from './S6643'; // no-extend-native
Expand Down Expand Up @@ -429,6 +430,7 @@ rules['no-empty-alternatives'] = S6323;
rules['no-empty-function'] = S1186;
rules['no-empty-group'] = S6331;
rules['no-empty-interface'] = S4023;
rules['no-empty-test-file'] = S2187;
rules['no-equals-in-for-termination'] = S888;
rules['no-exclusive-tests'] = S6426;
rules['no-extend-native'] = S6643;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoEmptyCollectionCheck.class,
NoEmptyGroupCheck.class,
NoEmptyInterfaceCheck.class,
NoEmptyTestFileCheck.class,
NoExclusiveTestsCheck.class,
NoExtendNativeCheck.class,
NoExtraBindCheck.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.
*/
package org.sonar.javascript.checks;

import org.sonar.check.Rule;
import org.sonar.plugins.javascript.api.JavaScriptRule;
import org.sonar.plugins.javascript.api.TestFileCheck;
import org.sonar.plugins.javascript.api.TypeScriptRule;

@JavaScriptRule
@TypeScriptRule
@Rule(key = "S2187")
public class NoEmptyTestFileCheck extends TestFileCheck {

@Override
public String eslintKey() {
return "no-empty-test-file";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<h2>Why is this an issue?</h2>
<p>Test files in JavaScript and TypeScript are meant to contain test cases. These test cases are used to verify the functionality of your code and
ensure that it behaves as expected. If a test file doesn’t contain any test cases, it’s not serving its purpose.</p>
<p>A test file without test cases might indicate:</p>
<ul>
<li> An incomplete test suite: Perhaps the developer started writing tests but didn’t finish. </li>
<li> A mistake: The developer might have accidentally deleted the test cases or moved them to another file. </li>
</ul>
<p>This rule flags any file that has a <code>.test</code> or <code>.spec</code> suffix but does not contain any test cases defined using the different
forms of the <code>it</code> and <code>test</code> functions from Jasmine, Jest, Mocha, or Node.js testing API.</p>
<h2>How to fix it</h2>
<p>To fix a test file that doesn’t contain any test cases, you should add test cases or delete the file if it isn’t needed.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
// eval.test.js

/* no test cases */
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
// eval.test.js

it('1 + 2 should give 3', () =&gt; {
expect(1 + 2).toBe(3);
});
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> Jasmine docs - <a href="https://jasmine.github.io/api/edge/global">API</a> </li>
<li> Jest docs - <a href="https://jestjs.io/docs/api">API</a> </li>
<li> Mocha docs - <a href="https://mochajs.org/#getting-started">API</a> </li>
<li> Node.js docs - <a href="https://nodejs.org/api/test.html">API</a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"title": "Test files should contain at least one test case",
"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH"
},
"attribute": "TESTED"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"jasmine",
"jest",
"mocha",
"node",
"tests",
"unused",
"confusing"
],
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-2187",
"sqKey": "S2187",
"scope": "Tests",
"quickfix": "infeasible",
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"S2094",
"S2123",
"S2137",
"S2187",
"S2189",
"S2201",
"S2234",
Expand Down