diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index f14c044..a493633 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -18,159 +18,151 @@ import { } from './caching'; import * as cachingUtils from './utils/caching.utils'; -describe('caching', (): void => { - describe('cacheRecord', (): void => { - let cacheKeySpy: MockInstance< - [prefix: string, hashData: Record], - string - >; - - const prefix = 'warcraft'; - const cacheKey = 'ragnaros'; - const hashData = { race: 'orc' }; - const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' }; - const targetsCache = { ragnaros: cacheItem }; - - beforeEach((): void => { - cacheKeySpy = vi - .spyOn(cachingUtils, 'cacheKey') - .mockReturnValue(cacheKey); - }); +describe('cacheRecord', (): void => { + let cacheKeySpy: MockInstance< + [prefix: string, hashData: Record], + string + >; + + const prefix = 'warcraft'; + const cacheKey = 'ragnaros'; + const hashData = { race: 'orc' }; + const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' }; + const targetsCache = { ragnaros: cacheItem }; + + beforeEach((): void => { + cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(cacheKey); + }); - afterEach((): void => { - cacheKeySpy.mockRestore(); - }); + afterEach((): void => { + cacheKeySpy.mockRestore(); + }); - describe('getCacheRecord', (): void => { - it('should call cacheKey once with correct arguments', (): void => { - getCacheRecord(targetsCache, prefix, hashData); - expect(cacheKeySpy).toHaveBeenCalledTimes(1); - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); - }); + describe('getCacheRecord', (): void => { + it('should call cacheKey once with correct arguments', (): void => { + getCacheRecord(targetsCache, prefix, hashData); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + }); - it('should return the correct cache record if there is a cache hit', (): void => { - expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual( - cacheItem - ); - }); + it('should return the correct cache record if there is a cache hit', (): void => { + expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual(cacheItem); + }); - it('should return undefined if there is no cache hit', (): void => { - cacheKeySpy.mockReturnValue('non-existent-key'); - expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined(); - }); + it('should return undefined if there is no cache hit', (): void => { + cacheKeySpy.mockReturnValue('non-existent-key'); + expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined(); }); + }); - describe('setCacheRecord', (): void => { - const cacheData = { thunderfury: 'Blood of Sylvanas' }; + describe('setCacheRecord', (): void => { + const cacheData = { thunderfury: 'Blood of Sylvanas' }; - it('should call cacheKey once with correct arguments', (): void => { - setCacheRecord(targetsCache, prefix, hashData, cacheData); - expect(cacheKeySpy).toHaveBeenCalledTimes(1); - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); - }); + it('should call cacheKey once with correct arguments', (): void => { + setCacheRecord(targetsCache, prefix, hashData, cacheData); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + }); - it('should set the cache record, and return it', (): void => { - expect(setCacheRecord(targetsCache, prefix, hashData, cacheData)).toBe( - cacheData - ); - expect(targetsCache).toHaveProperty(cacheKey, cacheData); - }); + it('should set the cache record, and return it', (): void => { + expect(setCacheRecord(targetsCache, prefix, hashData, cacheData)).toBe( + cacheData + ); + expect(targetsCache).toHaveProperty(cacheKey, cacheData); + }); - it('should update existing cache data, and return it', (): void => { - const recordToUpdate = { thunderfury: 'Soul of Sylvanas' }; - setCacheRecord(targetsCache, prefix, hashData, cacheData); + it('should update existing cache data, and return it', (): void => { + const recordToUpdate = { thunderfury: 'Soul of Sylvanas' }; + setCacheRecord(targetsCache, prefix, hashData, cacheData); - expect( - setCacheRecord(targetsCache, prefix, hashData, recordToUpdate) - ).toBe(recordToUpdate); - expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); - }); + expect( + setCacheRecord(targetsCache, prefix, hashData, recordToUpdate) + ).toBe(recordToUpdate); + expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); }); }); +}); - describe('readTargetsCache', (): void => { - const path = 'azeroth'; - let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; - let readJsonFileSpy: MockInstance< - [path: string, options?: JsonReadOptions], - object - >; - - beforeEach((): void => { - existsSyncSpy = vi - .spyOn(nodeFs, 'existsSync') - .mockImplementation((): boolean => true); - readJsonFileSpy = vi - .spyOn(nxDevKit, 'readJsonFile') - .mockImplementation( - (): Record> => { - return MOCK_TARGETS_CACHE; - } - ); - process.env.NX_CACHE_PROJECT_GRAPH = 'true'; - }); +describe('readTargetsCache', (): void => { + const path = 'azeroth'; + let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; + let readJsonFileSpy: MockInstance< + [path: string, options?: JsonReadOptions], + object + >; + + beforeEach((): void => { + existsSyncSpy = vi + .spyOn(nodeFs, 'existsSync') + .mockImplementation((): boolean => true); + readJsonFileSpy = vi + .spyOn(nxDevKit, 'readJsonFile') + .mockImplementation((): Record> => { + return MOCK_TARGETS_CACHE; + }); + process.env.NX_CACHE_PROJECT_GRAPH = 'true'; + }); - afterEach((): void => { - existsSyncSpy.mockRestore(); - readJsonFileSpy.mockRestore(); - delete process.env.NX_CACHE_PROJECT_GRAPH; - }); + afterEach((): void => { + existsSyncSpy.mockRestore(); + readJsonFileSpy.mockRestore(); + delete process.env.NX_CACHE_PROJECT_GRAPH; + }); - it('should call existSync once with correct argument', (): void => { - readTargetsCache(path); - expect(existsSyncSpy).toHaveBeenCalledWith(path); - expect(existsSyncSpy).toHaveBeenCalledTimes(1); - }); + it('should call existSync once with correct argument', (): void => { + readTargetsCache(path); + expect(existsSyncSpy).toHaveBeenCalledWith(path); + expect(existsSyncSpy).toHaveBeenCalledTimes(1); + }); - it('should call readJsonFile once with correct argument', (): void => { - readTargetsCache(path); - expect(readJsonFileSpy).toHaveBeenCalledWith(path); - expect(readJsonFileSpy).toHaveBeenCalledTimes(1); - }); + it('should call readJsonFile once with correct argument', (): void => { + readTargetsCache(path); + expect(readJsonFileSpy).toHaveBeenCalledWith(path); + expect(readJsonFileSpy).toHaveBeenCalledTimes(1); + }); - it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { - expect(readTargetsCache(path)).toEqual(MOCK_TARGETS_CACHE); - }); + it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { + expect(readTargetsCache(path)).toEqual(MOCK_TARGETS_CACHE); + }); - it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { - process.env.NX_CACHE_PROJECT_GRAPH = 'false'; - expect(readTargetsCache(path)).toEqual({}); - }); + it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { + process.env.NX_CACHE_PROJECT_GRAPH = 'false'; + expect(readTargetsCache(path)).toEqual({}); + }); - it('should return empty object if existsSync returns false', (): void => { - existsSyncSpy.mockImplementation((): boolean => false); - expect(readTargetsCache(path)).toEqual({}); - }); + it('should return empty object if existsSync returns false', (): void => { + existsSyncSpy.mockImplementation((): boolean => false); + expect(readTargetsCache(path)).toEqual({}); + }); - it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH = false', (): void => { - existsSyncSpy.mockImplementation((): boolean => false); - process.env.NX_CACHE_PROJECT_GRAPH = 'false'; - expect(readTargetsCache(path)).toEqual({}); - }); + it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH = false', (): void => { + existsSyncSpy.mockImplementation((): boolean => false); + process.env.NX_CACHE_PROJECT_GRAPH = 'false'; + expect(readTargetsCache(path)).toEqual({}); }); +}); - describe('writeTargetsToCache', (): void => { - const writeJsonFile = vi - .spyOn(nxDevKit, 'writeJsonFile') - .mockImplementation((): string => 'dont write to file :D'); - const path = 'azeroth'; +describe('writeTargetsToCache', (): void => { + const writeJsonFile = vi + .spyOn(nxDevKit, 'writeJsonFile') + .mockImplementation((): string => 'dont write to file :D'); + const path = 'azeroth'; - afterEach((): void => { - writeJsonFile.mockRestore(); - delete process.env.NX_CACHE_PROJECT_GRAPH; - }); + afterEach((): void => { + writeJsonFile.mockRestore(); + delete process.env.NX_CACHE_PROJECT_GRAPH; + }); - it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { - process.env.NX_CACHE_PROJECT_GRAPH = 'true'; - writeTargetsToCache(path, MOCK_TARGETS_CACHE); - expect(writeJsonFile).toHaveBeenCalledWith(path, MOCK_TARGETS_CACHE); - expect(writeJsonFile).toHaveBeenCalledTimes(1); - }); + it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { + process.env.NX_CACHE_PROJECT_GRAPH = 'true'; + writeTargetsToCache(path, MOCK_TARGETS_CACHE); + expect(writeJsonFile).toHaveBeenCalledWith(path, MOCK_TARGETS_CACHE); + expect(writeJsonFile).toHaveBeenCalledTimes(1); + }); - it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => { - process.env.NX_CACHE_PROJECT_GRAPH = 'false'; - writeTargetsToCache(path, MOCK_TARGETS_CACHE); - expect(writeJsonFile).toHaveBeenCalledTimes(0); - }); + it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => { + process.env.NX_CACHE_PROJECT_GRAPH = 'false'; + writeTargetsToCache(path, MOCK_TARGETS_CACHE); + expect(writeJsonFile).toHaveBeenCalledTimes(0); }); }); diff --git a/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts b/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts index cf41764..b3e1642 100644 --- a/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts @@ -13,6 +13,7 @@ describe('cacheKey', (): void => { const hashData = { race: 'orc' }; const hashObjectReturnValue = '123456789'; let hashObjectSpy: MockInstance<[obj: object], string>; + const regex = /^[a-zA-Z]+-\d+$/; beforeEach((): void => { hashObjectSpy = vi @@ -23,34 +24,27 @@ describe('cacheKey', (): void => { hashObjectSpy.mockRestore(); }); - describe('prefix', (): void => { - it('should return cache key with unmodified prefix', (): void => { - expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`); - }); + it('should return cache key with unmodified prefix', (): void => { + expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`); }); - describe('format', (): void => { - const regex = /^[a-zA-Z]+-\d+$/; - it('should return a value in the format "string-numbers"', (): void => { - expect(cacheKey(prefix, hashData)).toMatch(regex); - }); + it('should return a value in the format "string-numbers"', (): void => { + expect(cacheKey(prefix, hashData)).toMatch(regex); }); - describe('hashed object', (): void => { - it('should call hashObject once, and with correct argument', (): void => { - cacheKey(prefix, hashData); - expect(hashObjectSpy).toHaveBeenCalledTimes(1); - expect(hashObjectSpy).toHaveBeenCalledWith(hashData); - }); + it('should call hashObject once, and with correct argument', (): void => { + cacheKey(prefix, hashData); + expect(hashObjectSpy).toHaveBeenCalledTimes(1); + expect(hashObjectSpy).toHaveBeenCalledWith(hashData); + }); - it('should return cache key, when hashData is empty', (): void => { - expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`); - }); + it('should return cache key, when hashData is empty', (): void => { + expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`); + }); - it('should return cache key, when hashData is NOT empty', (): void => { - expect(cacheKey(prefix, hashData)).toBe( - `${prefix}-${hashObjectReturnValue}` - ); - }); + it('should return cache key, when hashData is NOT empty', (): void => { + expect(cacheKey(prefix, hashData)).toBe( + `${prefix}-${hashObjectReturnValue}` + ); }); });