From b0af28120fd9352a5352ecb9aec9bb8abd3f3bf9 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 9 Jul 2020 08:51:26 +0800 Subject: [PATCH] testing: Get to 100% coverage Also: - internally renames `isCallingBack` to `isCallback` as per file name and usage - removed code block from `always-return` which is not necessary given that "good" return/throw statements cannot occur within shortcut syntax (which expect expressions) --- __tests__/catch-or-return.js | 3 +++ __tests__/param-names.js | 1 + __tests__/prefer-await-to-callbacks.js | 1 + __tests__/prefer-await-to-then.js | 1 + package.json | 8 ++++++++ rules/always-return.js | 9 ++------- rules/lib/has-promise-callback.js | 1 + rules/lib/is-callback.js | 5 +++-- rules/no-native.js | 5 +++++ rules/no-return-in-finally.js | 1 + rules/valid-params.js | 2 ++ 11 files changed, 28 insertions(+), 9 deletions(-) diff --git a/__tests__/catch-or-return.js b/__tests__/catch-or-return.js index c9f76ff2..25925936 100644 --- a/__tests__/catch-or-return.js +++ b/__tests__/catch-or-return.js @@ -123,6 +123,9 @@ ruleTester.run('catch-or-return', rule, { code: 'frank().then(go).finally()', options: [{ terminationMethod: ['catch', 'finally'] }], }, + + // for coverage + 'nonPromiseExpressionStatement();', ], invalid: [ diff --git a/__tests__/param-names.js b/__tests__/param-names.js index 473d51ea..7da5c9f4 100644 --- a/__tests__/param-names.js +++ b/__tests__/param-names.js @@ -17,6 +17,7 @@ ruleTester.run('param-names', rule, { 'new Promise(resolve => {})', 'new Promise((resolve, reject) => {})', 'new Promise(() => {})', + 'new NonPromise()', ], invalid: [ diff --git a/__tests__/prefer-await-to-callbacks.js b/__tests__/prefer-await-to-callbacks.js index cc667ac6..78e7d640 100644 --- a/__tests__/prefer-await-to-callbacks.js +++ b/__tests__/prefer-await-to-callbacks.js @@ -17,6 +17,7 @@ ruleTester.run('prefer-await-to-callbacks', rule, { 'async function hi() { await thing().catch() }', 'dbConn.on("error", err => { console.error(err) })', 'dbConn.once("error", err => { console.error(err) })', + 'heart(something => {})', ], invalid: [ diff --git a/__tests__/prefer-await-to-then.js b/__tests__/prefer-await-to-then.js index 18c4e073..8dea660c 100644 --- a/__tests__/prefer-await-to-then.js +++ b/__tests__/prefer-await-to-then.js @@ -20,6 +20,7 @@ ruleTester.run('prefer-await-to-then', rule, { try { await something() } catch (error) { somethingElse() } }`, 'something().then(async () => await somethingElse())', + 'function foo() { hey.somethingElse(x => {}) }', ], invalid: [ diff --git a/package.json b/package.json index e19ce2ec..07fe6059 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,14 @@ "proseWrap": "always" }, "jest": { + "coverageThreshold": { + "global": { + "branches": 100, + "functions": 100, + "lines": 100, + "statements": 100 + } + }, "projects": [ { "displayName": "test", diff --git a/rules/always-return.js b/rules/always-return.js index 2d0606e9..51068497 100644 --- a/rules/always-return.js +++ b/rules/always-return.js @@ -35,6 +35,7 @@ function isInlineThenFunctionExpression(node) { } function hasParentReturnStatement(node) { + // istanbul ignore else -- not reachable given not checking `Program` if (node && node.parent && node.parent.type) { // if the parent is a then, and we haven't returned anything, fail if (isThenCallExpression(node.parent)) { @@ -47,6 +48,7 @@ function hasParentReturnStatement(node) { return hasParentReturnStatement(node.parent) } + // istanbul ignore next -- not reachable given not checking `Program` return false } @@ -139,13 +141,6 @@ module.exports = { return } - // check shortcircuit syntax like `x && x()` and `y || x()`` - const prevSegments = segment.prevSegments - for (let ii = prevSegments.length - 1; ii >= 0; --ii) { - const prevSegment = prevSegments[ii] - if (funcInfo.branchInfoMap[prevSegment.id].good) return - } - context.report({ message: 'Each then() should return a value or throw', node: branch.node, diff --git a/rules/lib/has-promise-callback.js b/rules/lib/has-promise-callback.js index 12607f48..8cf07c01 100644 --- a/rules/lib/has-promise-callback.js +++ b/rules/lib/has-promise-callback.js @@ -7,6 +7,7 @@ 'use strict' function hasPromiseCallback(node) { + // istanbul ignore if -- only being called within `CallExpression` if (node.type !== 'CallExpression') return if (node.callee.type !== 'MemberExpression') return const propertyName = node.callee.property.name diff --git a/rules/lib/is-callback.js b/rules/lib/is-callback.js index 7790ac79..2f60b5d3 100644 --- a/rules/lib/is-callback.js +++ b/rules/lib/is-callback.js @@ -2,12 +2,13 @@ const isNamedCallback = require('./is-named-callback') -function isCallingBack(node, exceptions) { +function isCallback(node, exceptions) { const isCallExpression = node.type === 'CallExpression' + // istanbul ignore next -- always invoked on `CallExpression` const callee = node.callee || {} const nameIsCallback = isNamedCallback(callee.name, exceptions) const isCB = isCallExpression && nameIsCallback return isCB } -module.exports = isCallingBack +module.exports = isCallback diff --git a/rules/no-native.js b/rules/no-native.js index 8503231e..7ad996eb 100644 --- a/rules/no-native.js +++ b/rules/no-native.js @@ -11,10 +11,14 @@ function isDeclared(scope, ref) { return false } + // Presumably can't pass this since the implicit `Promise` global + // being checked here would always lack `defs` + // istanbul ignore else if (!variable.defs || !variable.defs.length) { return false } + // istanbul ignore next return true }) } @@ -46,6 +50,7 @@ module.exports = { return } + // istanbul ignore else if (!isDeclared(scope, ref)) { context.report({ node: ref.identifier, diff --git a/rules/no-return-in-finally.js b/rules/no-return-in-finally.js index bf80ce1c..f70fa01f 100644 --- a/rules/no-return-in-finally.js +++ b/rules/no-return-in-finally.js @@ -19,6 +19,7 @@ module.exports = { node.callee.property && node.callee.property.name === 'finally' ) { + // istanbul ignore else -- passing `isPromise` means should have a body if ( node.arguments && node.arguments[0] && diff --git a/rules/valid-params.js b/rules/valid-params.js index c5ae1219..ea185bd9 100644 --- a/rules/valid-params.js +++ b/rules/valid-params.js @@ -22,6 +22,7 @@ module.exports = { const name = node.callee.property.name const numArgs = node.arguments.length + // istanbul ignore next -- `isPromise` filters out others switch (name) { case 'resolve': case 'reject': @@ -58,6 +59,7 @@ module.exports = { } break default: + // istanbul ignore next -- `isPromise` filters out others break } },