Skip to content
Permalink
Browse files
fix(no-identical-title): not reporting when using backticks (#237)
Fixes #232
  • Loading branch information
himynameisdave authored and SimenB committed Mar 12, 2019
1 parent 42d2d42 commit 4f8ef6ddb31666e013b03b5c3b3f9da1c1edb923
Showing with 53 additions and 5 deletions.
  1. +32 −0 rules/__tests__/no-identical-title.test.js
  2. +18 −5 rules/no-identical-title.js
  3. +3 −0 rules/util.js
@@ -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() {});',
@@ -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: [
@@ -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,
},
],
},
],
});
@@ -1,6 +1,12 @@
'use strict';

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

const newDescribeContext = () => ({
describeTitles: [],
@@ -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: {
@@ -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(
@@ -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
@@ -212,6 +214,7 @@ module.exports = {
isFunction,
isTestCase,
isString,
hasExpressions,
getDocsUrl,
scopeHasLocalReference,
composeFixers,

0 comments on commit 4f8ef6d

Please sign in to comment.