Skip to content

Commit

Permalink
adds proper testing of custom reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
GantMan committed Feb 4, 2018
1 parent d7037d7 commit 42cbe36
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions __tests__/command_helpers/__snapshots__/reviewRule.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`reviewRule when rule: custom Errors when plugin doesn not exist 1`] = `[Error: Plugin not found 'FAKE']`;

exports[`reviewRule when rule: unknown rule gets added 1`] = `[Error: Encountered unknown rule]`;
25 changes: 21 additions & 4 deletions __tests__/command_helpers/reviewRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,32 @@ describe('reviewRule', () => {
// should not change rules
expect(reportResults.cliRules.length).toBe(1)
})

test('Errors when plugin doesn not exist', async () => {
const rule = ['CUSTOM', [{ rule: 'custom', plugin: 'FAKE', name: 'checkSecondThing' }]]

// Async error snapshots (not simple)
try {
await reviewRule(rule, reportResults, mockContext)
fail('Unknown rule should have errored')
} catch (e) {
expect(e).toMatchSnapshot()
}
})
})

describe('when rule: unknown', () => {
test('rule gets added', async () => {
const rule = ['UNKNOWN', [{ rule: 'UNKNOWN', command: 'ls', match: '.+' }]]
const numErrors = mockContext.print.error.mock.calls.length
const result = await reviewRule(rule, reportResults, mockContext)
// Failure in a specific rule
expect(mockContext.print.error.mock.calls.length).toBe(numErrors + 1)

// Async error snapshots (not simple)
try {
await reviewRule(rule, reportResults, mockContext)
fail('Unknown rule should have errored')
} catch (e) {
expect(e).toMatchSnapshot()
}

})
})
})
5 changes: 2 additions & 3 deletions src/extensions/functions/reviewRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ module.exports = async (
break
case 'custom':
const customPluginRule = findPluginInfo(rule, context)
if (customPluginRule.success && customPluginRule.plugin.report) {
if (customPluginRule.success) {
// let plugin update the report
customPluginRule.plugin.report(rule, context, report)
if (customPluginRule.plugin.report) customPluginRule.plugin.report(rule, context, report)
} else {
throw new Error(customPluginRule.message)
}
Expand All @@ -82,5 +82,4 @@ module.exports = async (
.then(results => {
return results
})
.catch(err => print.error(err))
}

0 comments on commit 42cbe36

Please sign in to comment.