mirror of
				https://github.com/actions/setup-node.git
				synced 2025-11-04 13:29:12 +08:00 
			
		
		
		
	Update unit-tests for cache-save.ts file, add coverage option to Jest
This commit is contained in:
		@@ -294,6 +294,63 @@ describe('run', () => {
 | 
				
			|||||||
      );
 | 
					      );
 | 
				
			||||||
      expect(setFailedSpy).not.toHaveBeenCalled();
 | 
					      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(() => {
 | 
					  afterEach(() => {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,7 @@
 | 
				
			|||||||
    "build": "ncc build -o dist/setup src/setup-node.ts && ncc build -o dist/cache-save src/cache-save.ts",
 | 
					    "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": "prettier --write **/*.ts",
 | 
				
			||||||
    "format-check": "prettier --check **/*.ts",
 | 
					    "format-check": "prettier --check **/*.ts",
 | 
				
			||||||
    "test": "jest",
 | 
					    "test": "jest --coverage",
 | 
				
			||||||
    "pre-checkin": "npm run format && npm run build && npm test"
 | 
					    "pre-checkin": "npm run format && npm run build && npm test"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "repository": {
 | 
					  "repository": {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user