Skip to content

Commit

Permalink
Merge pull request #315 from lo1tuma/fix-empty-desc
Browse files Browse the repository at this point in the history
Fix no-empty-description rule to not report on dynamic values
  • Loading branch information
lo1tuma committed Dec 13, 2021
2 parents 744d7fa + 76b3312 commit a16b64c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/rules/no-empty-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ function objectOptions(options = {}) {
return { testNames, message };
}

function isTemplateString(node) {
return [ 'TaggedTemplateExpression', 'TemplateLiteral' ].includes(node && node.type);
}

function isIdentifier(node) {
return node && node.type === 'Identifier';
}

function isStaticallyAnalyzableDescription(node, extractedText) {
if (extractedText === null) {
return !(isTemplateString(node) || isIdentifier(node));
}

return true;
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -51,15 +67,11 @@ module.exports = {
return node.callee && node.callee.name && testNames.includes(node.callee.name);
}

function isTemplateString(node) {
return [ 'TaggedTemplateExpression', 'TemplateLiteral' ].includes(node && node.type);
}

function checkDescription(mochaCallExpression) {
function isNonEmptyDescription(mochaCallExpression) {
const description = mochaCallExpression.arguments[0];
const text = getStringIfConstant(description);
const text = getStringIfConstant(description, context.getScope());

if (isTemplateString(description) && text === null) {
if (!isStaticallyAnalyzableDescription(description, text)) {
return true;
}

Expand All @@ -69,7 +81,7 @@ module.exports = {
return {
CallExpression(node) {
if (isTest(node)) {
if (!checkDescription(node)) {
if (!isNonEmptyDescription(node)) {
context.report({
node,
message: message || ERROR_MESSAGE
Expand Down
12 changes: 12 additions & 0 deletions test/rules/no-empty-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ ruleTester.run('no-empty-description', rules['no-empty-description'], {
'test.only("some text")',
'test("some text", function() { })',

'var dynamicTitle = "foo"; it(dynamicTitle, function() {});',
'it(dynamicTitle, function() {});',

'notTest()',

{
Expand All @@ -46,6 +49,10 @@ ruleTester.run('no-empty-description', rules['no-empty-description'], {
{
options: [ { testNames: [ 'someFunction' ] } ],
code: 'someFunction("this is a test", function () { });'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'const dynamicTitle = "foo"; it(dynamicTitle, function() {});'
}
],

Expand Down Expand Up @@ -76,6 +83,11 @@ ruleTester.run('no-empty-description', rules['no-empty-description'], {
parserOptions: { ecmaVersion: 2019 },
code: 'it(` `, function () { });',
errors: [ { message: defaultErrorMessage, ...firstLine } ]
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'const foo = ""; it(foo);',
errors: [ { message: defaultErrorMessage, line: 1, column: 17 } ]
}
]

Expand Down

0 comments on commit a16b64c

Please sign in to comment.