Skip to content

Commit

Permalink
Fix: Check for arrow functions in utils.getAssertContextNameForTest (#…
Browse files Browse the repository at this point in the history
…214)

* Fix: Check for arrow functions in utils.getAssertContextNameForTest

Fixes #213

* Add tests to other affected rules
  • Loading branch information
edg2s authored Nov 3, 2021
1 parent 34968fe commit ad5f79f
Show file tree
Hide file tree
Showing 18 changed files with 280 additions and 120 deletions.
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ exports.isSkip = function (calleeNode) {

exports.getAssertContextNameForTest = function (argumentsNodes) {
const functionExpr = argumentsNodes.find(function (argNode) {
return argNode.type === "FunctionExpression";
return argNode.type === "FunctionExpression" || argNode.type === "ArrowFunctionExpression";
});

return this.getAssertContextName(functionExpr);
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/assert-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function wrap(assertionCode, testName = "Name") {
return `QUnit.test('${testName}', function (assert) { ${assertionCode} });`;
}

function wrapArrow(assertionCode, testName = "Name") {
return `QUnit.test('${testName}', (assert) => { ${assertionCode} });`;
}

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -377,6 +381,17 @@ ruleTester.run("assert-args", rule, {
}
}]
},
{
code: wrapArrow("assert.strictEqual();"),
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpectedArgCountNoMessage",
data: {
callee: "assert.strictEqual",
argCount: 0
}
}]
},
{
code: wrap("assert.strictEqual(a);"),
errors: [{
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/literal-compare-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function wrap(assertionCode, testName = "Name") {
return `QUnit.test('${testName}', function (assert) { ${assertionCode} });`;
}

function wrapArrow(assertionCode, testName = "Name") {
return `QUnit.test('${testName}', (assert) => { ${assertionCode} });`;
}

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -115,6 +119,18 @@ ruleTester.run("literal-compare-order", rule, {
}
}]
},
{
code: wrapArrow("assert.equal('Literal', variable);"),
output: wrapArrow("assert.equal(variable, 'Literal');"),
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "actualFirst",
data: {
expected: "'Literal'",
actual: "variable"
}
}]
},
{
code: wrap("assert.equal('Literal', variable, 'message');"),
output: wrap("assert.equal(variable, 'Literal', 'message');"),
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-assert-equal-boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ ruleTester.run("no-assert-equal-boolean", rule, {
output: "QUnit.test('Name', function (assert) { assert.true(a); });",
errors: [{ messageId: "useAssertTrueOrFalse" }]
},
{
code: "QUnit.test('Name', (assert) => { assert.equal(a, true); });",
output: "QUnit.test('Name', (assert) => { assert.true(a); });",
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "useAssertTrueOrFalse" }]
},
{
code: "QUnit.test('Name', function (assert) { assert.equal(a, false); });",
output: "QUnit.test('Name', function (assert) { assert.false(a); });",
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/no-assert-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ ruleTester.run("no-assert-equal", rule, {
]
}]
},
{
code: "QUnit.test('Name', (assert) => { assert.equal(a, b); });",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpectedAssertEqual",
data: { assertVar: "assert" },
suggestions: [
{
messageId: "switchToDeepEqual",
output: "QUnit.test('Name', (assert) => { assert.deepEqual(a, b); });"
},
{
messageId: "switchToPropEqual",
output: "QUnit.test('Name', (assert) => { assert.propEqual(a, b); });"
},
{
messageId: "switchToStrictEqual",
output: "QUnit.test('Name', (assert) => { assert.strictEqual(a, b); });"
}
]
}]
},
{
code: "QUnit.test('Name', function (foo) { foo.equal(a, b); });",
errors: [{
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-assert-logical-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function wrap(code) {
return `QUnit.test('test', function (assert) { ${code} });`;
}

function wrapArrow(code) {
return `QUnit.test('test', (assert) => { ${code} });`;
}

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -68,6 +72,19 @@ ruleTester.run("no-assert-logical-expression", rule, {
column: 50
}]
},
{
code: wrapArrow("assert.ok(foo && bar);"),
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "noLogicalOperator",
data: {
operator: "&&"
},
type: "LogicalExpression",
line: 1,
column: 44
}]
},
{
code: wrap("assert.ok(foo || bar);"),
errors: [{
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/no-assert-ok.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ ruleTester.run("no-assert-ok", rule, {
}
}]
},
{
code: "QUnit.test('Name', (assert) => { assert.ok(a); });",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpectedLocalOkNotOk",
data: {
assertVar: "assert",
assertion: "ok"
}
}]
},
{
code: "QUnit.test('Name', function (foo) { foo.ok(a); });",
errors: [{
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-async-in-loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,18 @@ ruleTester.run("no-async-in-loops", rule, {
type: "CallExpression"
}]
},
{
code: "test('name', (assert) => { while (false) assert.async(); });",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpectedAsyncInLoop",
data: {
call: "assert.async()",
loopTypeText: "while loop"
},
type: "CallExpression"
}]
},
{
code: "test('name', function (assert) { while (false) { assert.async(); } });",
errors: [{
Expand Down
Loading

0 comments on commit ad5f79f

Please sign in to comment.