From 0cff2d09f265133c87e4d44d0ea9341fa5fc26a7 Mon Sep 17 00:00:00 2001 From: Joel Rudsberg Date: Fri, 29 Nov 2024 14:22:08 +0100 Subject: [PATCH] Refactor sbom test --- __tests__/sbom.test.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/__tests__/sbom.test.ts b/__tests__/sbom.test.ts index b977ac6..70bb4b5 100644 --- a/__tests__/sbom.test.ts +++ b/__tests__/sbom.test.ts @@ -5,9 +5,17 @@ import {join} from 'path' import {tmpdir} from 'os' import {mkdtempSync, writeFileSync, rmSync} from 'fs' -// Module level mock jest.mock('@actions/glob') +// sbom.ts uses glob to find the SBOM file +// This helper function mocks the glob module to return 'files' +function mockGlobResult(files: string[]) { + const mockCreate = jest.fn().mockResolvedValue({ + glob: jest.fn().mockResolvedValue(files) + }) + ;(glob.create as jest.Mock).mockImplementation(mockCreate) +} + describe('sbom feature', () => { let spyInfo: jest.SpyInstance> let spyWarning: jest.SpyInstance> @@ -101,10 +109,7 @@ describe('sbom feature', () => { const sbomPath = join(workspace, 'test.sbom.json') writeFileSync(sbomPath, JSON.stringify(sampleSBOM, null, 2)) - const mockCreate = jest.fn().mockResolvedValue({ - glob: jest.fn().mockResolvedValue([sbomPath]) - }) - ;(glob.create as jest.Mock).mockImplementation(mockCreate) + mockGlobResult([sbomPath]) await processSBOM() @@ -120,11 +125,7 @@ describe('sbom feature', () => { setUpSBOMSupport() spyInfo.mockClear() - // Mock glob to return empty array (no files found) - const mockCreate = jest.fn().mockResolvedValue({ - glob: jest.fn().mockResolvedValue([]) - }) - ;(glob.create as jest.Mock).mockImplementation(mockCreate) + mockGlobResult([]) await processSBOM() expect(spyWarning).toHaveBeenCalledWith(