From 9be11cf2907d0c74fa209938a02dfc7d4c4482b1 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 16 Feb 2020 16:56:15 +0100 Subject: [PATCH] Recognize t.try() assertion Fixes #272 --- rules/assertion-arguments.js | 8 ++++++++ test/assertion-arguments.js | 8 ++++++++ test/use-t-well.js | 4 ++++ util.js | 3 ++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/rules/assertion-arguments.js b/rules/assertion-arguments.js index a446e364..5259815b 100644 --- a/rules/assertion-arguments.js +++ b/rules/assertion-arguments.js @@ -115,6 +115,14 @@ const create = context => { return; } + if (members[0] === 'try') { + if (gottenArgs < 1) { + report(node, 'Not enough arguments. Expected at least 1.'); + } + + return; + } + const nArgs = expectedNbArguments[members[0]]; if (!nArgs) { diff --git a/test/assertion-arguments.js b/test/assertion-arguments.js index bcd1ce66..b733bee9 100644 --- a/test/assertion-arguments.js +++ b/test/assertion-arguments.js @@ -105,6 +105,9 @@ ruleTester.run('assertion-arguments', rule, { testCase('always', 't.is.skip(\'same\', \'same\', \'message\');'), testCase('always', 't.snapshot(value, \'message\');'), testCase('always', 't.timeout(100);'), + testCase('always', 't.try(tt => tt.pass());'), + testCase('always', 't.try(tt => tt.pass(), 1, 2);'), + testCase('always', 't.try(\'title\', tt => tt.pass(), 1, 2);'), // Shouldn't be triggered since it's not a test file testCase('always', 't.true(true);', [], false), @@ -134,6 +137,10 @@ ruleTester.run('assertion-arguments', rule, { testCase('never', 't.is.skip(\'same\', \'same\');'), testCase('never', 't.snapshot(value);'), testCase('never', 't.timeout(100);'), + testCase('never', 't.try(tt => tt.pass());'), + testCase('never', 't.try(tt => tt.pass(), 1, 2);'), + testCase('never', 't.try(\'title\', tt => tt.pass(), 1, 2);'), + // Shouldn't be triggered since it's not a test file testCase('never', 't.true(true, \'message\');', [], false), @@ -181,6 +188,7 @@ ruleTester.run('assertion-arguments', rule, { testCase(false, 't.is.skip(\'same\');', tooFewError(2)), testCase(false, 't.snapshot();', tooFewError(1)), testCase(false, 't.timeout();', tooFewError(1)), + testCase(false, 't.try();', tooFewError(1)), // Too many arguments testCase(false, 't.plan(1, \'extra argument\');', tooManyError(1)), diff --git a/test/use-t-well.js b/test/use-t-well.js index f17cea71..a923f1be 100644 --- a/test/use-t-well.js +++ b/test/use-t-well.js @@ -65,6 +65,10 @@ ruleTester.run('use-t-well', rule, { testCase('t.context.foo(a, a);'), testCase('foo.t.bar(a, a);'), testCase('t.timeout(100);'), + testCase('t.try(tt => tt.pass())'), + testCase('t.try(tt => tt.pass(), 1, 2)'), + testCase('t.try(\'title\', tt => tt.pass())'), + testCase('t.try(\'title\', tt => tt.pass(), 1, 2)'), // Shouldn't be triggered since it's not a test file testCase('t.foo(a, a);', false), testCase('t.foo;', false) diff --git a/util.js b/util.js index 2c814ffb..37d19c7e 100644 --- a/util.js +++ b/util.js @@ -116,7 +116,8 @@ const assertionMethodsNumArguments = new Map([ ['throws', 1], ['throwsAsync', 1], ['true', 1], - ['truthy', 1] + ['truthy', 1], + ['try', 1] ]); const assertionMethodNames = [...assertionMethodsNumArguments.keys()];