Skip to content

Commit

Permalink
testing: Get to 100% coverage
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
brettz9 committed Feb 11, 2021
1 parent 6b87bf3 commit b0af281
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions __tests__/catch-or-return.js
Expand Up @@ -123,6 +123,9 @@ ruleTester.run('catch-or-return', rule, {
code: 'frank().then(go).finally()',
options: [{ terminationMethod: ['catch', 'finally'] }],
},

// for coverage
'nonPromiseExpressionStatement();',
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions __tests__/param-names.js
Expand Up @@ -17,6 +17,7 @@ ruleTester.run('param-names', rule, {
'new Promise(resolve => {})',
'new Promise((resolve, reject) => {})',
'new Promise(() => {})',
'new NonPromise()',
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions __tests__/prefer-await-to-callbacks.js
Expand Up @@ -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: [
Expand Down
1 change: 1 addition & 0 deletions __tests__/prefer-await-to-then.js
Expand Up @@ -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: [
Expand Down
8 changes: 8 additions & 0 deletions package.json
Expand Up @@ -59,6 +59,14 @@
"proseWrap": "always"
},
"jest": {
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"projects": [
{
"displayName": "test",
Expand Down
9 changes: 2 additions & 7 deletions rules/always-return.js
Expand Up @@ -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)) {
Expand All @@ -47,6 +48,7 @@ function hasParentReturnStatement(node) {
return hasParentReturnStatement(node.parent)
}

// istanbul ignore next -- not reachable given not checking `Program`
return false
}

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions rules/lib/has-promise-callback.js
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions rules/lib/is-callback.js
Expand Up @@ -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
5 changes: 5 additions & 0 deletions rules/no-native.js
Expand Up @@ -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
})
}
Expand Down Expand Up @@ -46,6 +50,7 @@ module.exports = {
return
}

// istanbul ignore else
if (!isDeclared(scope, ref)) {
context.report({
node: ref.identifier,
Expand Down
1 change: 1 addition & 0 deletions rules/no-return-in-finally.js
Expand Up @@ -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] &&
Expand Down
2 changes: 2 additions & 0 deletions rules/valid-params.js
Expand Up @@ -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':
Expand Down Expand Up @@ -58,6 +59,7 @@ module.exports = {
}
break
default:
// istanbul ignore next -- `isPromise` filters out others
break
}
},
Expand Down

0 comments on commit b0af281

Please sign in to comment.