Skip to content

Commit 28716ae

Browse files
miszomacklinu
authored andcommitted
feat: add showSuccessMessage config option (#54)
1 parent 2ac7519 commit 28716ae

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

dangerfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import jest from './dist'
22

3-
jest()
3+
jest({ showSuccessMessage: true })

src/__tests__/index.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,30 @@ beforeEach(() => {
2727
}
2828
})
2929

30-
test('messages with passing test results', () => {
30+
test('no fails with passing test results', () => {
3131
jestResults({
3232
testResultsJsonPath: fixture('passing-tests.json'),
3333
})
3434
expect(global['fail']).not.toHaveBeenCalled()
3535
})
3636

37+
test('passing test results with visible success message', () => {
38+
jestResults({
39+
testResultsJsonPath: fixture('passing-tests.json'),
40+
showSuccessMessage: true,
41+
})
42+
expect(global['message']).toHaveBeenCalledWith(
43+
expect.stringMatching(/Jest tests passed/)
44+
)
45+
})
46+
47+
test('passing test results without visible success message', () => {
48+
jestResults({
49+
testResultsJsonPath: fixture('passing-tests.json'),
50+
})
51+
expect(global['message']).not.toHaveBeenCalled()
52+
})
53+
3754
test('fails with failing test results', () => {
3855
jestResults({
3956
testResultsJsonPath: fixture('failing-tests.json'),

src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ import {
1111
} from './types'
1212
declare var danger: DangerDSLType
1313
declare function fail(message?: string): void
14+
declare function message(message?: string): void
1415

1516
export interface IPluginConfig {
1617
testResultsJsonPath: string
18+
showSuccessMessage: boolean
1719
}
1820

1921
export default function jest(config: Partial<IPluginConfig> = {}) {
20-
const { testResultsJsonPath = 'test-results.json' } = config
22+
const {
23+
testResultsJsonPath = 'test-results.json',
24+
showSuccessMessage = false,
25+
} = config
2126
try {
2227
const jsonFileContents = fs.readFileSync(testResultsJsonPath, 'utf8')
2328
const jsonResults: IJestTestResults = JSON.parse(jsonFileContents)
2429
if (jsonResults.success) {
25-
// tslint:disable-next-line:no-console
26-
console.log('Jest tests passed :+1:')
30+
jestSuccessFeedback(jsonResults, showSuccessMessage)
2731
return
2832
}
2933

@@ -42,6 +46,20 @@ export default function jest(config: Partial<IPluginConfig> = {}) {
4246
}
4347
}
4448

49+
const jestSuccessFeedback = (
50+
jsonResults: IJestTestResults,
51+
showSuccessMessage: boolean
52+
): void => {
53+
if (!showSuccessMessage) {
54+
// tslint:disable-next-line:no-console
55+
console.log(':+1: Jest tests passed')
56+
} else {
57+
message(
58+
`:+1: Jest tests passed: ${jsonResults.numPassedTests}/${jsonResults.numTotalTests} (${jsonResults.numPendingTests} skipped)`
59+
)
60+
}
61+
}
62+
4563
const presentErrorsForOldStyleResults = (jsonResults: IJestTestOldResults) => {
4664
const failing = jsonResults.testResults.filter(tr => tr.status === 'failed')
4765

0 commit comments

Comments
 (0)