Skip to content

Commit

Permalink
fix(no-identical-title): not reporting when using backticks (#237)
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
himynameisdave authored and SimenB committed Mar 12, 2019
1 parent 42d2d42 commit 4f8ef6d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
32 changes: 32 additions & 0 deletions rules/__tests__/no-identical-title.test.js
Expand Up @@ -12,6 +12,8 @@ ruleTester.run('no-identical-title', rule, {
' it("it", function() {});',
'});',
].join('\n'),
['describe();describe();'].join('\n'),
['it();it();'].join('\n'),
[
'describe("describe1", function() {',
' it("it1", function() {});',
Expand Down Expand Up @@ -81,6 +83,17 @@ ruleTester.run('no-identical-title', rule, {
' });',
'});',
].join('\n'),
{
code: [
'describe("describe", () => {',
' it(`testing ${someVar} with the same title`, () => {});',
' it(`testing ${someVar} with the same title`, () => {});',
'});',
].join('\n'),
env: {
es6: true,
},
},
],

invalid: [
Expand Down Expand Up @@ -212,5 +225,24 @@ ruleTester.run('no-identical-title', rule, {
},
],
},
{
code: [
'describe("describe", () => {',
' it(`testing backticks with the same title`, () => {});',
' it(`testing backticks with the same title`, () => {});',
'});',
].join('\n'),
env: {
es6: true,
},
errors: [
{
message:
'Test title is used multiple times in the same describe block.',
column: 5,
line: 3,
},
],
},
],
});
23 changes: 18 additions & 5 deletions rules/no-identical-title.js
@@ -1,6 +1,12 @@
'use strict';

const { getDocsUrl, isDescribe, isTestCase } = require('./util');
const {
getDocsUrl,
isDescribe,
isTestCase,
isString,
hasExpressions,
} = require('./util');

const newDescribeContext = () => ({
describeTitles: [],
Expand Down Expand Up @@ -34,8 +40,15 @@ const handleDescribeBlockTitles = (context, titles, node, title) => {
titles.push(title);
};

const isFirstArgLiteral = node =>
node.arguments && node.arguments[0] && node.arguments[0].type === 'Literal';
const isFirstArgValid = arg => {
if (!arg || !isString(arg)) {
return false;
}
if (arg.type === 'TemplateLiteral' && hasExpressions(arg)) {
return false;
}
return true;
};

module.exports = {
meta: {
Expand All @@ -51,10 +64,10 @@ module.exports = {
if (isDescribe(node)) {
contexts.push(newDescribeContext());
}
if (!isFirstArgLiteral(node)) {
const [firstArgument] = node.arguments;
if (!isFirstArgValid(firstArgument)) {
return;
}

const title = node.arguments[0].value;
handleTestCaseTitles(context, currentLayer.testTitles, node, title);
handleDescribeBlockTitles(
Expand Down
3 changes: 3 additions & 0 deletions rules/util.js
Expand Up @@ -134,6 +134,8 @@ const isString = node =>
(node.type === 'Literal' && typeof node.value === 'string') ||
node.type === 'TemplateLiteral';

const hasExpressions = node => node.expressions && node.expressions.length > 0;

/**
* Generates the URL to documentation for the given rule name. It uses the
* package version to build the link to a tagged version of the
Expand Down Expand Up @@ -212,6 +214,7 @@ module.exports = {
isFunction,
isTestCase,
isString,
hasExpressions,
getDocsUrl,
scopeHasLocalReference,
composeFixers,
Expand Down

0 comments on commit 4f8ef6d

Please sign in to comment.