Skip to content

Commit

Permalink
Update unit-tests for cache-save.ts file, add coverage option to Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanZosimov committed Jun 27, 2022
1 parent eaead35 commit 73ea510
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
57 changes: 57 additions & 0 deletions __tests__/cache-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 73ea510

Please sign in to comment.