From 73ea510762152dc1904fb0029b3c55a1bba346e9 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Mon, 27 Jun 2022 10:17:28 +0200 Subject: [PATCH] Update unit-tests for cache-save.ts file, add coverage option to Jest --- __tests__/cache-save.test.ts | 57 ++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/__tests__/cache-save.test.ts b/__tests__/cache-save.test.ts index 82db219..2ce225a 100644 --- a/__tests__/cache-save.test.ts +++ b/__tests__/cache-save.test.ts @@ -294,6 +294,63 @@ describe('run', () => { ); expect(setFailedSpy).not.toHaveBeenCalled(); }); + + it('save with -1 cacheId , should not fail workflow', async () => { + inputs['cache'] = 'npm'; + getStateSpy.mockImplementation((name: string) => { + if (name === State.CacheMatchedKey) { + return npmFileHash; + } else { + return yarnFileHash; + } + }); + getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/npm`); + saveCacheSpy.mockImplementation(() => { + return -1; + }); + + await run(); + + expect(getInputSpy).toHaveBeenCalled(); + expect(getStateSpy).toHaveBeenCalledTimes(2); + expect(getCommandOutputSpy).toHaveBeenCalledTimes(1); + expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`); + expect(infoSpy).not.toHaveBeenCalledWith( + `Cache hit occurred on the primary key ${npmFileHash}, not saving cache.` + ); + expect(saveCacheSpy).toHaveBeenCalled(); + expect(infoSpy).not.toHaveBeenLastCalledWith( + `Cache saved with the key: ${yarnFileHash}` + ); + expect(setFailedSpy).not.toHaveBeenCalled(); + }); + + it('saves with error from toolkit, should fail workflow', async () => { + inputs['cache'] = 'npm'; + getStateSpy.mockImplementation((name: string) => { + if (name === State.CacheMatchedKey) { + return npmFileHash; + } else { + return yarnFileHash; + } + }); + getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/npm`); + saveCacheSpy.mockImplementation(() => { + throw new cache.ValidationError('Validation failed'); + }); + + await run(); + + expect(getInputSpy).toHaveBeenCalled(); + expect(getStateSpy).toHaveBeenCalledTimes(2); + expect(getCommandOutputSpy).toHaveBeenCalledTimes(1); + expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`); + expect(infoSpy).not.toHaveBeenCalledWith( + `Cache hit occurred on the primary key ${npmFileHash}, not saving cache.` + ); + expect(saveCacheSpy).toHaveBeenCalled(); + expect(setFailedSpy).toHaveBeenCalled(); + }); }); afterEach(() => { diff --git a/package.json b/package.json index 9dabb69..1059509 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "ncc build -o dist/setup src/setup-node.ts && ncc build -o dist/cache-save src/cache-save.ts", "format": "prettier --write **/*.ts", "format-check": "prettier --check **/*.ts", - "test": "jest", + "test": "jest --coverage", "pre-checkin": "npm run format && npm run build && npm test" }, "repository": {