Skip to content

Commit 4f8ef6d

Browse files
himynameisdaveSimenB
authored andcommitted
fix(no-identical-title): not reporting when using backticks (#237)
Fixes #232
1 parent 42d2d42 commit 4f8ef6d

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

rules/__tests__/no-identical-title.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ ruleTester.run('no-identical-title', rule, {
1212
' it("it", function() {});',
1313
'});',
1414
].join('\n'),
15+
['describe();describe();'].join('\n'),
16+
['it();it();'].join('\n'),
1517
[
1618
'describe("describe1", function() {',
1719
' it("it1", function() {});',
@@ -81,6 +83,17 @@ ruleTester.run('no-identical-title', rule, {
8183
' });',
8284
'});',
8385
].join('\n'),
86+
{
87+
code: [
88+
'describe("describe", () => {',
89+
' it(`testing ${someVar} with the same title`, () => {});',
90+
' it(`testing ${someVar} with the same title`, () => {});',
91+
'});',
92+
].join('\n'),
93+
env: {
94+
es6: true,
95+
},
96+
},
8497
],
8598

8699
invalid: [
@@ -212,5 +225,24 @@ ruleTester.run('no-identical-title', rule, {
212225
},
213226
],
214227
},
228+
{
229+
code: [
230+
'describe("describe", () => {',
231+
' it(`testing backticks with the same title`, () => {});',
232+
' it(`testing backticks with the same title`, () => {});',
233+
'});',
234+
].join('\n'),
235+
env: {
236+
es6: true,
237+
},
238+
errors: [
239+
{
240+
message:
241+
'Test title is used multiple times in the same describe block.',
242+
column: 5,
243+
line: 3,
244+
},
245+
],
246+
},
215247
],
216248
});

rules/no-identical-title.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict';
22

3-
const { getDocsUrl, isDescribe, isTestCase } = require('./util');
3+
const {
4+
getDocsUrl,
5+
isDescribe,
6+
isTestCase,
7+
isString,
8+
hasExpressions,
9+
} = require('./util');
410

511
const newDescribeContext = () => ({
612
describeTitles: [],
@@ -34,8 +40,15 @@ const handleDescribeBlockTitles = (context, titles, node, title) => {
3440
titles.push(title);
3541
};
3642

37-
const isFirstArgLiteral = node =>
38-
node.arguments && node.arguments[0] && node.arguments[0].type === 'Literal';
43+
const isFirstArgValid = arg => {
44+
if (!arg || !isString(arg)) {
45+
return false;
46+
}
47+
if (arg.type === 'TemplateLiteral' && hasExpressions(arg)) {
48+
return false;
49+
}
50+
return true;
51+
};
3952

4053
module.exports = {
4154
meta: {
@@ -51,10 +64,10 @@ module.exports = {
5164
if (isDescribe(node)) {
5265
contexts.push(newDescribeContext());
5366
}
54-
if (!isFirstArgLiteral(node)) {
67+
const [firstArgument] = node.arguments;
68+
if (!isFirstArgValid(firstArgument)) {
5569
return;
5670
}
57-
5871
const title = node.arguments[0].value;
5972
handleTestCaseTitles(context, currentLayer.testTitles, node, title);
6073
handleDescribeBlockTitles(

rules/util.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ const isString = node =>
134134
(node.type === 'Literal' && typeof node.value === 'string') ||
135135
node.type === 'TemplateLiteral';
136136

137+
const hasExpressions = node => node.expressions && node.expressions.length > 0;
138+
137139
/**
138140
* Generates the URL to documentation for the given rule name. It uses the
139141
* package version to build the link to a tagged version of the
@@ -212,6 +214,7 @@ module.exports = {
212214
isFunction,
213215
isTestCase,
214216
isString,
217+
hasExpressions,
215218
getDocsUrl,
216219
scopeHasLocalReference,
217220
composeFixers,

0 commit comments

Comments
 (0)