From 8c55c836d30db494e0081e74a78fe886ea76530d Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Sat, 27 Aug 2022 13:37:48 -0400 Subject: [PATCH] fix: Handle arrow function test with `except-simple` option in `require-expect` rule --- lib/rules/require-expect.js | 10 ++++++---- tests/lib/rules/require-expect.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/rules/require-expect.js b/lib/rules/require-expect.js index 6d2a105b..83163622 100644 --- a/lib/rules/require-expect.js +++ b/lib/rules/require-expect.js @@ -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', assert => {}) + 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', assert => {}) + if (currentTest && !currentTest.node.arguments.includes(node)) { currentTest.blockDepth--; } } diff --git a/tests/lib/rules/require-expect.js b/tests/lib/rules/require-expect.js index c8e07442..9ac305a0 100644 --- a/tests/lib/rules/require-expect.js +++ b/tests/lib/rules/require-expect.js @@ -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 { @@ -125,12 +130,24 @@ ruleTester.run("require-expect", rule, { options: ["except-simple"] }, + // When the test body itself 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) });", @@ -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"] } ],