Rule S2699 (Tests should include assertions) - assertions stored in a separate file

Must-share information (formatted with Markdown):

  • SonarQube 9.9
  • Running on azure VM
  • Currently the reported ‘Must have assertion’ issues are set as False Positives
  • It works without error if the assert is in the TestMethod but we want the code to be maintainable and separated out into a common file - then we get the error as sonar is unable to identify the assertion.

I understand that for this rule then
cross-procedural / file analysis is only able to explore the body of test methods. Our sonar account manager says this was closed years ago - - was it related to this issue ?
Is there a fix for this in v10.4 or a planned future version ? Or a recommended work around perhaps ?

Hey David.

Can you share a small sample project that demonstrates the issue?

const chai = require('chai')

function checkElementHasText ($, id, text) {
  chai.assert.equal($('#' + id).text().trim(), text)

function checkElementContainsText ($, id, text) {
  chai.assert.containIgnoreSpaces($('#' + id).text().trim(), text)

function checkElementHasHtml ($, id, html) {
  chai.assert.containIgnoreSpaces($('#' + id).html().trim(), html)

function checkElementAttributeHasValue ($, id, attribute, value) {
  chai.assert.equal($('#' + id).attr(attribute), value)

function checkElementHasClass ($, id, classo) {
  chai.assert.isTrue($('#' + id).hasClass(classo))

function checkElementDoesNotHaveClass ($, id, classo) {
  chai.assert.isFalse($('#' + id).hasClass(classo))

function checkLinkHasHref ($, id, href) {
  chai.assert.equal($('#' + id).attr('href'), href)

function checkPageDoesNotHaveText ($, text) {

function checkElementNotPresent ($, id) {
  chai.assert.equal($('#' + id).length, 0)

function checkElementIsPresent ($, id) {
  chai.assert.isTrue($('#' + id).length > 0)

module.exports = {
  checkElementHasText, checkElementContainsText, checkElementHasHtml, checkElementAttributeHasValue, checkElementHasClass, checkElementDoesNotHaveClass, checkLinkHasHref, checkPageDoesNotHaveText, checkElementNotPresent, checkElementIsPresent

Hello @David_Brownhill,

Thank you for bringing up this concern regarding the rule about test assertions in test files. You’re absolutely correct in pointing out the importance of cross-file analysis, particularly when assertions are modularized across different files.

At the moment, several rules within the JavaScript analyzer are confined to considering the scope of individual files when reporting issues. This, unfortunately, includes the rule related to test assertions. We acknowledge the limitation and are planning to introduce more advanced analysis capabilities throughout this year.

However, since we are only at the early stages of our plans, nothing has been decided yet regarding whether this would directly benefit the specific rule you mentioned. We are currently in the process of evaluating various options and their potential impacts.

In the meantime, the only viable workaround available would be to either disable the rule or mark the reported issues as false positives while awaiting further enhancements.

Hope this helps,