Skip to content

Commit

Permalink
fix: Handle arrow function test with except-simple option in `requi…
Browse files Browse the repository at this point in the history
…re-expect` rule
  • Loading branch information
bmish committed Aug 27, 2022
1 parent ef8f129 commit 5b275f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/rules/require-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,16 @@ module.exports = {
}
},

"BlockStatement, ArrowFunctionExpression[body.type!='BlockStatement]": function () {
if (currentTest) {
"BlockStatement, ArrowFunctionExpression[body.type!='BlockStatement]": function (node) {
// Ignore any arrow function used by the test body itself i.e.: test('name', () => {})
if (currentTest && !currentTest.node.arguments.includes(node)) {
currentTest.blockDepth++;
}
},

"BlockStatement, ArrowFunctionExpression[body.type!='BlockStatement]:exit": function () {
if (currentTest) {
"BlockStatement, ArrowFunctionExpression[body.type!='BlockStatement]:exit": function (node) {
// Ignore any arrow function used by the test body itself i.e.: test('name', () => {})
if (currentTest && !currentTest.node.arguments.includes(node)) {
currentTest.blockDepth--;
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/require-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ ruleTester.run("require-expect", rule, {
code: "test('name', function(assert) { assert.expect(0); noParentObject() });",
options: ["always"]
},
{
// With arrow function
code: "test('name', assert => { assert.expect(0); noParentObject() });",
options: ["always"]
},

// assert at top of test context is ok
{
Expand Down Expand Up @@ -125,12 +130,24 @@ ruleTester.run("require-expect", rule, {
options: ["except-simple"]
},

// When the test is using an arrow function, make sure we still correctly-detect top-level expect.
{
code: "test('name', assert => { assert.expect(2); assert.ok(true); assert.ok(true); });",
options: ["except-simple"]
}

// "never" - assert without expect is fine
{
code: "test('name', function(assert) { assert.ok(true) });",
options: ["never"]
},

// "never" - assert without expect is fine (with arrow function)
{
code: "test('name', assert => { assert.ok(true) });",
options: ["never"]
},

// "never-except-zero" - assert without expect is fine
{
code: "test('name', function(assert) { assert.ok(true) });",
Expand All @@ -141,6 +158,12 @@ ruleTester.run("require-expect", rule, {
{
code: "test('name', function(assert) { assert.expect(0) });",
options: ["never-except-zero"]
},

// "never-except-zero" - expect zero is fine (with arrow function)
{
code: "test('name', assert => { assert.expect(0) });",
options: ["never-except-zero"]
}
],

Expand Down

0 comments on commit 5b275f6

Please sign in to comment.