Skip to content

Commit a0fb41c

Browse files
committed
Merge pull request #5 from Springworks/top-describe-path-context
Allow use of context alias in top-describe-path
2 parents 801821b + f9afd42 commit a0fb41c

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

docs/rules/top-describe-path.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Ensure that the top `describe` call in each test file use the correct path (top-describe-path)
1+
# Ensure that the top `describe` or `context` call in each test file use the correct path (top-describe-path)
22

33
We use `mocha` in many of our projects and have adopted the convention of using `__filename` as the description in the root `describe` in each test file. That way it's easy to find a failing test.
44
It is easier and less error-prone to write `__filename` then the actual path.

lib/rules/top-describe-path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function(context) {
1212
return n.type === 'ExpressionStatement' &&
1313
n.expression.type === 'CallExpression' &&
1414
n.expression.callee.type === 'Identifier' &&
15-
n.expression.callee.name === 'describe';
15+
(n.expression.callee.name === 'describe' || n.expression.callee.name === 'context');
1616
}
1717

1818
function getTopDescribeCallExpression(program_node) {
@@ -39,7 +39,7 @@ module.exports = function(context) {
3939

4040
context.report({
4141
node: arg,
42-
message: 'Unexpected path in top describe',
42+
message: 'Unexpected path in top ' + top_desc.callee.name,
4343
fix: fixer => fixer.replaceText(arg, '\'' + filepath + '\''),
4444
});
4545
},

tests/lib/rules/top-describe-path.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ ruleTester.run("top-describe-path", rule, {
1818
code: "var x = 1; describe('tests/lib/rules/top-describe-path.js', function() { describe('foo', function() { }); });",
1919
filename: __filename,
2020
},
21+
{
22+
code: "context('tests/lib/rules/top-describe-path.js', function() { });",
23+
filename: __filename,
24+
},
25+
{
26+
code: "context('tests/lib/rules/top-describe-path.js', function() { describe('foo', function() { }); });",
27+
filename: __filename,
28+
},
29+
{
30+
code: "var x = 1; context('tests/lib/rules/top-describe-path.js', function() { describe('foo', function() { }); });",
31+
filename: __filename,
32+
},
2133
],
2234

2335
invalid: [
@@ -57,5 +69,41 @@ ruleTester.run("top-describe-path", rule, {
5769
type: "Literal"
5870
}],
5971
},
72+
{
73+
code: "context(__filename, function() { });",
74+
output: "context('tests/lib/rules/top-describe-path.js', function() { });",
75+
filename: __filename,
76+
errors: [{
77+
message: "Unexpected path in top context",
78+
type: "Identifier"
79+
}],
80+
},
81+
{
82+
code: "context(__filename, function() { describe('foo', function() { }); });",
83+
output: "context('tests/lib/rules/top-describe-path.js', function() { describe('foo', function() { }); });",
84+
filename: __filename,
85+
errors: [{
86+
message: "Unexpected path in top context",
87+
type: "Identifier"
88+
}],
89+
},
90+
{
91+
code: "context('incorrect path', function() { });",
92+
output: "context('tests/lib/rules/top-describe-path.js', function() { });",
93+
filename: __filename,
94+
errors: [{
95+
message: "Unexpected path in top context",
96+
type: "Literal"
97+
}],
98+
},
99+
{
100+
code: "context('incorrect path', function() { describe('foo', function() { }); });",
101+
output: "context('tests/lib/rules/top-describe-path.js', function() { describe('foo', function() { }); });",
102+
filename: __filename,
103+
errors: [{
104+
message: "Unexpected path in top context",
105+
type: "Literal"
106+
}],
107+
},
60108
],
61109
});

0 commit comments

Comments
 (0)