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

Check static template strings in valid-test-description and valid-suite-description #237

Merged
merged 1 commit into from
Feb 18, 2020
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
10 changes: 6 additions & 4 deletions lib/rules/valid-suite-description.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const { getStringIfConstant } = require('eslint-utils');

/**
* @fileoverview Match suite descriptions to match a pre-configured regular expression
* @author Alexander Afanasyev
*/

const astUtils = require('../util/ast');
const defaultSuiteNames = [ 'describe', 'context', 'suite' ];

function inlineOptions(options) {
Expand Down Expand Up @@ -76,10 +77,11 @@ module.exports = {

function hasValidSuiteDescription(mochaCallExpression) {
const args = mochaCallExpression.arguments;
const description = args[0];
const descriptionArgument = args[0];
const description = getStringIfConstant(descriptionArgument, context.getScope());

if (astUtils.isStringLiteral(description)) {
return pattern.test(description.value);
if (description) {
return pattern.test(description);
}

return true;
Expand Down
9 changes: 5 additions & 4 deletions lib/rules/valid-test-description.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

const { getStringIfConstant } = require('eslint-utils');

/**
* @fileoverview Match test descriptions to match a pre-configured regular expression
* @author Alexander Afanasyev
*/

const astUtils = require('../util/ast');

const defaultTestNames = [ 'it', 'test', 'specify' ];

function inlineOptions(options) {
Expand Down Expand Up @@ -77,9 +77,10 @@ module.exports = {
function hasValidTestDescription(mochaCallExpression) {
const args = mochaCallExpression.arguments;
const testDescriptionArgument = args[0];
const description = getStringIfConstant(testDescriptionArgument, context.getScope());

if (astUtils.isStringLiteral(testDescriptionArgument)) {
return pattern.test(testDescriptionArgument.value);
if (description) {
return pattern.test(description);
}

return true;
Expand Down
5 changes: 0 additions & 5 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ function isMochaFunctionCall(node, scope) {
return isTestCase(node) || isDescribe(node) || isHookCall(node);
}

function isStringLiteral(node) {
return node && node.type === 'Literal' && typeof node.value === 'string';
}

function hasParentMochaFunctionCall(functionExpression) {
return isTestCase(functionExpression.parent) || isHookCall(functionExpression.parent);
}
Expand All @@ -130,7 +126,6 @@ module.exports = {
isMochaFunctionCall,
isHookCall,
isSuiteConfigCall,
isStringLiteral,
hasParentMochaFunctionCall,
findReturnStatement,
isReturnOfUndefined
Expand Down
29 changes: 22 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"changelog": "pr-log"
},
"dependencies": {
"eslint-utils": "^2.0.0",
"ramda": "^0.26.1"
},
"devDependencies": {
Expand Down
30 changes: 30 additions & 0 deletions test/rules/valid-suite-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
parserOptions: { ecmaVersion: 2017 },
options: [ '^Foo' ],
code: 'describe(`Foo with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
options: [ '^Foo' ],
code: 'describe(anyTag`with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
options: [ '^Foo' ],
code: 'describe(`${dynamicVar} with template strings`, function () {});'
}

],
Expand Down Expand Up @@ -89,6 +99,26 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
errors: [
{ message: 'some error message' }
]
},
{
options: [ '^[A-Z]' ],
code: 'describe(`this is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "describe()" description found.', line: 1, column: 1 }
]
},
{
options: [ '^[A-Z]' ],
code: 'const foo = "this"; describe(`${foo} is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "describe()" description found.', line: 1, column: 21 }
]
}
]
});
26 changes: 26 additions & 0 deletions test/rules/valid-test-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
{
parserOptions: { ecmaVersion: 2017 },
code: 'it(`should work with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'it(foo`work with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'it(`${foo} work with template strings`, function () {});'
}
],

Expand Down Expand Up @@ -113,6 +121,24 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
errors: [
{ message: 'Invalid "it()" description found.' }
]
},
{
code: 'it(`this is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "it()" description found.', line: 1, column: 1 }
]
},
{
code: 'const foo = "this"; it(`${foo} is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "it()" description found.', line: 1, column: 21 }
]
}
]
});