Skip to content

Commit 65a5c6a

Browse files
author
Graydon Hope
committed
Fix tests to include proper testing for output report generation
1 parent 0f60e1b commit 65a5c6a

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

packages/cli/test/unit/commands/scan/output-scan-report.test.mts

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,45 @@
1919
* - src/commands/outputScanReport.mts (implementation)
2020
*/
2121

22-
import { describe, expect, it } from 'vitest'
22+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2323

2424
import {
25+
outputScanReport,
2526
toJsonReport,
2627
toMarkdownReport,
2728
} from '../../../../src/commands/scan/output-scan-report.mts'
2829
import { SOCKET_WEBSITE_URL } from '../../../../src/constants/socket.mts'
2930

3031
import type { ScanReport } from '../../../../src/commands/scan/generate-report.mts'
3132

33+
const { mockGenerateReport, mockLogger, mockGetSpinner } = vi.hoisted(() => ({
34+
mockGenerateReport: vi.fn(),
35+
mockLogger: { log: vi.fn(), fail: vi.fn(), dir: vi.fn() },
36+
mockGetSpinner: vi.fn(() => ({
37+
start: vi.fn(),
38+
successAndStop: vi.fn(),
39+
})),
40+
}))
41+
42+
vi.mock('../../../../src/commands/scan/generate-report.mts', async () => {
43+
const actual =
44+
await vi.importActual<
45+
typeof import('../../../../src/commands/scan/generate-report.mts')
46+
>('../../../../src/commands/scan/generate-report.mts')
47+
return {
48+
...actual,
49+
generateReport: mockGenerateReport,
50+
}
51+
})
52+
53+
vi.mock('@socketsecurity/lib-internal/logger', () => ({
54+
getDefaultLogger: () => mockLogger,
55+
}))
56+
57+
vi.mock('@socketsecurity/lib-internal/constants/process', () => ({
58+
getSpinner: mockGetSpinner,
59+
}))
60+
3261
describe('output-scan-report', () => {
3362
describe('toJsonReport', () => {
3463
it('should be able to generate a healthy json report', () => {
@@ -159,6 +188,71 @@ describe('output-scan-report', () => {
159188
`)
160189
})
161190
})
191+
192+
describe('outputScanReport exit code behavior', () => {
193+
const originalExitCode = process.exitCode
194+
195+
beforeEach(() => {
196+
process.exitCode = undefined
197+
vi.clearAllMocks()
198+
})
199+
200+
afterEach(() => {
201+
process.exitCode = originalExitCode
202+
})
203+
204+
it('sets exit code to 1 when report is unhealthy', async () => {
205+
mockGenerateReport.mockReturnValue({
206+
ok: true,
207+
data: getUnhealthyReport(),
208+
})
209+
210+
await outputScanReport(
211+
{
212+
ok: true,
213+
data: { scan: [], securityPolicy: {} },
214+
} as any,
215+
{
216+
orgSlug: 'test-org',
217+
scanId: 'test-scan',
218+
includeLicensePolicy: false,
219+
outputKind: 'json',
220+
filepath: '-',
221+
fold: 'none',
222+
reportLevel: 'error',
223+
short: false,
224+
},
225+
)
226+
227+
expect(process.exitCode).toBe(1)
228+
})
229+
230+
it('does not set exit code when report is healthy', async () => {
231+
mockGenerateReport.mockReturnValue({
232+
ok: true,
233+
data: getHealthyReport(),
234+
})
235+
236+
await outputScanReport(
237+
{
238+
ok: true,
239+
data: { scan: [], securityPolicy: {} },
240+
} as any,
241+
{
242+
orgSlug: 'test-org',
243+
scanId: 'test-scan',
244+
includeLicensePolicy: false,
245+
outputKind: 'json',
246+
filepath: '-',
247+
fold: 'none',
248+
reportLevel: 'error',
249+
short: false,
250+
},
251+
)
252+
253+
expect(process.exitCode).toBeUndefined()
254+
})
255+
})
162256
})
163257

164258
function getHealthyReport(): ScanReport {

0 commit comments

Comments
 (0)