Skip to content

Commit 42cbe36

Browse files
committed
adds proper testing of custom reporting
1 parent d7037d7 commit 42cbe36

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`reviewRule when rule: custom Errors when plugin doesn not exist 1`] = `[Error: Plugin not found 'FAKE']`;
4+
5+
exports[`reviewRule when rule: unknown rule gets added 1`] = `[Error: Encountered unknown rule]`;

__tests__/command_helpers/reviewRule.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,32 @@ describe('reviewRule', () => {
8080
// should not change rules
8181
expect(reportResults.cliRules.length).toBe(1)
8282
})
83+
84+
test('Errors when plugin doesn not exist', async () => {
85+
const rule = ['CUSTOM', [{ rule: 'custom', plugin: 'FAKE', name: 'checkSecondThing' }]]
86+
87+
// Async error snapshots (not simple)
88+
try {
89+
await reviewRule(rule, reportResults, mockContext)
90+
fail('Unknown rule should have errored')
91+
} catch (e) {
92+
expect(e).toMatchSnapshot()
93+
}
94+
})
8395
})
8496

8597
describe('when rule: unknown', () => {
8698
test('rule gets added', async () => {
8799
const rule = ['UNKNOWN', [{ rule: 'UNKNOWN', command: 'ls', match: '.+' }]]
88-
const numErrors = mockContext.print.error.mock.calls.length
89-
const result = await reviewRule(rule, reportResults, mockContext)
90-
// Failure in a specific rule
91-
expect(mockContext.print.error.mock.calls.length).toBe(numErrors + 1)
100+
101+
// Async error snapshots (not simple)
102+
try {
103+
await reviewRule(rule, reportResults, mockContext)
104+
fail('Unknown rule should have errored')
105+
} catch (e) {
106+
expect(e).toMatchSnapshot()
107+
}
108+
92109
})
93110
})
94111
})

src/extensions/functions/reviewRule.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ module.exports = async (
6565
break
6666
case 'custom':
6767
const customPluginRule = findPluginInfo(rule, context)
68-
if (customPluginRule.success && customPluginRule.plugin.report) {
68+
if (customPluginRule.success) {
6969
// let plugin update the report
70-
customPluginRule.plugin.report(rule, context, report)
70+
if (customPluginRule.plugin.report) customPluginRule.plugin.report(rule, context, report)
7171
} else {
7272
throw new Error(customPluginRule.message)
7373
}
@@ -82,5 +82,4 @@ module.exports = async (
8282
.then(results => {
8383
return results
8484
})
85-
.catch(err => print.error(err))
8685
}

0 commit comments

Comments
 (0)