Skip to content

Commit

Permalink
Resolve issue SonarSource#4631
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmorand-sonarsource committed Mar 27, 2024
1 parent ce1d001 commit 14d73a7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/jsts/src/rules/S2699/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Rule, SourceCode } from 'eslint';
import * as estree from 'estree';
import { childrenOf } from '../../linter';
import { Chai, isFunctionCall, Mocha, resolveFunction, Sinon, Vitest } from '../helpers';
import { Supertest } from '../helpers/supertest';

/**
* We assume that the user is using a single assertion library per file,
Expand All @@ -40,7 +41,12 @@ export const rule: Rule.RuleModule = {
}
},
'Program:exit': () => {
if (Chai.isImported(context) || Sinon.isImported(context) || Vitest.isImported(context)) {
if (
Chai.isImported(context) ||
Sinon.isImported(context) ||
Vitest.isImported(context) ||
Supertest.isImported(context)
) {
potentialIssues.forEach(issue => {
context.report(issue);
});
Expand Down Expand Up @@ -84,7 +90,8 @@ class TestCaseAssertionVisitor {
if (
Chai.isAssertion(context, node) ||
Sinon.isAssertion(context, node) ||
Vitest.isAssertion(context, node)
Vitest.isAssertion(context, node) ||
Supertest.isAssertion(context, node)
) {
this.hasAssertions = true;
return;
Expand Down
20 changes: 20 additions & 0 deletions packages/jsts/src/rules/S2699/supertest.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import supertest from 'supertest';
import foo from 'supertest';

describe('supertest', function () { // Compliant
it('should work when assigned to a variable named "supertest" and the "get" HTTP verb', function () {
return supertest(app).get(`/foo/bar`).expect('Content-Type', /json/u).expect(200);
});

it('should work regardless of the HTTP verb', function () { // Compliant
return supertest(app).foo(`/foo/bar`).expect('Content-Type', /json/u).expect(200);
});

it('should work regardless of the name of the assigned variable', function () { // Compliant
return foo(app).get(`/foo/bar`).expect('Content-Type', /json/u).expect(200);
});

it('should fail when no assertion', function () { // Noncompliant
return supertest(app).get(`/foo/bar`);
});
});
57 changes: 57 additions & 0 deletions packages/jsts/src/rules/helpers/supertest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2024 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 { getFullyQualifiedName, getImportDeclarations, getRequireCalls } from '.';
import * as estree from 'estree';

export namespace Supertest {
export function isImported(context: Rule.RuleContext): boolean {
return (
getRequireCalls(context).some(
r => r.arguments[0].type === 'Literal' && r.arguments[0].value === 'supertest',
) || getImportDeclarations(context).some(i => i.source.value === 'supertest')
);
}

export function isAssertion(context: Rule.RuleContext, node: estree.Node) {
const fqn = extractFQNForCallExpression(context, node);

if (!fqn) {
return false;
}

const names = fqn.split('.');

/**
* supertest assertions look like `[alias resolved to supertest](...).[HTTP verb](...).expect(...)`, typically:
* `supertest(application).get('/foo').expect(200)`
* hence only the first and third values matter, the second one being an HTTP verb irrelevant for assertion detection
*/
return names.length >= 3 && names[0] === 'supertest' && names[2] === 'expect';
}

function extractFQNForCallExpression(context: Rule.RuleContext, node: estree.Node) {
if (node.type !== 'CallExpression') {
return undefined;
}

return getFullyQualifiedName(context, node);
}
}

0 comments on commit 14d73a7

Please sign in to comment.