From 66ace9353c1521d8aec19e62a413cb79ab09c05e Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Sun, 8 Dec 2024 11:22:41 +0100 Subject: [PATCH 01/31] tests: add cache key unit tests --- .../src/plugin/caching.unit-test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 projects/nx-verdaccio/src/plugin/caching.unit-test.ts diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts new file mode 100644 index 00000000..a6b75e11 --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -0,0 +1,59 @@ +import { afterEach, describe, expect, type MockInstance } from 'vitest'; +import { cacheKey } from './caching'; +import * as fileHasher from 'nx/src/hasher/file-hasher'; + + +describe('cacheKey', (): void => { + const prefix = 'warcraft'; + const hashData = { race: 'orc' }; + let hashObjectSpy: MockInstance<[obj: object], string>; + + describe('hashed object', (): void => { + beforeEach((): void => { + hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); + }); + afterEach((): void => { + hashObjectSpy.mockRestore(); + }); + it('should return cache key with hashed object when it is empty', (): void => { + // {} = 3244421341483603138 + expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); + }); + it('should return cache key with hashed object when it is NOT empty', (): void => { + // { race: 'orc' } = 9075797468634534731 + expect(cacheKey(prefix, hashData)).toBe(`${prefix}-5048043832971198124`); + }); + it('should call hashObject with the correct data', () => { + const hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); + + const result = cacheKey(prefix, hashData); + + expect(hashObjectSpy).toHaveBeenCalledTimes(1); + expect(hashObjectSpy).toHaveBeenCalledWith(hashData); + expect(result).toContain(prefix); + }); + it('should return unmodified hashObject return value', (): void => { + const hashObjectSpy = vi + .spyOn(fileHasher, 'hashObject') + .mockImplementation((): string => 'mocked-hash'); + const result = cacheKey(prefix, hashData); + + expect(result).toBe(`${prefix}-mocked-hash`); + expect(hashObjectSpy).toHaveBeenCalledTimes(1); + expect(hashObjectSpy).toHaveBeenCalledWith(hashData); + }); + }); + describe('prefix', (): void => { + it('should return cache key with unmodified prefix', (): void => { + // {} = 3244421341483603138 + expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); + }); + }); + describe('format', (): void => { + const regex = /^[a-zA-Z]+-\d+$/; + it('should return a value in the format "string-numbers"', (): void => { + const result = cacheKey(prefix, hashData); + expect(result).toMatch(regex); + }); + }); +}); From df5496733e4d1c5a07ccebee0b8bbcca1093ee98 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Mon, 9 Dec 2024 17:11:00 +0100 Subject: [PATCH 02/31] tests: add get cache test, move cacheKey to utils --- projects/nx-verdaccio/src/plugin/caching.ts | 6 +- .../src/plugin/caching.unit-test.ts | 91 ++++++++----------- .../src/plugin/utils/caching.utils.ts | 5 + .../plugin/utils/caching.utils.unit-test.ts | 60 ++++++++++++ 4 files changed, 105 insertions(+), 57 deletions(-) create mode 100644 projects/nx-verdaccio/src/plugin/utils/caching.utils.ts create mode 100644 projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts diff --git a/projects/nx-verdaccio/src/plugin/caching.ts b/projects/nx-verdaccio/src/plugin/caching.ts index bef0bb18..8bec1df2 100644 --- a/projects/nx-verdaccio/src/plugin/caching.ts +++ b/projects/nx-verdaccio/src/plugin/caching.ts @@ -1,14 +1,10 @@ -import { hashObject } from 'nx/src/hasher/file-hasher'; import { type ProjectConfiguration, readJsonFile, writeJsonFile, } from '@nx/devkit'; import { existsSync } from 'node:fs'; - -export function cacheKey(prefix: string, hashData: Record) { - return `${prefix}-${hashObject(hashData)}`; -} +import { cacheKey } from './utils/caching.utils'; export function getCacheRecord( targetsCache: Record, diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index a6b75e11..7bc5eb2e 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -1,59 +1,46 @@ -import { afterEach, describe, expect, type MockInstance } from 'vitest'; -import { cacheKey } from './caching'; -import * as fileHasher from 'nx/src/hasher/file-hasher'; +import { afterEach, beforeEach, describe, expect } from 'vitest'; +import * as moduleUnderTest from './caching'; +import * as cachingUtils from './utils/caching.utils'; - -describe('cacheKey', (): void => { +describe('getCacheRecord', () => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; - let hashObjectSpy: MockInstance<[obj: object], string>; - - describe('hashed object', (): void => { - beforeEach((): void => { - hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); - }); - afterEach((): void => { - hashObjectSpy.mockRestore(); - }); - it('should return cache key with hashed object when it is empty', (): void => { - // {} = 3244421341483603138 - expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); - }); - it('should return cache key with hashed object when it is NOT empty', (): void => { - // { race: 'orc' } = 9075797468634534731 - expect(cacheKey(prefix, hashData)).toBe(`${prefix}-5048043832971198124`); - }); - it('should call hashObject with the correct data', () => { - const hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); - - const result = cacheKey(prefix, hashData); - - expect(hashObjectSpy).toHaveBeenCalledTimes(1); - expect(hashObjectSpy).toHaveBeenCalledWith(hashData); - expect(result).toContain(prefix); - }); - it('should return unmodified hashObject return value', (): void => { - const hashObjectSpy = vi - .spyOn(fileHasher, 'hashObject') - .mockImplementation((): string => 'mocked-hash'); - const result = cacheKey(prefix, hashData); - - expect(result).toBe(`${prefix}-mocked-hash`); - expect(hashObjectSpy).toHaveBeenCalledTimes(1); - expect(hashObjectSpy).toHaveBeenCalledWith(hashData); - }); + let cacheKeySpy: ReturnType; + const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' }; + const targetsCache = { 'ragnaros': cacheItem }; + + beforeEach((): void => { + cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey'); + + }); + afterEach((): void => { + cacheKeySpy.mockRestore(); + }); + + it('should call cacheKey with the correct arguments', () => { + + cacheKeySpy.mockReturnValue('ragnaros'); + moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); }); - describe('prefix', (): void => { - it('should return cache key with unmodified prefix', (): void => { - // {} = 3244421341483603138 - expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); - }); + + it('should return the correct record if cacheKey matches', () => { + cacheKeySpy.mockReturnValue('ragnaros'); + + const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + + expect(result).toEqual(cacheItem); }); - describe('format', (): void => { - const regex = /^[a-zA-Z]+-\d+$/; - it('should return a value in the format "string-numbers"', (): void => { - const result = cacheKey(prefix, hashData); - expect(result).toMatch(regex); - }); + + it('should return undefined if no matching key exists in the cache', () => { + cacheKeySpy.mockReturnValue('non-existent-key'); + + const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + + expect(result).toBeUndefined(); }); }); + + diff --git a/projects/nx-verdaccio/src/plugin/utils/caching.utils.ts b/projects/nx-verdaccio/src/plugin/utils/caching.utils.ts new file mode 100644 index 00000000..34386ca0 --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/utils/caching.utils.ts @@ -0,0 +1,5 @@ +import { hashObject } from 'nx/src/hasher/file-hasher'; + +export function cacheKey(prefix: string, hashData: Record) { + return `${prefix}-${hashObject(hashData)}`; +} 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 new file mode 100644 index 00000000..13aa6274 --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts @@ -0,0 +1,60 @@ +import { afterEach, describe, expect, type MockInstance } from 'vitest'; +import * as fileHasher from 'nx/src/hasher/file-hasher'; +import { cacheKey } from './caching.utils'; + +describe('cacheKey', (): void => { + const prefix = 'warcraft'; + const hashData = { race: 'orc' }; + let hashObjectSpy: MockInstance<[obj: object], string>; + + describe('hashed object', (): void => { + beforeEach((): void => { + hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); + }); + afterEach((): void => { + hashObjectSpy.mockRestore(); + }); + it('should return cache key with hashed object when it is empty', (): void => { + // {} = 3244421341483603138 + expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); + }); + it('should return cache key with hashed object when it is NOT empty', (): void => { + // { race: 'orc' } = 9075797468634534731 + expect(cacheKey(prefix, hashData)).toBe(`${prefix}-5048043832971198124`); + }); + it('should call hashObject with the correct data', () => { + const hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); + + const result = cacheKey(prefix, hashData); + + expect(hashObjectSpy).toHaveBeenCalledTimes(1); + expect(hashObjectSpy).toHaveBeenCalledWith(hashData); + expect(result).toContain(prefix); + }); + it('should return unmodified hashObject return value', (): void => { + const hashObjectSpy = vi + .spyOn(fileHasher, 'hashObject') + .mockImplementation((): string => 'mocked-hash'); + const result = cacheKey(prefix, hashData); + + expect(result).toBe(`${prefix}-mocked-hash`); + expect(hashObjectSpy).toHaveBeenCalledTimes(1); + expect(hashObjectSpy).toHaveBeenCalledWith(hashData); + }); + }); + describe('prefix', (): void => { + it('should return cache key with unmodified prefix', (): void => { + // {} = 3244421341483603138 + expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); + }); + }); + describe('format', (): void => { + const regex = /^[a-zA-Z]+-\d+$/; + it('should return a value in the format "string-numbers"', (): void => { + const result = cacheKey(prefix, hashData); + expect(result).toMatch(regex); + }); + }); +}); + + From 7daec0ccd6b687acc8604c70cd127addeb954a48 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Wed, 11 Dec 2024 18:24:59 +0100 Subject: [PATCH 03/31] tests: set cache record --- projects/nx-verdaccio/src/plugin/caching.ts | 4 +- .../src/plugin/caching.unit-test.ts | 70 ++++++++++++++----- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.ts b/projects/nx-verdaccio/src/plugin/caching.ts index 8bec1df2..4a301122 100644 --- a/projects/nx-verdaccio/src/plugin/caching.ts +++ b/projects/nx-verdaccio/src/plugin/caching.ts @@ -10,7 +10,7 @@ export function getCacheRecord( targetsCache: Record, prefix: string, hashData: Record -) { +): T { const targetCacheKey = cacheKey(prefix, hashData); if (targetsCache[targetCacheKey]) { @@ -24,7 +24,7 @@ export function setCacheRecord( prefix: string, hashData: Record, cacheData: T -) { +): T { const targetCacheKey = cacheKey(prefix, hashData); return (targetsCache[targetCacheKey] = cacheData); diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 7bc5eb2e..2660cae1 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -1,8 +1,10 @@ import { afterEach, beforeEach, describe, expect } from 'vitest'; import * as moduleUnderTest from './caching'; import * as cachingUtils from './utils/caching.utils'; +import { setCacheRecord } from './caching'; +import { cacheKey } from './utils/caching.utils'; -describe('getCacheRecord', () => { +describe('caching', () => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; let cacheKeySpy: ReturnType; @@ -17,30 +19,66 @@ describe('getCacheRecord', () => { cacheKeySpy.mockRestore(); }); - it('should call cacheKey with the correct arguments', () => { + describe('getCacheRecord', () => { + it('should call cacheKey with the correct arguments', () => { - cacheKeySpy.mockReturnValue('ragnaros'); - moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + cacheKeySpy.mockReturnValue('ragnaros'); + moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); - expect(cacheKeySpy).toHaveBeenCalledTimes(1); - }); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + }); + + it('should return the correct record if cacheKey matches', () => { + cacheKeySpy.mockReturnValue('ragnaros'); + + const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); - it('should return the correct record if cacheKey matches', () => { - cacheKeySpy.mockReturnValue('ragnaros'); + expect(result).toEqual(cacheItem); + }); - const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + it('should return undefined if no matching key exists in the cache', () => { + cacheKeySpy.mockReturnValue('non-existent-key'); - expect(result).toEqual(cacheItem); + const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + + expect(result).toBeUndefined(); + }); }); - it('should return undefined if no matching key exists in the cache', () => { - cacheKeySpy.mockReturnValue('non-existent-key'); + describe('setCacheRecord', (): void => { + const cacheData = { thunderfury: 'Blood of Sylvanas' }; + it('should call cacheKey with the correct arguments', (): void => { + cacheKeySpy.mockReturnValue('ragnaros'); + setCacheRecord(targetsCache, prefix, hashData, cacheData); + + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + }); + + it('should set a cache record and return the cached data', () => { + const result = setCacheRecord(targetsCache, prefix, hashData, cacheData); - const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + const expectedKey = cacheKey(prefix, hashData); + expect(targetsCache).toHaveProperty(expectedKey, cacheData); + expect(result).toBe(cacheData); + }); - expect(result).toBeUndefined(); + it('should overwrite existing cache data with the same key', () => { + const updated = { thunderfury: 'Soul of Sylvanas' }; + + cacheKeySpy.mockReturnValue('ragnaros'); + + setCacheRecord(targetsCache, prefix, hashData, cacheData); + const result = setCacheRecord(targetsCache, prefix, hashData, updated); + + const expectedKey = cacheKey(prefix, hashData); + expect(targetsCache).toHaveProperty(expectedKey, updated); + expect(result).toBe(updated); + }); }); -}); + +}) + From 552872513f234d41cdd02a9f6aeb788cab9b2f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Roma=C5=84ski?= Date: Thu, 12 Dec 2024 07:47:24 +0100 Subject: [PATCH 04/31] tests: read target cache beginning --- projects/nx-verdaccio/src/plugin/caching.ts | 2 ++ .../src/plugin/caching.unit-test.ts | 31 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.ts b/projects/nx-verdaccio/src/plugin/caching.ts index 66a68024..d8e8c05d 100644 --- a/projects/nx-verdaccio/src/plugin/caching.ts +++ b/projects/nx-verdaccio/src/plugin/caching.ts @@ -33,6 +33,8 @@ export function setCacheRecord( export function readTargetsCache( cachePath: string ): Record> { + // that part is ready + const mockedExistsSync = existsSync(cachePath); return process.env.NX_CACHE_PROJECT_GRAPH !== 'false' && existsSync(cachePath) ? readJsonFile(cachePath) : {}; diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 2660cae1..b47ed38f 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -1,7 +1,8 @@ import { afterEach, beforeEach, describe, expect } from 'vitest'; import * as moduleUnderTest from './caching'; import * as cachingUtils from './utils/caching.utils'; -import { setCacheRecord } from './caching'; +import * as nodeFs from 'node:fs'; +import { readTargetsCache, setCacheRecord } from './caching'; import { cacheKey } from './utils/caching.utils'; describe('caching', () => { @@ -78,7 +79,31 @@ describe('caching', () => { }); }); -}) - + //export function readTargetsCache( + // cachePath: string + // ): Record> { + // return process.env.NX_CACHE_PROJECT_GRAPH !== 'false' && existsSync(cachePath) + // ? readJsonFile(cachePath) + // : {}; + // } + describe('readTargetsCache', (): void => { + const existsSyncSpy = vi + .spyOn(nodeFs, 'existsSync') + .mockImplementation((): boolean => true); + + // beforeEach((): void => { + // existsSyncSpy = vi.spyOn(cachingUtils, 'cacheKey'); + // }); + + afterEach((): void => { + existsSyncSpy.mockRestore(); + }); + it('should call cacheKey with the correct arguments', (): void => { + readTargetsCache('test'); + expect(existsSyncSpy).toHaveBeenCalledWith('test'); + expect(existsSyncSpy).toHaveBeenCalledTimes(1); + }); + }); +}) From 89baebf97aaa0b3147f9a3e9f034e7165bd09d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Roma=C5=84ski?= Date: Thu, 12 Dec 2024 10:27:27 +0100 Subject: [PATCH 05/31] tests: read target cache - mock read json file --- projects/nx-verdaccio/src/plugin/caching.ts | 4 +- .../src/plugin/caching.unit-test.ts | 76 ++++++++++++++++++- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.ts b/projects/nx-verdaccio/src/plugin/caching.ts index d8e8c05d..54b9c200 100644 --- a/projects/nx-verdaccio/src/plugin/caching.ts +++ b/projects/nx-verdaccio/src/plugin/caching.ts @@ -33,10 +33,8 @@ export function setCacheRecord( export function readTargetsCache( cachePath: string ): Record> { - // that part is ready - const mockedExistsSync = existsSync(cachePath); return process.env.NX_CACHE_PROJECT_GRAPH !== 'false' && existsSync(cachePath) - ? readJsonFile(cachePath) + ? readJsonFile(cachePath) : {}; } diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index b47ed38f..1e676d09 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -2,8 +2,10 @@ import { afterEach, beforeEach, describe, expect } from 'vitest'; import * as moduleUnderTest from './caching'; import * as cachingUtils from './utils/caching.utils'; import * as nodeFs from 'node:fs'; +import * as nxDevKit from '@nx/devkit'; import { readTargetsCache, setCacheRecord } from './caching'; import { cacheKey } from './utils/caching.utils'; +import { ProjectConfiguration } from '@nx/devkit'; describe('caching', () => { const prefix = 'warcraft'; @@ -87,10 +89,76 @@ describe('caching', () => { // : {}; // } describe('readTargetsCache', (): void => { + afterEach(() => { + existsSyncSpy.mockRestore(); + delete process.env.NX_CACHE_PROJECT_GRAPH; + }); const existsSyncSpy = vi .spyOn(nodeFs, 'existsSync') .mockImplementation((): boolean => true); + const readJsonFileSpy = vi + .spyOn(nxDevKit, 'readJsonFile') + .mockImplementation((): Record> => { + return {'mockKey': mockProjectConfiguration} + }); + + + const mockProjectConfiguration: ProjectConfiguration = { + name: 'mock-project', + root: 'apps/mock-project', + sourceRoot: 'apps/mock-project/src', + projectType: 'application', + tags: ['e2e', 'unit-test'], + implicitDependencies: ['shared-library'], + targets: { + build: { + executor: '@nx/web:build', + options: { + outputPath: 'dist/apps/mock-project', + index: 'apps/mock-project/src/index.html', + main: 'apps/mock-project/src/main.ts', + tsConfig: 'apps/mock-project/tsconfig.app.json', + }, + configurations: { + production: { + fileReplacements: [ + { + replace: 'apps/mock-project/src/environments/environment.ts', + with: 'apps/mock-project/src/environments/environment.prod.ts', + }, + ], + optimization: true, + sourceMap: false, + }, + }, + }, + }, + generators: { + '@nx/react': { + library: { + style: 'scss', + }, + }, + }, + namedInputs: { + default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], + production: ['default', '!{projectRoot}/**/*.test.ts'], + }, + release: { + version: { + generator: '@nx/version', + generatorOptions: { + increment: 'minor', + }, + }, + }, + metadata: { + description: 'This is a mock project for testing.', + }, + }; + + // beforeEach((): void => { // existsSyncSpy = vi.spyOn(cachingUtils, 'cacheKey'); // }); @@ -99,11 +167,13 @@ describe('caching', () => { existsSyncSpy.mockRestore(); }); - it('should call cacheKey with the correct arguments', (): void => { - readTargetsCache('test'); - + it('should call cacheKey with the correct arguments and return from json file', (): void => { + process.env.NX_CACHE_PROJECT_GRAPH = 'true'; + const targetsCacheResult = readTargetsCache('test'); expect(existsSyncSpy).toHaveBeenCalledWith('test'); expect(existsSyncSpy).toHaveBeenCalledTimes(1); + expect(readJsonFileSpy).toHaveBeenCalledTimes(1); + expect(targetsCacheResult).toStrictEqual({'mockKey': mockProjectConfiguration}); }); }); }) From 3e97e607a7c54e958edce7cd8d3aa5fabe130b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Roma=C5=84ski?= Date: Thu, 12 Dec 2024 14:56:56 +0100 Subject: [PATCH 06/31] tests: separate testing of calls from testing of return value --- .../src/plugin/caching.unit-test.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 1e676d09..52d29013 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -159,20 +159,21 @@ describe('caching', () => { }; - // beforeEach((): void => { - // existsSyncSpy = vi.spyOn(cachingUtils, 'cacheKey'); - // }); - - afterEach((): void => { - existsSyncSpy.mockRestore(); + beforeEach((): void => { + process.env.NX_CACHE_PROJECT_GRAPH = 'true'; }); - it('should call cacheKey with the correct arguments and return from json file', (): void => { + it('should call exist sync with the correct arguments', (): void => { process.env.NX_CACHE_PROJECT_GRAPH = 'true'; - const targetsCacheResult = readTargetsCache('test'); + readTargetsCache('test'); expect(existsSyncSpy).toHaveBeenCalledWith('test'); expect(existsSyncSpy).toHaveBeenCalledTimes(1); expect(readJsonFileSpy).toHaveBeenCalledTimes(1); + }); + + it('should return target cache from json file', (): void => { + process.env.NX_CACHE_PROJECT_GRAPH = 'true'; + const targetsCacheResult = readTargetsCache('test'); expect(targetsCacheResult).toStrictEqual({'mockKey': mockProjectConfiguration}); }); }); From 11109d7be8d2688c019fe62ca6f16aff2d712216 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 16:25:42 +0100 Subject: [PATCH 07/31] tests: add negative scenario readTargetsCache --- .../src/plugin/caching.unit-test.ts | 150 ++++++++---------- .../src/plugin/constants.unit-test.ts | 55 +++++++ 2 files changed, 118 insertions(+), 87 deletions(-) create mode 100644 projects/nx-verdaccio/src/plugin/constants.unit-test.ts diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 52d29013..0ea5141a 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -1,22 +1,23 @@ -import { afterEach, beforeEach, describe, expect } from 'vitest'; +import { afterEach, beforeEach, describe, expect, MockInstance } from 'vitest'; import * as moduleUnderTest from './caching'; import * as cachingUtils from './utils/caching.utils'; -import * as nodeFs from 'node:fs'; -import * as nxDevKit from '@nx/devkit'; +import * as nodeFs from 'node:fs'; +import * as nxDevKit from '@nx/devkit'; import { readTargetsCache, setCacheRecord } from './caching'; import { cacheKey } from './utils/caching.utils'; import { ProjectConfiguration } from '@nx/devkit'; +import { JsonReadOptions } from 'nx/src/utils/fileutils'; +import { MOCK_PROJECT_CONFIGURATION } from './constants.unit-test'; describe('caching', () => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; let cacheKeySpy: ReturnType; const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' }; - const targetsCache = { 'ragnaros': cacheItem }; + const targetsCache = { ragnaros: cacheItem }; beforeEach((): void => { cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey'); - }); afterEach((): void => { cacheKeySpy.mockRestore(); @@ -24,7 +25,6 @@ describe('caching', () => { describe('getCacheRecord', () => { it('should call cacheKey with the correct arguments', () => { - cacheKeySpy.mockReturnValue('ragnaros'); moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); @@ -35,7 +35,11 @@ describe('caching', () => { it('should return the correct record if cacheKey matches', () => { cacheKeySpy.mockReturnValue('ragnaros'); - const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + const result = moduleUnderTest.getCacheRecord( + targetsCache, + prefix, + hashData + ); expect(result).toEqual(cacheItem); }); @@ -43,7 +47,11 @@ describe('caching', () => { it('should return undefined if no matching key exists in the cache', () => { cacheKeySpy.mockReturnValue('non-existent-key'); - const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + const result = moduleUnderTest.getCacheRecord( + targetsCache, + prefix, + hashData + ); expect(result).toBeUndefined(); }); @@ -81,89 +89,30 @@ describe('caching', () => { }); }); - //export function readTargetsCache( - // cachePath: string - // ): Record> { - // return process.env.NX_CACHE_PROJECT_GRAPH !== 'false' && existsSync(cachePath) - // ? readJsonFile(cachePath) - // : {}; - // } describe('readTargetsCache', (): void => { - afterEach(() => { - existsSyncSpy.mockRestore(); - delete process.env.NX_CACHE_PROJECT_GRAPH; - }); - const existsSyncSpy = vi - .spyOn(nodeFs, 'existsSync') - .mockImplementation((): boolean => true); - - const readJsonFileSpy = vi - .spyOn(nxDevKit, 'readJsonFile') - .mockImplementation((): Record> => { - return {'mockKey': mockProjectConfiguration} - }); - - - const mockProjectConfiguration: ProjectConfiguration = { - name: 'mock-project', - root: 'apps/mock-project', - sourceRoot: 'apps/mock-project/src', - projectType: 'application', - tags: ['e2e', 'unit-test'], - implicitDependencies: ['shared-library'], - targets: { - build: { - executor: '@nx/web:build', - options: { - outputPath: 'dist/apps/mock-project', - index: 'apps/mock-project/src/index.html', - main: 'apps/mock-project/src/main.ts', - tsConfig: 'apps/mock-project/tsconfig.app.json', - }, - configurations: { - production: { - fileReplacements: [ - { - replace: 'apps/mock-project/src/environments/environment.ts', - with: 'apps/mock-project/src/environments/environment.prod.ts', - }, - ], - optimization: true, - sourceMap: false, - }, - }, - }, - }, - generators: { - '@nx/react': { - library: { - style: 'scss', - }, - }, - }, - namedInputs: { - default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], - production: ['default', '!{projectRoot}/**/*.test.ts'], - }, - release: { - version: { - generator: '@nx/version', - generatorOptions: { - increment: 'minor', - }, - }, - }, - metadata: { - description: 'This is a mock project for testing.', - }, - }; - + let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; + let readJsonFileSpy: MockInstance< + [path: string, options?: JsonReadOptions], + object + >; beforeEach((): void => { + existsSyncSpy = vi.spyOn(nodeFs, 'existsSync'); + readJsonFileSpy = vi.spyOn(nxDevKit, 'readJsonFile'); process.env.NX_CACHE_PROJECT_GRAPH = 'true'; }); + afterEach((): void => { + existsSyncSpy.mockRestore(); + readJsonFileSpy.mockRestore(); + delete process.env.NX_CACHE_PROJECT_GRAPH; + }); + it('should call exist sync with the correct arguments', (): void => { + existsSyncSpy.mockImplementation((): boolean => true); + readJsonFileSpy.mockImplementation((): Record> => { + return {'mockKey': MOCK_PROJECT_CONFIGURATION} + }); process.env.NX_CACHE_PROJECT_GRAPH = 'true'; readTargetsCache('test'); expect(existsSyncSpy).toHaveBeenCalledWith('test'); @@ -172,9 +121,36 @@ describe('caching', () => { }); it('should return target cache from json file', (): void => { + existsSyncSpy.mockImplementation((): boolean => true); + readJsonFileSpy.mockImplementation((): Record> => { + return {'mockKey': MOCK_PROJECT_CONFIGURATION} + }); process.env.NX_CACHE_PROJECT_GRAPH = 'true'; - const targetsCacheResult = readTargetsCache('test'); - expect(targetsCacheResult).toStrictEqual({'mockKey': mockProjectConfiguration}); + const targetsCacheResult = readTargetsCache('test'); + expect(targetsCacheResult).toStrictEqual({ + mockKey: MOCK_PROJECT_CONFIGURATION, + }); + }); + + it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { + existsSyncSpy.mockImplementation((): boolean => true); + process.env.NX_CACHE_PROJECT_GRAPH = 'false'; + const targetsCacheResult = readTargetsCache('test'); + expect(targetsCacheResult).toStrictEqual({}); + }); + + it('should return empty object if existsSync returns false', (): void => { + existsSyncSpy.mockImplementation((): boolean => true); + process.env.NX_CACHE_PROJECT_GRAPH = 'false'; + const targetsCacheResult = readTargetsCache('test'); + expect(targetsCacheResult).toStrictEqual({}); + }); + + 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'; + const targetsCacheResult = readTargetsCache('test'); + expect(targetsCacheResult).toStrictEqual({}); }); }); -}) +}); diff --git a/projects/nx-verdaccio/src/plugin/constants.unit-test.ts b/projects/nx-verdaccio/src/plugin/constants.unit-test.ts new file mode 100644 index 00000000..eb49e013 --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/constants.unit-test.ts @@ -0,0 +1,55 @@ +import { type ProjectConfiguration } from '@nx/devkit'; + +export const MOCK_PROJECT_CONFIGURATION: ProjectConfiguration = { + name: 'mock-project', + root: 'apps/mock-project', + sourceRoot: 'apps/mock-project/src', + projectType: 'application', + tags: ['e2e', 'unit-test'], + implicitDependencies: ['shared-library'], + targets: { + build: { + executor: '@nx/web:build', + options: { + outputPath: 'dist/apps/mock-project', + index: 'apps/mock-project/src/index.html', + main: 'apps/mock-project/src/main.ts', + tsConfig: 'apps/mock-project/tsconfig.app.json', + }, + configurations: { + production: { + fileReplacements: [ + { + replace: 'apps/mock-project/src/environments/environment.ts', + with: 'apps/mock-project/src/environments/environment.prod.ts', + }, + ], + optimization: true, + sourceMap: false, + }, + }, + }, + }, + generators: { + '@nx/react': { + library: { + style: 'scss', + }, + }, + }, + namedInputs: { + default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], + production: ['default', '!{projectRoot}/**/*.test.ts'], + }, + release: { + version: { + generator: '@nx/version', + generatorOptions: { + increment: 'minor', + }, + }, + }, + metadata: { + description: 'This is a mock project for testing.', + }, +}; From be99d62c7b892419421dae1049a680d0e5206546 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 16:38:29 +0100 Subject: [PATCH 08/31] tests: read targets cache optimization, better naming, polishing code --- .../src/plugin/caching.unit-test.ts | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 0ea5141a..8d0f0e60 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -8,8 +8,9 @@ import { cacheKey } from './utils/caching.utils'; import { ProjectConfiguration } from '@nx/devkit'; import { JsonReadOptions } from 'nx/src/utils/fileutils'; import { MOCK_PROJECT_CONFIGURATION } from './constants.unit-test'; +import { PathLike } from 'fs'; -describe('caching', () => { +describe('caching', (): void => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; let cacheKeySpy: ReturnType; @@ -89,7 +90,9 @@ describe('caching', () => { }); }); + // I'M PROUD OF THIS ONE, NOW IT'S TIME FOR REMAINING :) describe('readTargetsCache', (): void => { + const cachePath = 'azeroth'; let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; let readJsonFileSpy: MockInstance< [path: string, options?: JsonReadOptions], @@ -97,8 +100,12 @@ describe('caching', () => { >; beforeEach((): void => { - existsSyncSpy = vi.spyOn(nodeFs, 'existsSync'); - readJsonFileSpy = vi.spyOn(nxDevKit, 'readJsonFile'); + existsSyncSpy = vi.spyOn(nodeFs, 'existsSync') + .mockImplementation((): boolean => true); + readJsonFileSpy = vi.spyOn(nxDevKit, 'readJsonFile') + .mockImplementation((): Record> => { + return { mockKey: MOCK_PROJECT_CONFIGURATION } + }); process.env.NX_CACHE_PROJECT_GRAPH = 'true'; }); @@ -108,48 +115,41 @@ describe('caching', () => { delete process.env.NX_CACHE_PROJECT_GRAPH; }); - it('should call exist sync with the correct arguments', (): void => { - existsSyncSpy.mockImplementation((): boolean => true); - readJsonFileSpy.mockImplementation((): Record> => { - return {'mockKey': MOCK_PROJECT_CONFIGURATION} - }); - process.env.NX_CACHE_PROJECT_GRAPH = 'true'; - readTargetsCache('test'); - expect(existsSyncSpy).toHaveBeenCalledWith('test'); + it('should call existSync once and with correct argument', (): void => { + readTargetsCache(cachePath); + expect(existsSyncSpy).toHaveBeenCalledWith(cachePath); expect(existsSyncSpy).toHaveBeenCalledTimes(1); + }); + + it('should call readJsonFile once and with correct argument', (): void => { + readTargetsCache(cachePath); + expect(readJsonFileSpy).toHaveBeenCalledWith(cachePath); expect(readJsonFileSpy).toHaveBeenCalledTimes(1); }); - it('should return target cache from json file', (): void => { - existsSyncSpy.mockImplementation((): boolean => true); - readJsonFileSpy.mockImplementation((): Record> => { - return {'mockKey': MOCK_PROJECT_CONFIGURATION} - }); - process.env.NX_CACHE_PROJECT_GRAPH = 'true'; - const targetsCacheResult = readTargetsCache('test'); + it('should return target cache if existsSync returns true and NX_CACHE_PROJECT_GRAPH = true', (): void => { + const targetsCacheResult = readTargetsCache(cachePath); expect(targetsCacheResult).toStrictEqual({ mockKey: MOCK_PROJECT_CONFIGURATION, }); }); it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { - existsSyncSpy.mockImplementation((): boolean => true); process.env.NX_CACHE_PROJECT_GRAPH = 'false'; - const targetsCacheResult = readTargetsCache('test'); + const targetsCacheResult = readTargetsCache(cachePath); expect(targetsCacheResult).toStrictEqual({}); }); it('should return empty object if existsSync returns false', (): void => { - existsSyncSpy.mockImplementation((): boolean => true); - process.env.NX_CACHE_PROJECT_GRAPH = 'false'; - const targetsCacheResult = readTargetsCache('test'); + existsSyncSpy.mockImplementation((): boolean => false); + const targetsCacheResult = readTargetsCache(cachePath); expect(targetsCacheResult).toStrictEqual({}); }); 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'; - const targetsCacheResult = readTargetsCache('test'); + const targetsCacheResult = readTargetsCache(cachePath); expect(targetsCacheResult).toStrictEqual({}); }); }); From 17bfd3526dcf2bec33ee8fae7fa049b2ea2c63d1 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 16:55:44 +0100 Subject: [PATCH 09/31] tests: better naming, code polish --- .../src/plugin/caching.unit-test.ts | 148 +++++++++--------- 1 file changed, 71 insertions(+), 77 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 8d0f0e60..3f0c032f 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -1,96 +1,90 @@ -import { afterEach, beforeEach, describe, expect, MockInstance } from 'vitest'; -import * as moduleUnderTest from './caching'; -import * as cachingUtils from './utils/caching.utils'; +import { afterEach, beforeEach, describe, expect, type MockInstance } from 'vitest'; import * as nodeFs from 'node:fs'; import * as nxDevKit from '@nx/devkit'; -import { readTargetsCache, setCacheRecord } from './caching'; -import { cacheKey } from './utils/caching.utils'; -import { ProjectConfiguration } from '@nx/devkit'; -import { JsonReadOptions } from 'nx/src/utils/fileutils'; +import { type ProjectConfiguration } from '@nx/devkit'; +import { type JsonReadOptions } from 'nx/src/utils/fileutils'; +import { getCacheRecord, readTargetsCache, setCacheRecord } from './caching'; +import * as cachingUtils from './utils/caching.utils'; import { MOCK_PROJECT_CONFIGURATION } from './constants.unit-test'; -import { PathLike } from 'fs'; describe('caching', (): void => { - const prefix = 'warcraft'; - const hashData = { race: 'orc' }; - let cacheKeySpy: ReturnType; - const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' }; - const targetsCache = { ragnaros: cacheItem }; - - beforeEach((): void => { - cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey'); - }); - afterEach((): void => { - cacheKeySpy.mockRestore(); - }); + describe('cacheRecord', (): void => { + let cacheKeySpy: MockInstance< + [prefix: string, hashData: Record], + string + >; - describe('getCacheRecord', () => { - it('should call cacheKey with the correct arguments', () => { - cacheKeySpy.mockReturnValue('ragnaros'); - moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + const prefix = 'warcraft'; + const cacheKey = 'ragnaros'; + const hashData = { race: 'orc' }; + const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' }; + const targetsCache = { ragnaros: cacheItem }; - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); - expect(cacheKeySpy).toHaveBeenCalledTimes(1); + beforeEach((): void => { + cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(cacheKey); + }); + afterEach((): void => { + cacheKeySpy.mockRestore(); }); - it('should return the correct record if cacheKey matches', () => { - cacheKeySpy.mockReturnValue('ragnaros'); - - const result = moduleUnderTest.getCacheRecord( - targetsCache, - prefix, - hashData - ); + describe('getCacheRecord', (): void => { + it('should call cacheKey once and, with correct arguments', (): void => { + getCacheRecord(targetsCache, prefix, hashData); - expect(result).toEqual(cacheItem); - }); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + }); - it('should return undefined if no matching key exists in the cache', () => { - cacheKeySpy.mockReturnValue('non-existent-key'); + it('should return correct record if match', (): void => { + const result = getCacheRecord( + targetsCache, + prefix, + hashData + ); + expect(result).toEqual(cacheItem); + }); - const result = moduleUnderTest.getCacheRecord( - targetsCache, - prefix, - hashData - ); + it('should return undefined if no match', (): void => { + cacheKeySpy.mockReturnValue('non-existent-key'); - expect(result).toBeUndefined(); + const result = getCacheRecord( + targetsCache, + prefix, + hashData + ); + expect(result).toBeUndefined(); + }); }); - }); - describe('setCacheRecord', (): void => { - const cacheData = { thunderfury: 'Blood of Sylvanas' }; - it('should call cacheKey with the correct arguments', (): void => { - cacheKeySpy.mockReturnValue('ragnaros'); - setCacheRecord(targetsCache, prefix, hashData, cacheData); + describe('setCacheRecord', (): void => { + const cacheData = { thunderfury: 'Blood of Sylvanas' }; - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); - expect(cacheKeySpy).toHaveBeenCalledTimes(1); - }); + it('should call cacheKey once, and with correct arguments', (): void => { + setCacheRecord(targetsCache, prefix, hashData, cacheData); - it('should set a cache record and return the cached data', () => { - const result = setCacheRecord(targetsCache, prefix, hashData, cacheData); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + }); - const expectedKey = cacheKey(prefix, hashData); - expect(targetsCache).toHaveProperty(expectedKey, cacheData); - expect(result).toBe(cacheData); - }); + it('should set the cache record, and return it', (): void => { + const result = setCacheRecord(targetsCache, prefix, hashData, cacheData); - it('should overwrite existing cache data with the same key', () => { - const updated = { thunderfury: 'Soul of Sylvanas' }; + expect(result).toBe(cacheData); + expect(targetsCache).toHaveProperty(cacheKey, cacheData); + }); - cacheKeySpy.mockReturnValue('ragnaros'); + it('should update existing cache data, and return it', (): void => { + const recordToUpdate = { thunderfury: 'Soul of Sylvanas' }; - setCacheRecord(targetsCache, prefix, hashData, cacheData); - const result = setCacheRecord(targetsCache, prefix, hashData, updated); + setCacheRecord(targetsCache, prefix, hashData, cacheData); + const updatedRecord = setCacheRecord(targetsCache, prefix, hashData, recordToUpdate); - const expectedKey = cacheKey(prefix, hashData); - expect(targetsCache).toHaveProperty(expectedKey, updated); - expect(result).toBe(updated); + expect(updatedRecord).toBe(recordToUpdate); + expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); + }); }); - }); + }) - // I'M PROUD OF THIS ONE, NOW IT'S TIME FOR REMAINING :) describe('readTargetsCache', (): void => { const cachePath = 'azeroth'; let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; @@ -115,21 +109,21 @@ describe('caching', (): void => { delete process.env.NX_CACHE_PROJECT_GRAPH; }); - it('should call existSync once and with correct argument', (): void => { + it('should call existSync once, and with correct argument', (): void => { readTargetsCache(cachePath); expect(existsSyncSpy).toHaveBeenCalledWith(cachePath); expect(existsSyncSpy).toHaveBeenCalledTimes(1); }); - it('should call readJsonFile once and with correct argument', (): void => { + it('should call readJsonFile once, and with correct argument', (): void => { readTargetsCache(cachePath); expect(readJsonFileSpy).toHaveBeenCalledWith(cachePath); expect(readJsonFileSpy).toHaveBeenCalledTimes(1); }); - it('should return target cache if existsSync returns true and NX_CACHE_PROJECT_GRAPH = true', (): void => { + it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { const targetsCacheResult = readTargetsCache(cachePath); - expect(targetsCacheResult).toStrictEqual({ + expect(targetsCacheResult).toEqual({ mockKey: MOCK_PROJECT_CONFIGURATION, }); }); @@ -137,20 +131,20 @@ describe('caching', (): void => { it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { process.env.NX_CACHE_PROJECT_GRAPH = 'false'; const targetsCacheResult = readTargetsCache(cachePath); - expect(targetsCacheResult).toStrictEqual({}); + expect(targetsCacheResult).toEqual({}); }); it('should return empty object if existsSync returns false', (): void => { existsSyncSpy.mockImplementation((): boolean => false); const targetsCacheResult = readTargetsCache(cachePath); - expect(targetsCacheResult).toStrictEqual({}); + expect(targetsCacheResult).toEqual({}); }); - it('should return empty object if existsSync returns false and NX_CACHE_PROJECT_GRAPH = false', (): void => { + 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'; const targetsCacheResult = readTargetsCache(cachePath); - expect(targetsCacheResult).toStrictEqual({}); + expect(targetsCacheResult).toEqual({}); }); }); }); From 5b1ad171fd340d2b12904b606c00da626a03b335 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 17:28:14 +0100 Subject: [PATCH 10/31] tests: writeTargetsToCache, final touches --- projects/nx-verdaccio/src/plugin/caching.ts | 2 +- .../src/plugin/caching.unit-test.ts | 110 ++++++++++++------ .../src/plugin/constants.unit-test.ts | 55 --------- testing/test-utils/src/lib/constants.ts | 60 ++++++++++ 4 files changed, 137 insertions(+), 90 deletions(-) delete mode 100644 projects/nx-verdaccio/src/plugin/constants.unit-test.ts diff --git a/projects/nx-verdaccio/src/plugin/caching.ts b/projects/nx-verdaccio/src/plugin/caching.ts index 54b9c200..9fbcc5de 100644 --- a/projects/nx-verdaccio/src/plugin/caching.ts +++ b/projects/nx-verdaccio/src/plugin/caching.ts @@ -41,7 +41,7 @@ export function readTargetsCache( export function writeTargetsToCache( cachePath: string, results: Record> -) { +): void { process.env.NX_CACHE_PROJECT_GRAPH !== 'false' && writeJsonFile(cachePath, results); } diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 3f0c032f..ae540e3b 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -1,11 +1,22 @@ -import { afterEach, beforeEach, describe, expect, type MockInstance } from 'vitest'; +import { + afterEach, + beforeEach, + describe, + expect, + type MockInstance, +} from 'vitest'; import * as nodeFs from 'node:fs'; import * as nxDevKit from '@nx/devkit'; import { type ProjectConfiguration } from '@nx/devkit'; import { type JsonReadOptions } from 'nx/src/utils/fileutils'; -import { getCacheRecord, readTargetsCache, setCacheRecord } from './caching'; +import { MOCK_TARGETS_CACHE } from '@push-based/test-utils'; +import { + getCacheRecord, + readTargetsCache, + setCacheRecord, + writeTargetsToCache, +} from './caching'; import * as cachingUtils from './utils/caching.utils'; -import { MOCK_PROJECT_CONFIGURATION } from './constants.unit-test'; describe('caching', (): void => { describe('cacheRecord', (): void => { @@ -21,8 +32,11 @@ describe('caching', (): void => { const targetsCache = { ragnaros: cacheItem }; beforeEach((): void => { - cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(cacheKey); + cacheKeySpy = vi + .spyOn(cachingUtils, 'cacheKey') + .mockReturnValue(cacheKey); }); + afterEach((): void => { cacheKeySpy.mockRestore(); }); @@ -36,22 +50,14 @@ describe('caching', (): void => { }); it('should return correct record if match', (): void => { - const result = getCacheRecord( - targetsCache, - prefix, - hashData - ); + const result = getCacheRecord(targetsCache, prefix, hashData); expect(result).toEqual(cacheItem); }); it('should return undefined if no match', (): void => { cacheKeySpy.mockReturnValue('non-existent-key'); - const result = getCacheRecord( - targetsCache, - prefix, - hashData - ); + const result = getCacheRecord(targetsCache, prefix, hashData); expect(result).toBeUndefined(); }); }); @@ -67,7 +73,12 @@ describe('caching', (): void => { }); it('should set the cache record, and return it', (): void => { - const result = setCacheRecord(targetsCache, prefix, hashData, cacheData); + const result = setCacheRecord( + targetsCache, + prefix, + hashData, + cacheData + ); expect(result).toBe(cacheData); expect(targetsCache).toHaveProperty(cacheKey, cacheData); @@ -77,16 +88,21 @@ describe('caching', (): void => { const recordToUpdate = { thunderfury: 'Soul of Sylvanas' }; setCacheRecord(targetsCache, prefix, hashData, cacheData); - const updatedRecord = setCacheRecord(targetsCache, prefix, hashData, recordToUpdate); + const updatedRecord = setCacheRecord( + targetsCache, + prefix, + hashData, + recordToUpdate + ); expect(updatedRecord).toBe(recordToUpdate); expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); }); }); - }) + }); describe('readTargetsCache', (): void => { - const cachePath = 'azeroth'; + const path = 'azeroth'; let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; let readJsonFileSpy: MockInstance< [path: string, options?: JsonReadOptions], @@ -94,12 +110,16 @@ describe('caching', (): void => { >; beforeEach((): void => { - existsSyncSpy = vi.spyOn(nodeFs, 'existsSync') + existsSyncSpy = vi + .spyOn(nodeFs, 'existsSync') .mockImplementation((): boolean => true); - readJsonFileSpy = vi.spyOn(nxDevKit, 'readJsonFile') - .mockImplementation((): Record> => { - return { mockKey: MOCK_PROJECT_CONFIGURATION } - }); + readJsonFileSpy = vi + .spyOn(nxDevKit, 'readJsonFile') + .mockImplementation( + (): Record> => { + return MOCK_TARGETS_CACHE; + } + ); process.env.NX_CACHE_PROJECT_GRAPH = 'true'; }); @@ -110,41 +130,63 @@ describe('caching', (): void => { }); it('should call existSync once, and with correct argument', (): void => { - readTargetsCache(cachePath); - expect(existsSyncSpy).toHaveBeenCalledWith(cachePath); + readTargetsCache(path); + expect(existsSyncSpy).toHaveBeenCalledWith(path); expect(existsSyncSpy).toHaveBeenCalledTimes(1); }); it('should call readJsonFile once, and with correct argument', (): void => { - readTargetsCache(cachePath); - expect(readJsonFileSpy).toHaveBeenCalledWith(cachePath); + 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 => { - const targetsCacheResult = readTargetsCache(cachePath); - expect(targetsCacheResult).toEqual({ - mockKey: MOCK_PROJECT_CONFIGURATION, - }); + const targetsCacheResult = readTargetsCache(path); + expect(targetsCacheResult).toEqual(MOCK_TARGETS_CACHE); }); it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { process.env.NX_CACHE_PROJECT_GRAPH = 'false'; - const targetsCacheResult = readTargetsCache(cachePath); + const targetsCacheResult = readTargetsCache(path); expect(targetsCacheResult).toEqual({}); }); it('should return empty object if existsSync returns false', (): void => { existsSyncSpy.mockImplementation((): boolean => false); - const targetsCacheResult = readTargetsCache(cachePath); + const targetsCacheResult = readTargetsCache(path); expect(targetsCacheResult).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'; - const targetsCacheResult = readTargetsCache(cachePath); + const targetsCacheResult = readTargetsCache(path); expect(targetsCacheResult).toEqual({}); }); }); + + 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; + }); + + 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); + }); + }); }); diff --git a/projects/nx-verdaccio/src/plugin/constants.unit-test.ts b/projects/nx-verdaccio/src/plugin/constants.unit-test.ts deleted file mode 100644 index eb49e013..00000000 --- a/projects/nx-verdaccio/src/plugin/constants.unit-test.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { type ProjectConfiguration } from '@nx/devkit'; - -export const MOCK_PROJECT_CONFIGURATION: ProjectConfiguration = { - name: 'mock-project', - root: 'apps/mock-project', - sourceRoot: 'apps/mock-project/src', - projectType: 'application', - tags: ['e2e', 'unit-test'], - implicitDependencies: ['shared-library'], - targets: { - build: { - executor: '@nx/web:build', - options: { - outputPath: 'dist/apps/mock-project', - index: 'apps/mock-project/src/index.html', - main: 'apps/mock-project/src/main.ts', - tsConfig: 'apps/mock-project/tsconfig.app.json', - }, - configurations: { - production: { - fileReplacements: [ - { - replace: 'apps/mock-project/src/environments/environment.ts', - with: 'apps/mock-project/src/environments/environment.prod.ts', - }, - ], - optimization: true, - sourceMap: false, - }, - }, - }, - }, - generators: { - '@nx/react': { - library: { - style: 'scss', - }, - }, - }, - namedInputs: { - default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], - production: ['default', '!{projectRoot}/**/*.test.ts'], - }, - release: { - version: { - generator: '@nx/version', - generatorOptions: { - increment: 'minor', - }, - }, - }, - metadata: { - description: 'This is a mock project for testing.', - }, -}; diff --git a/testing/test-utils/src/lib/constants.ts b/testing/test-utils/src/lib/constants.ts index e6d5cd04..f7970a61 100644 --- a/testing/test-utils/src/lib/constants.ts +++ b/testing/test-utils/src/lib/constants.ts @@ -1 +1,61 @@ +import type { ProjectConfiguration } from '@nx/devkit'; + export const MEMFS_VOLUME = '/test'; + +const MOCK_PROJECT_CONFIGURATION: ProjectConfiguration = { + name: 'mock-project', + root: 'apps/mock-project', + sourceRoot: 'apps/mock-project/src', + projectType: 'application', + tags: ['e2e', 'unit-test'], + implicitDependencies: ['shared-library'], + targets: { + build: { + executor: '@nx/web:build', + options: { + outputPath: 'dist/apps/mock-project', + index: 'apps/mock-project/src/index.html', + main: 'apps/mock-project/src/main.ts', + tsConfig: 'apps/mock-project/tsconfig.app.json', + }, + configurations: { + production: { + fileReplacements: [ + { + replace: 'apps/mock-project/src/environments/environment.ts', + with: 'apps/mock-project/src/environments/environment.prod.ts', + }, + ], + optimization: true, + sourceMap: false, + }, + }, + }, + }, + generators: { + '@nx/react': { + library: { + style: 'scss', + }, + }, + }, + namedInputs: { + default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], + production: ['default', '!{projectRoot}/**/*.test.ts'], + }, + release: { + version: { + generator: '@nx/version', + generatorOptions: { + increment: 'minor', + }, + }, + }, + metadata: { + description: 'This is a mock project for testing.', + }, +}; + +export const MOCK_TARGETS_CACHE: Record> = { + mockKey: MOCK_PROJECT_CONFIGURATION +} From d78918a4c73408be601ca77dc0d2bd19521f6d24 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 18:04:20 +0100 Subject: [PATCH 11/31] tests: memory optimization --- .../src/plugin/caching.unit-test.ts | 43 ++++-------- .../plugin/utils/caching.utils.unit-test.ts | 66 ++++++++----------- 2 files changed, 39 insertions(+), 70 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index ae540e3b..41263399 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -44,21 +44,17 @@ describe('caching', (): void => { describe('getCacheRecord', (): void => { it('should call cacheKey once and, with correct arguments', (): void => { getCacheRecord(targetsCache, prefix, hashData); - expect(cacheKeySpy).toHaveBeenCalledTimes(1); expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); }); it('should return correct record if match', (): void => { - const result = getCacheRecord(targetsCache, prefix, hashData); - expect(result).toEqual(cacheItem); + expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual(cacheItem); }); it('should return undefined if no match', (): void => { cacheKeySpy.mockReturnValue('non-existent-key'); - - const result = getCacheRecord(targetsCache, prefix, hashData); - expect(result).toBeUndefined(); + expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined(); }); }); @@ -67,35 +63,24 @@ describe('caching', (): void => { it('should call cacheKey once, and 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 => { - const result = setCacheRecord( - targetsCache, - prefix, - hashData, - cacheData - ); - - expect(result).toBe(cacheData); + 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); - const updatedRecord = setCacheRecord( - targetsCache, - prefix, - hashData, - recordToUpdate - ); - expect(updatedRecord).toBe(recordToUpdate); + expect( + setCacheRecord(targetsCache, prefix, hashData, recordToUpdate) + ).toBe(recordToUpdate); expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); }); }); @@ -142,27 +127,23 @@ describe('caching', (): void => { }); it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { - const targetsCacheResult = readTargetsCache(path); - expect(targetsCacheResult).toEqual(MOCK_TARGETS_CACHE); + 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'; - const targetsCacheResult = readTargetsCache(path); - expect(targetsCacheResult).toEqual({}); + expect(readTargetsCache(path)).toEqual({}); }); it('should return empty object if existsSync returns false', (): void => { existsSyncSpy.mockImplementation((): boolean => false); - const targetsCacheResult = readTargetsCache(path); - expect(targetsCacheResult).toEqual({}); + 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'; - const targetsCacheResult = readTargetsCache(path); - expect(targetsCacheResult).toEqual({}); + expect(readTargetsCache(path)).toEqual({}); }); }); 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 13aa6274..616911ba 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 @@ -1,58 +1,46 @@ -import { afterEach, describe, expect, type MockInstance } from 'vitest'; +import { afterEach,beforeEach, describe, expect, type MockInstance } from 'vitest'; import * as fileHasher from 'nx/src/hasher/file-hasher'; import { cacheKey } from './caching.utils'; describe('cacheKey', (): void => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; + const hashObjectReturnValue = '123456789' let hashObjectSpy: MockInstance<[obj: object], string>; - describe('hashed object', (): void => { - beforeEach((): void => { - hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); - }); - afterEach((): void => { - hashObjectSpy.mockRestore(); - }); - it('should return cache key with hashed object when it is empty', (): void => { - // {} = 3244421341483603138 - expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); - }); - it('should return cache key with hashed object when it is NOT empty', (): void => { - // { race: 'orc' } = 9075797468634534731 - expect(cacheKey(prefix, hashData)).toBe(`${prefix}-5048043832971198124`); - }); - it('should call hashObject with the correct data', () => { - const hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); - - const result = cacheKey(prefix, hashData); - - expect(hashObjectSpy).toHaveBeenCalledTimes(1); - expect(hashObjectSpy).toHaveBeenCalledWith(hashData); - expect(result).toContain(prefix); - }); - it('should return unmodified hashObject return value', (): void => { - const hashObjectSpy = vi - .spyOn(fileHasher, 'hashObject') - .mockImplementation((): string => 'mocked-hash'); - const result = cacheKey(prefix, hashData); - - expect(result).toBe(`${prefix}-mocked-hash`); - expect(hashObjectSpy).toHaveBeenCalledTimes(1); - expect(hashObjectSpy).toHaveBeenCalledWith(hashData); - }); + beforeEach((): void => { + hashObjectSpy = vi.spyOn(fileHasher, 'hashObject').mockReturnValue(hashObjectReturnValue) }); + afterEach((): void => { + hashObjectSpy.mockRestore(); + }); + describe('prefix', (): void => { it('should return cache key with unmodified prefix', (): void => { - // {} = 3244421341483603138 - expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); + 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 => { - const result = cacheKey(prefix, hashData); - expect(result).toMatch(regex); + 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 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}`); }); }); }); From fd1432622a6835968b8eeb099c7bbefd4adfc773 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 18:33:09 +0100 Subject: [PATCH 12/31] chore: nx format --- CHANGELOG.md | 69 +++++++------------ projects/nx-verdaccio/src/plugin/caching.ts | 2 +- .../src/plugin/caching.unit-test.ts | 13 ++-- .../plugin/utils/caching.utils.unit-test.ts | 20 ++++-- testing/test-utils/src/lib/constants.ts | 9 ++- 5 files changed, 52 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf5b8ab0..1f0ca649 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,227 +1,207 @@ ## 0.0.0-alpha.27 (2024-12-11) - ### 🚀 Features - cleanup git status in folders that are checked in ([5e2c9b5](https://github.com/push-based/nx-verdaccio/commit/5e2c9b5)) - add skip install ([b1628ef](https://github.com/push-based/nx-verdaccio/commit/b1628ef)) - **nx-verdaccio:** add skipInstall and post script ([f2c0c86](https://github.com/push-based/nx-verdaccio/commit/f2c0c86)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.26 (2024-10-23) - ### 🩹 Fixes - add skipTeardown to env-teardown executor ([ff52749](https://github.com/push-based/nx-verdaccio/commit/ff52749)) - add skipTeardown to env-teardown executor ([664c6bd](https://github.com/push-based/nx-verdaccio/commit/664c6bd)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.25 (2024-10-22) - ### 🩹 Fixes - pkg-install unit tests ([9c6e64d](https://github.com/push-based/nx-verdaccio/commit/9c6e64d)) - npmWorkspace tests unit tests ([7485fe4](https://github.com/push-based/nx-verdaccio/commit/7485fe4)) - e2e ([10a7c76](https://github.com/push-based/nx-verdaccio/commit/10a7c76)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.24 (2024-10-20) - ### 🩹 Fixes - install optional deps with package ([abf41db](https://github.com/push-based/nx-verdaccio/commit/abf41db)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.23 (2024-10-19) - ### 🩹 Fixes - detach if not windows ([5f0e94e](https://github.com/push-based/nx-verdaccio/commit/5f0e94e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.22 (2024-10-18) - ### 🩹 Fixes - envRoot derivation in executor ([ae6b9aa](https://github.com/push-based/nx-verdaccio/commit/ae6b9aa)) - **nx-verdaccio:** move envRoot into executors ([3818fba](https://github.com/push-based/nx-verdaccio/commit/3818fba)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.21 (2024-10-15) - ### 🩹 Fixes - remove detached ([558a25e](https://github.com/push-based/nx-verdaccio/commit/558a25e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.20 (2024-10-15) - ### 🩹 Fixes - docs ([4921af8](https://github.com/push-based/nx-verdaccio/commit/4921af8)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.19 (2024-10-15) - ### 🩹 Fixes - deps ([daec92d](https://github.com/push-based/nx-verdaccio/commit/daec92d)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.18 (2024-10-15) - ### 🩹 Fixes - use info to log ([fce0cab](https://github.com/push-based/nx-verdaccio/commit/fce0cab)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.17 (2024-10-15) - ### 🩹 Fixes - forward params of nxv-e2e target ([8348d94](https://github.com/push-based/nx-verdaccio/commit/8348d94)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.16 (2024-10-15) - ### 🩹 Fixes - mark package ([8a0630b](https://github.com/push-based/nx-verdaccio/commit/8a0630b)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.15 (2024-10-15) - ### 🩹 Fixes - nx17 fix ([ebbfe6b](https://github.com/push-based/nx-verdaccio/commit/ebbfe6b)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.14 (2024-10-15) - ### 🩹 Fixes - version ([55a8bb1](https://github.com/push-based/nx-verdaccio/commit/55a8bb1)) - version 1 ([9cfa53e](https://github.com/push-based/nx-verdaccio/commit/9cfa53e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.13 (2024-10-15) - ### 🩹 Fixes - add command to make it work in older nx versions ([8cca3b7](https://github.com/push-based/nx-verdaccio/commit/8cca3b7)) - add description ([9af5dd2](https://github.com/push-based/nx-verdaccio/commit/9af5dd2)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.12 (2024-10-14) - ### 🩹 Fixes - readme 3 ([7b2422b](https://github.com/push-based/nx-verdaccio/commit/7b2422b)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.11 (2024-10-14) - ### 🩹 Fixes - readme 2 ([2540ebd](https://github.com/push-based/nx-verdaccio/commit/2540ebd)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.10 (2024-10-14) - ### 🩹 Fixes - readme ([26f1b13](https://github.com/push-based/nx-verdaccio/commit/26f1b13)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.9 (2024-10-14) - ### 🩹 Fixes - deps ([ceeb41b](https://github.com/push-based/nx-verdaccio/commit/ceeb41b)) - deps ([4f5303e](https://github.com/push-based/nx-verdaccio/commit/4f5303e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.8 (2024-10-14) - ### 🚀 Features - **nx-verdaccio:** update plugin to enable env-setup caching ([f129325](https://github.com/push-based/nx-verdaccio/commit/f129325)) @@ -234,13 +214,12 @@ - set default values for cacheSizeTasks and options in plugin ([b782914](https://github.com/push-based/nx-verdaccio/commit/b782914)) - mock exec implementation to handle callbacks in unit tests ([88ebbd4](https://github.com/push-based/nx-verdaccio/commit/88ebbd4)) -### ❤️ Thank You +### ❤️ Thank You - getlarge ## 0.0.0-alpha.7 (2024-10-13) - ### 🚀 Features - modify package json before publishing ([3faf91c](https://github.com/push-based/nx-verdaccio/commit/3faf91c)) @@ -255,31 +234,29 @@ - refactor testing API usage ([f3e4c29](https://github.com/push-based/nx-verdaccio/commit/f3e4c29)) - refactor testing ([7425758](https://github.com/push-based/nx-verdaccio/commit/7425758)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.6 (2024-09-29) - ### 🩹 Fixes - make package public ([046a788](https://github.com/push-based/-nx-verdaccio-e2e-setup/commit/046a788)) - change JS esm module type to CommonJS ([9d9d60d](https://github.com/push-based/-nx-verdaccio-e2e-setup/commit/9d9d60d)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.5 (2024-09-29) - ### 🩹 Fixes - fix typo ([8830e1a](https://github.com/push-based/-nx-verdaccio-e2e-setup/commit/8830e1a)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular diff --git a/projects/nx-verdaccio/src/plugin/caching.ts b/projects/nx-verdaccio/src/plugin/caching.ts index 9fbcc5de..1e2d56a7 100644 --- a/projects/nx-verdaccio/src/plugin/caching.ts +++ b/projects/nx-verdaccio/src/plugin/caching.ts @@ -34,7 +34,7 @@ export function readTargetsCache( cachePath: string ): Record> { return process.env.NX_CACHE_PROJECT_GRAPH !== 'false' && existsSync(cachePath) - ? readJsonFile(cachePath) + ? readJsonFile(cachePath) : {}; } diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 41263399..f67b5a9a 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -49,7 +49,9 @@ describe('caching', (): void => { }); it('should return correct record if match', (): void => { - expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual(cacheItem); + expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual( + cacheItem + ); }); it('should return undefined if no match', (): void => { @@ -68,9 +70,9 @@ describe('caching', (): void => { }); it('should set the cache record, and return it', (): void => { - expect( - setCacheRecord(targetsCache, prefix, hashData, cacheData) - ).toBe(cacheData); + expect(setCacheRecord(targetsCache, prefix, hashData, cacheData)).toBe( + cacheData + ); expect(targetsCache).toHaveProperty(cacheKey, cacheData); }); @@ -148,7 +150,8 @@ describe('caching', (): void => { }); describe('writeTargetsToCache', (): void => { - const writeJsonFile = vi.spyOn(nxDevKit, 'writeJsonFile') + const writeJsonFile = vi + .spyOn(nxDevKit, 'writeJsonFile') .mockImplementation((): string => 'dont write to file :D'); const path = 'azeroth'; 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 616911ba..cf41764f 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 @@ -1,15 +1,23 @@ -import { afterEach,beforeEach, describe, expect, type MockInstance } from 'vitest'; +import { + afterEach, + beforeEach, + describe, + expect, + type MockInstance, +} from 'vitest'; import * as fileHasher from 'nx/src/hasher/file-hasher'; import { cacheKey } from './caching.utils'; describe('cacheKey', (): void => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; - const hashObjectReturnValue = '123456789' + const hashObjectReturnValue = '123456789'; let hashObjectSpy: MockInstance<[obj: object], string>; beforeEach((): void => { - hashObjectSpy = vi.spyOn(fileHasher, 'hashObject').mockReturnValue(hashObjectReturnValue) + hashObjectSpy = vi + .spyOn(fileHasher, 'hashObject') + .mockReturnValue(hashObjectReturnValue); }); afterEach((): void => { hashObjectSpy.mockRestore(); @@ -40,9 +48,9 @@ describe('cacheKey', (): void => { }); it('should return cache key, when hashData is NOT empty', (): void => { - expect(cacheKey(prefix, hashData)).toBe(`${prefix}-${hashObjectReturnValue}`); + expect(cacheKey(prefix, hashData)).toBe( + `${prefix}-${hashObjectReturnValue}` + ); }); }); }); - - diff --git a/testing/test-utils/src/lib/constants.ts b/testing/test-utils/src/lib/constants.ts index f7970a61..b5c9ebf9 100644 --- a/testing/test-utils/src/lib/constants.ts +++ b/testing/test-utils/src/lib/constants.ts @@ -56,6 +56,9 @@ const MOCK_PROJECT_CONFIGURATION: ProjectConfiguration = { }, }; -export const MOCK_TARGETS_CACHE: Record> = { - mockKey: MOCK_PROJECT_CONFIGURATION -} +export const MOCK_TARGETS_CACHE: Record< + string, + Partial +> = { + mockKey: MOCK_PROJECT_CONFIGURATION, +}; From b046f6ee629819e874ff69df8cfb6d821db2abbb Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 20 Dec 2024 15:51:50 +0100 Subject: [PATCH 13/31] tests: name changes --- .../nx-verdaccio/src/plugin/caching.unit-test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index f67b5a9a..f14c044d 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -42,19 +42,19 @@ describe('caching', (): void => { }); describe('getCacheRecord', (): void => { - it('should call cacheKey once and, with correct arguments', (): 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 correct record if match', (): void => { + 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 no match', (): void => { + it('should return undefined if there is no cache hit', (): void => { cacheKeySpy.mockReturnValue('non-existent-key'); expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined(); }); @@ -63,7 +63,7 @@ describe('caching', (): void => { describe('setCacheRecord', (): void => { const cacheData = { thunderfury: 'Blood of Sylvanas' }; - it('should call cacheKey once, and with correct arguments', (): void => { + it('should call cacheKey once with correct arguments', (): void => { setCacheRecord(targetsCache, prefix, hashData, cacheData); expect(cacheKeySpy).toHaveBeenCalledTimes(1); expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); @@ -116,13 +116,13 @@ describe('caching', (): void => { delete process.env.NX_CACHE_PROJECT_GRAPH; }); - it('should call existSync once, and with correct argument', (): void => { + it('should call existSync once with correct argument', (): void => { readTargetsCache(path); expect(existsSyncSpy).toHaveBeenCalledWith(path); expect(existsSyncSpy).toHaveBeenCalledTimes(1); }); - it('should call readJsonFile once, and with correct argument', (): void => { + it('should call readJsonFile once with correct argument', (): void => { readTargetsCache(path); expect(readJsonFileSpy).toHaveBeenCalledWith(path); expect(readJsonFileSpy).toHaveBeenCalledTimes(1); @@ -160,7 +160,7 @@ describe('caching', (): void => { delete process.env.NX_CACHE_PROJECT_GRAPH; }); - it('should call writeJsonFile once, with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { + 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); From 4c35d59d59ec2f05bfdce159a2dd5801fdc7a26a Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 20 Dec 2024 15:53:33 +0100 Subject: [PATCH 14/31] chore: exclude changelog file from prettier formatting --- .prettierignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index e26f0b3f..fd36f863 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,5 @@ /dist /coverage /.nx/cache -/.nx/workspace-data \ No newline at end of file +/.nx/workspace-data +CHANGELOG.md From da5919bdf01801fcfac11541a6b789023404be3d Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 20 Dec 2024 16:01:30 +0100 Subject: [PATCH 15/31] tests: delete unnecessary nesting of describe blocks --- .../src/plugin/caching.unit-test.ts | 250 +++++++++--------- .../plugin/utils/caching.utils.unit-test.ts | 40 ++- 2 files changed, 138 insertions(+), 152 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index f14c044d..a493633c 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 cf41764f..b3e16425 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}` + ); }); }); From 2e749e3d7cd6a9b263e9d9b7fb9c7d9f649638b6 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 20 Dec 2024 16:05:09 +0100 Subject: [PATCH 16/31] tests: change hardcoded env set to vi.stubEnv --- projects/nx-verdaccio/src/plugin/caching.unit-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index a493633c..e1117a79 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -100,13 +100,13 @@ describe('readTargetsCache', (): void => { .mockImplementation((): Record> => { return MOCK_TARGETS_CACHE; }); - process.env.NX_CACHE_PROJECT_GRAPH = 'true'; + vi.stubEnv("NX_CACHE_PROJECT_GRAPH", 'true') }); afterEach((): void => { existsSyncSpy.mockRestore(); readJsonFileSpy.mockRestore(); - delete process.env.NX_CACHE_PROJECT_GRAPH; + vi.clearAllMocks(); }); it('should call existSync once with correct argument', (): void => { From a78f6c415b055aa11b9624e0766c556acd0a44b2 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 20 Dec 2024 16:14:32 +0100 Subject: [PATCH 17/31] tests: move consts project configuration to fixtures --- .../fixtures/project-configuration.fixture.ts | 62 ++++++++++++++++++ .../src/plugin/caching.unit-test.ts | 2 +- testing/test-utils/src/lib/constants.ts | 63 ------------------- 3 files changed, 63 insertions(+), 64 deletions(-) create mode 100644 projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts diff --git a/projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts b/projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts new file mode 100644 index 00000000..3ca4ba30 --- /dev/null +++ b/projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts @@ -0,0 +1,62 @@ +import type { ProjectConfiguration } from '@nx/devkit'; + +const MOCK_PROJECT_CONFIGURATION: ProjectConfiguration = { + name: 'mock-project', + root: 'apps/mock-project', + sourceRoot: 'apps/mock-project/src', + projectType: 'application', + tags: ['e2e', 'unit-test'], + implicitDependencies: ['shared-library'], + targets: { + build: { + executor: '@nx/web:build', + options: { + outputPath: 'dist/apps/mock-project', + index: 'apps/mock-project/src/index.html', + main: 'apps/mock-project/src/main.ts', + tsConfig: 'apps/mock-project/tsconfig.app.json', + }, + configurations: { + production: { + fileReplacements: [ + { + replace: 'apps/mock-project/src/environments/environment.ts', + with: 'apps/mock-project/src/environments/environment.prod.ts', + }, + ], + optimization: true, + sourceMap: false, + }, + }, + }, + }, + generators: { + '@nx/react': { + library: { + style: 'scss', + }, + }, + }, + namedInputs: { + default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], + production: ['default', '!{projectRoot}/**/*.test.ts'], + }, + release: { + version: { + generator: '@nx/version', + generatorOptions: { + increment: 'minor', + }, + }, + }, + metadata: { + description: 'This is a mock project for testing.', + }, +}; + +export const MOCK_TARGETS_CACHE: Record< + string, + Partial +> = { + mockKey: MOCK_PROJECT_CONFIGURATION, +}; diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index e1117a79..18dc0643 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -9,7 +9,6 @@ import * as nodeFs from 'node:fs'; import * as nxDevKit from '@nx/devkit'; import { type ProjectConfiguration } from '@nx/devkit'; import { type JsonReadOptions } from 'nx/src/utils/fileutils'; -import { MOCK_TARGETS_CACHE } from '@push-based/test-utils'; import { getCacheRecord, readTargetsCache, @@ -17,6 +16,7 @@ import { writeTargetsToCache, } from './caching'; import * as cachingUtils from './utils/caching.utils'; +import { MOCK_TARGETS_CACHE } from '../fixtures/project-configuration.fixture'; describe('cacheRecord', (): void => { let cacheKeySpy: MockInstance< diff --git a/testing/test-utils/src/lib/constants.ts b/testing/test-utils/src/lib/constants.ts index b5c9ebf9..e6d5cd04 100644 --- a/testing/test-utils/src/lib/constants.ts +++ b/testing/test-utils/src/lib/constants.ts @@ -1,64 +1 @@ -import type { ProjectConfiguration } from '@nx/devkit'; - export const MEMFS_VOLUME = '/test'; - -const MOCK_PROJECT_CONFIGURATION: ProjectConfiguration = { - name: 'mock-project', - root: 'apps/mock-project', - sourceRoot: 'apps/mock-project/src', - projectType: 'application', - tags: ['e2e', 'unit-test'], - implicitDependencies: ['shared-library'], - targets: { - build: { - executor: '@nx/web:build', - options: { - outputPath: 'dist/apps/mock-project', - index: 'apps/mock-project/src/index.html', - main: 'apps/mock-project/src/main.ts', - tsConfig: 'apps/mock-project/tsconfig.app.json', - }, - configurations: { - production: { - fileReplacements: [ - { - replace: 'apps/mock-project/src/environments/environment.ts', - with: 'apps/mock-project/src/environments/environment.prod.ts', - }, - ], - optimization: true, - sourceMap: false, - }, - }, - }, - }, - generators: { - '@nx/react': { - library: { - style: 'scss', - }, - }, - }, - namedInputs: { - default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], - production: ['default', '!{projectRoot}/**/*.test.ts'], - }, - release: { - version: { - generator: '@nx/version', - generatorOptions: { - increment: 'minor', - }, - }, - }, - metadata: { - description: 'This is a mock project for testing.', - }, -}; - -export const MOCK_TARGETS_CACHE: Record< - string, - Partial -> = { - mockKey: MOCK_PROJECT_CONFIGURATION, -}; From 31e094b564bc4fb80171d38f6a7db052ee4b0cc0 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 3 Jan 2025 13:46:30 +0100 Subject: [PATCH 18/31] tests: remove unnecessary describe nesting --- .../src/plugin/caching.unit-test.ts | 80 +++++++++---------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 18dc0643..9315e816 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -38,48 +38,42 @@ describe('cacheRecord', (): 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); - }); - - 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(); - }); - }); - - 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 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); - - expect( - setCacheRecord(targetsCache, prefix, hashData, recordToUpdate) - ).toBe(recordToUpdate); - expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); - }); + 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 undefined if there is no cache hit', (): void => { + cacheKeySpy.mockReturnValue('non-existent-key'); + expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined(); + }); + + it('should call cacheKey once with correct arguments', (): void => { + setCacheRecord(targetsCache, prefix, hashData, cacheItem); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + }); + + it('should set the cache record, and return it', (): void => { + expect(setCacheRecord(targetsCache, prefix, hashData, cacheItem)).toBe( + cacheItem + ); + expect(targetsCache).toHaveProperty(cacheKey, cacheItem); + }); + + it('should update existing cache data, and return it', (): void => { + const recordToUpdate = { thunderfury: 'Soul of Sylvanas' }; + setCacheRecord(targetsCache, prefix, hashData, cacheItem); + + expect(setCacheRecord(targetsCache, prefix, hashData, recordToUpdate)).toBe( + recordToUpdate + ); + expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); }); }); @@ -100,7 +94,7 @@ describe('readTargetsCache', (): void => { .mockImplementation((): Record> => { return MOCK_TARGETS_CACHE; }); - vi.stubEnv("NX_CACHE_PROJECT_GRAPH", 'true') + vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); }); afterEach((): void => { From 138d786cdc51cd1da966978101dbe5590d4ea272 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 3 Jan 2025 13:47:37 +0100 Subject: [PATCH 19/31] tests: revert order --- projects/nx-verdaccio/src/plugin/caching.unit-test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 9315e816..182d0faa 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -105,14 +105,14 @@ describe('readTargetsCache', (): void => { it('should call existSync once with correct argument', (): void => { readTargetsCache(path); - expect(existsSyncSpy).toHaveBeenCalledWith(path); expect(existsSyncSpy).toHaveBeenCalledTimes(1); + expect(existsSyncSpy).toHaveBeenCalledWith(path); }); it('should call readJsonFile once with correct argument', (): void => { readTargetsCache(path); - expect(readJsonFileSpy).toHaveBeenCalledWith(path); expect(readJsonFileSpy).toHaveBeenCalledTimes(1); + expect(readJsonFileSpy).toHaveBeenCalledWith(path); }); it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { @@ -150,8 +150,8 @@ describe('writeTargetsToCache', (): void => { 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); + expect(writeJsonFile).toHaveBeenCalledWith(path, MOCK_TARGETS_CACHE); }); it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => { From 207eafd8175f24483af0a8dba5a3a9446f1b4926 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 3 Jan 2025 13:49:37 +0100 Subject: [PATCH 20/31] tests: use stubEnv instead of manual NX_CACHE_PROJECT_GRAPH setting --- projects/nx-verdaccio/src/plugin/caching.unit-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 182d0faa..92da088a 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -148,14 +148,14 @@ describe('writeTargetsToCache', (): void => { }); it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { - process.env.NX_CACHE_PROJECT_GRAPH = 'true'; + vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); writeTargetsToCache(path, MOCK_TARGETS_CACHE); expect(writeJsonFile).toHaveBeenCalledTimes(1); expect(writeJsonFile).toHaveBeenCalledWith(path, MOCK_TARGETS_CACHE); }); it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => { - process.env.NX_CACHE_PROJECT_GRAPH = 'false'; + vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); writeTargetsToCache(path, MOCK_TARGETS_CACHE); expect(writeJsonFile).toHaveBeenCalledTimes(0); }); From 3c5214545e73df68c16dfdf5f76261bf0e9df376 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 3 Jan 2025 13:56:41 +0100 Subject: [PATCH 21/31] tests: remove big fixture in favor of small mocking data --- .../fixtures/project-configuration.fixture.ts | 62 ------------------- .../src/plugin/caching.unit-test.ts | 17 ++--- 2 files changed, 9 insertions(+), 70 deletions(-) delete mode 100644 projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts diff --git a/projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts b/projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts deleted file mode 100644 index 3ca4ba30..00000000 --- a/projects/nx-verdaccio/src/fixtures/project-configuration.fixture.ts +++ /dev/null @@ -1,62 +0,0 @@ -import type { ProjectConfiguration } from '@nx/devkit'; - -const MOCK_PROJECT_CONFIGURATION: ProjectConfiguration = { - name: 'mock-project', - root: 'apps/mock-project', - sourceRoot: 'apps/mock-project/src', - projectType: 'application', - tags: ['e2e', 'unit-test'], - implicitDependencies: ['shared-library'], - targets: { - build: { - executor: '@nx/web:build', - options: { - outputPath: 'dist/apps/mock-project', - index: 'apps/mock-project/src/index.html', - main: 'apps/mock-project/src/main.ts', - tsConfig: 'apps/mock-project/tsconfig.app.json', - }, - configurations: { - production: { - fileReplacements: [ - { - replace: 'apps/mock-project/src/environments/environment.ts', - with: 'apps/mock-project/src/environments/environment.prod.ts', - }, - ], - optimization: true, - sourceMap: false, - }, - }, - }, - }, - generators: { - '@nx/react': { - library: { - style: 'scss', - }, - }, - }, - namedInputs: { - default: ['{projectRoot}/**/*', '!{projectRoot}/**/*.spec.ts'], - production: ['default', '!{projectRoot}/**/*.test.ts'], - }, - release: { - version: { - generator: '@nx/version', - generatorOptions: { - increment: 'minor', - }, - }, - }, - metadata: { - description: 'This is a mock project for testing.', - }, -}; - -export const MOCK_TARGETS_CACHE: Record< - string, - Partial -> = { - mockKey: MOCK_PROJECT_CONFIGURATION, -}; diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 92da088a..e74ffbdf 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -7,7 +7,6 @@ import { } from 'vitest'; import * as nodeFs from 'node:fs'; import * as nxDevKit from '@nx/devkit'; -import { type ProjectConfiguration } from '@nx/devkit'; import { type JsonReadOptions } from 'nx/src/utils/fileutils'; import { getCacheRecord, @@ -16,7 +15,6 @@ import { writeTargetsToCache, } from './caching'; import * as cachingUtils from './utils/caching.utils'; -import { MOCK_TARGETS_CACHE } from '../fixtures/project-configuration.fixture'; describe('cacheRecord', (): void => { let cacheKeySpy: MockInstance< @@ -79,6 +77,7 @@ describe('cacheRecord', (): void => { describe('readTargetsCache', (): void => { const path = 'azeroth'; + const mockTargetCache = {prop: { name: 'mock' }}; let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; let readJsonFileSpy: MockInstance< [path: string, options?: JsonReadOptions], @@ -91,8 +90,8 @@ describe('readTargetsCache', (): void => { .mockImplementation((): boolean => true); readJsonFileSpy = vi .spyOn(nxDevKit, 'readJsonFile') - .mockImplementation((): Record> => { - return MOCK_TARGETS_CACHE; + .mockImplementation(() => { + return mockTargetCache; }); vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); }); @@ -116,7 +115,7 @@ describe('readTargetsCache', (): void => { }); it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { - expect(readTargetsCache(path)).toEqual(MOCK_TARGETS_CACHE); + expect(readTargetsCache(path)).toEqual(mockTargetCache); }); it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { @@ -141,6 +140,8 @@ describe('writeTargetsToCache', (): void => { .spyOn(nxDevKit, 'writeJsonFile') .mockImplementation((): string => 'dont write to file :D'); const path = 'azeroth'; + const mockTargetCache = {prop: { name: 'mock' }}; + afterEach((): void => { writeJsonFile.mockRestore(); @@ -149,14 +150,14 @@ describe('writeTargetsToCache', (): void => { it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); - writeTargetsToCache(path, MOCK_TARGETS_CACHE); + writeTargetsToCache(path, {prop: { name: 'mock' }}); expect(writeJsonFile).toHaveBeenCalledTimes(1); - expect(writeJsonFile).toHaveBeenCalledWith(path, MOCK_TARGETS_CACHE); + expect(writeJsonFile).toHaveBeenCalledWith(path, mockTargetCache); }); it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => { vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); - writeTargetsToCache(path, MOCK_TARGETS_CACHE); + writeTargetsToCache(path, mockTargetCache); expect(writeJsonFile).toHaveBeenCalledTimes(0); }); }); From 8bfe583dc8e6fa9ad3cb1372750337ae8e3a8d74 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Sun, 5 Jan 2025 15:01:27 +0100 Subject: [PATCH 22/31] tests: change manual NX_CACHE_PROJECT_GRAPH setting to stubEnv --- projects/nx-verdaccio/src/plugin/caching.unit-test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index e74ffbdf..afb12461 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -119,7 +119,7 @@ describe('readTargetsCache', (): void => { }); it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { - process.env.NX_CACHE_PROJECT_GRAPH = 'false'; + vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); expect(readTargetsCache(path)).toEqual({}); }); @@ -130,7 +130,7 @@ describe('readTargetsCache', (): void => { 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'; + vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); expect(readTargetsCache(path)).toEqual({}); }); }); @@ -145,7 +145,7 @@ describe('writeTargetsToCache', (): void => { afterEach((): void => { writeJsonFile.mockRestore(); - delete process.env.NX_CACHE_PROJECT_GRAPH; + vi.clearAllMocks(); }); it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { From 07bfa75ab788f741952fff825df1a8bb87d4a841 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Sun, 5 Jan 2025 15:29:55 +0100 Subject: [PATCH 23/31] tests: memory optimization, checking for variable duplicates, introducing file consts --- .../src/plugin/caching.unit-test.ts | 98 ++++++++++--------- .../plugin/utils/caching.utils.unit-test.ts | 3 +- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index afb12461..23532127 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -5,29 +5,31 @@ import { expect, type MockInstance, } from 'vitest'; -import * as nodeFs from 'node:fs'; -import * as nxDevKit from '@nx/devkit'; -import { type JsonReadOptions } from 'nx/src/utils/fileutils'; +import { JsonWriteOptions, type JsonReadOptions } from 'nx/src/utils/fileutils'; import { getCacheRecord, readTargetsCache, setCacheRecord, writeTargetsToCache, } from './caching'; + +import * as nodeFs from 'node:fs'; +import * as nxDevKit from '@nx/devkit'; import * as cachingUtils from './utils/caching.utils'; +const PATH = 'azeroth'; +const MOCK_CACHE_ITEM = { name: 'mocked-name' }; +const MOCK_TARGET_CACHE = { ragnaros: MOCK_CACHE_ITEM }; + describe('cacheRecord', (): void => { + const prefix = 'warcraft'; + const cacheKey = 'ragnaros'; + let cacheKeySpy: MockInstance< - [prefix: string, hashData: Record], + [prefix: string, MOCK_CACHE_ITEM: 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); }); @@ -37,50 +39,48 @@ describe('cacheRecord', (): void => { }); it('should call cacheKey once with correct arguments', (): void => { - getCacheRecord(targetsCache, prefix, hashData); + getCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM); expect(cacheKeySpy).toHaveBeenCalledTimes(1); - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, MOCK_CACHE_ITEM); }); it('should return the correct cache record if there is a cache hit', (): void => { - expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual(cacheItem); + expect(getCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM)).toEqual(MOCK_CACHE_ITEM); }); it('should return undefined if there is no cache hit', (): void => { cacheKeySpy.mockReturnValue('non-existent-key'); - expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined(); + expect(getCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM)).toBeUndefined(); }); it('should call cacheKey once with correct arguments', (): void => { - setCacheRecord(targetsCache, prefix, hashData, cacheItem); + setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); expect(cacheKeySpy).toHaveBeenCalledTimes(1); - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, MOCK_CACHE_ITEM); }); it('should set the cache record, and return it', (): void => { - expect(setCacheRecord(targetsCache, prefix, hashData, cacheItem)).toBe( - cacheItem + expect(setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM)).toBe( + MOCK_CACHE_ITEM ); - expect(targetsCache).toHaveProperty(cacheKey, cacheItem); + expect(MOCK_TARGET_CACHE).toHaveProperty(cacheKey, MOCK_CACHE_ITEM); }); it('should update existing cache data, and return it', (): void => { - const recordToUpdate = { thunderfury: 'Soul of Sylvanas' }; - setCacheRecord(targetsCache, prefix, hashData, cacheItem); + const recordToUpdate = { name: 'Soul of Sylvanas' }; + setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); - expect(setCacheRecord(targetsCache, prefix, hashData, recordToUpdate)).toBe( + expect(setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, recordToUpdate)).toBe( recordToUpdate ); - expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate); + expect(MOCK_TARGET_CACHE).toHaveProperty(cacheKey, recordToUpdate); }); }); describe('readTargetsCache', (): void => { - const path = 'azeroth'; - const mockTargetCache = {prop: { name: 'mock' }}; - let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>; + let existsSyncSpy: MockInstance<[PATH: nodeFs.PathLike], boolean>; let readJsonFileSpy: MockInstance< - [path: string, options?: JsonReadOptions], + [PATH: string, options?: JsonReadOptions], object >; @@ -91,7 +91,7 @@ describe('readTargetsCache', (): void => { readJsonFileSpy = vi .spyOn(nxDevKit, 'readJsonFile') .mockImplementation(() => { - return mockTargetCache; + return MOCK_TARGET_CACHE; }); vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); }); @@ -103,61 +103,65 @@ describe('readTargetsCache', (): void => { }); it('should call existSync once with correct argument', (): void => { - readTargetsCache(path); + readTargetsCache(PATH); expect(existsSyncSpy).toHaveBeenCalledTimes(1); - expect(existsSyncSpy).toHaveBeenCalledWith(path); + expect(existsSyncSpy).toHaveBeenCalledWith(PATH); }); it('should call readJsonFile once with correct argument', (): void => { - readTargetsCache(path); + readTargetsCache(PATH); expect(readJsonFileSpy).toHaveBeenCalledTimes(1); - expect(readJsonFileSpy).toHaveBeenCalledWith(path); + expect(readJsonFileSpy).toHaveBeenCalledWith(PATH); }); it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { - expect(readTargetsCache(path)).toEqual(mockTargetCache); + expect(readTargetsCache(PATH)).toEqual(MOCK_TARGET_CACHE); }); it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); - expect(readTargetsCache(path)).toEqual({}); + expect(readTargetsCache(PATH)).toEqual({}); }); it('should return empty object if existsSync returns false', (): void => { existsSyncSpy.mockImplementation((): boolean => false); - expect(readTargetsCache(path)).toEqual({}); + expect(readTargetsCache(PATH)).toEqual({}); }); it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH = false', (): void => { existsSyncSpy.mockImplementation((): boolean => false); vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); - expect(readTargetsCache(path)).toEqual({}); + expect(readTargetsCache(PATH)).toEqual({}); }); }); describe('writeTargetsToCache', (): void => { - const writeJsonFile = vi - .spyOn(nxDevKit, 'writeJsonFile') - .mockImplementation((): string => 'dont write to file :D'); - const path = 'azeroth'; - const mockTargetCache = {prop: { name: 'mock' }}; + let writeJsonFileSpy: MockInstance< + [PATH: string, data: object, options?: JsonWriteOptions], + void + >; + beforeEach((): void => { + writeJsonFileSpy = vi + .spyOn(nxDevKit, 'writeJsonFile') + .mockImplementation((): string => 'preventing writing to file by mocking impl'); + }); afterEach((): void => { - writeJsonFile.mockRestore(); + writeJsonFileSpy.mockRestore(); vi.clearAllMocks(); }); it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); - writeTargetsToCache(path, {prop: { name: 'mock' }}); - expect(writeJsonFile).toHaveBeenCalledTimes(1); - expect(writeJsonFile).toHaveBeenCalledWith(path, mockTargetCache); + writeTargetsToCache(PATH, MOCK_TARGET_CACHE); + expect(writeJsonFileSpy).toHaveBeenCalledTimes(1); + expect(writeJsonFileSpy).toHaveBeenCalledWith(PATH, MOCK_TARGET_CACHE); }); it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => { vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); - writeTargetsToCache(path, mockTargetCache); - expect(writeJsonFile).toHaveBeenCalledTimes(0); + writeTargetsToCache(PATH, MOCK_TARGET_CACHE); + expect(writeJsonFileSpy).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 b3e16425..dadffe87 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 @@ -12,9 +12,10 @@ describe('cacheKey', (): void => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; const hashObjectReturnValue = '123456789'; - let hashObjectSpy: MockInstance<[obj: object], string>; const regex = /^[a-zA-Z]+-\d+$/; + let hashObjectSpy: MockInstance<[obj: object], string>; + beforeEach((): void => { hashObjectSpy = vi .spyOn(fileHasher, 'hashObject') From 42afe922f7cf265ca795a7ea8e28f2721b1756a8 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Sun, 5 Jan 2025 15:38:23 +0100 Subject: [PATCH 24/31] tests: mockReturnValue instead of implementation --- .../src/plugin/caching.unit-test.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 23532127..88a7f128 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -33,7 +33,6 @@ describe('cacheRecord', (): void => { beforeEach((): void => { cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(cacheKey); }); - afterEach((): void => { cacheKeySpy.mockRestore(); }); @@ -68,8 +67,8 @@ describe('cacheRecord', (): void => { it('should update existing cache data, and return it', (): void => { const recordToUpdate = { name: 'Soul of Sylvanas' }; - setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); + setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); expect(setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, recordToUpdate)).toBe( recordToUpdate ); @@ -87,15 +86,12 @@ describe('readTargetsCache', (): void => { beforeEach((): void => { existsSyncSpy = vi .spyOn(nodeFs, 'existsSync') - .mockImplementation((): boolean => true); + .mockReturnValue(true); readJsonFileSpy = vi .spyOn(nxDevKit, 'readJsonFile') - .mockImplementation(() => { - return MOCK_TARGET_CACHE; - }); + .mockReturnValue(MOCK_TARGET_CACHE); vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); }); - afterEach((): void => { existsSyncSpy.mockRestore(); readJsonFileSpy.mockRestore(); @@ -124,12 +120,12 @@ describe('readTargetsCache', (): void => { }); it('should return empty object if existsSync returns false', (): void => { - existsSyncSpy.mockImplementation((): boolean => false); + existsSyncSpy.mockReturnValue(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); + existsSyncSpy.mockReturnValue(false); vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); expect(readTargetsCache(PATH)).toEqual({}); }); @@ -144,9 +140,8 @@ describe('writeTargetsToCache', (): void => { beforeEach((): void => { writeJsonFileSpy = vi .spyOn(nxDevKit, 'writeJsonFile') - .mockImplementation((): string => 'preventing writing to file by mocking impl'); + .mockImplementation((): string => 'preventing writing to file by mocking imp'); }); - afterEach((): void => { writeJsonFileSpy.mockRestore(); vi.clearAllMocks(); From 6c4d56ed24e8a6f2d476adb5a5be54ece5f6fe9f Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Sun, 5 Jan 2025 15:51:42 +0100 Subject: [PATCH 25/31] tests: consistent testing names, code organization --- .../src/plugin/caching.unit-test.ts | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 88a7f128..ba0dc6ad 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -5,7 +5,7 @@ import { expect, type MockInstance, } from 'vitest'; -import { JsonWriteOptions, type JsonReadOptions } from 'nx/src/utils/fileutils'; +import { type JsonWriteOptions, type JsonReadOptions } from 'nx/src/utils/fileutils'; import { getCacheRecord, readTargetsCache, @@ -31,7 +31,9 @@ describe('cacheRecord', (): void => { >; beforeEach((): void => { - cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(cacheKey); + cacheKeySpy = vi + .spyOn(cachingUtils, 'cacheKey') + .mockReturnValue(cacheKey); }); afterEach((): void => { cacheKeySpy.mockRestore(); @@ -52,12 +54,6 @@ describe('cacheRecord', (): void => { expect(getCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM)).toBeUndefined(); }); - it('should call cacheKey once with correct arguments', (): void => { - setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); - expect(cacheKeySpy).toHaveBeenCalledTimes(1); - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, MOCK_CACHE_ITEM); - }); - it('should set the cache record, and return it', (): void => { expect(setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM)).toBe( MOCK_CACHE_ITEM @@ -84,18 +80,18 @@ describe('readTargetsCache', (): void => { >; beforeEach((): void => { + vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); existsSyncSpy = vi .spyOn(nodeFs, 'existsSync') .mockReturnValue(true); readJsonFileSpy = vi .spyOn(nxDevKit, 'readJsonFile') .mockReturnValue(MOCK_TARGET_CACHE); - vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); }); afterEach((): void => { + vi.clearAllMocks(); existsSyncSpy.mockRestore(); readJsonFileSpy.mockRestore(); - vi.clearAllMocks(); }); it('should call existSync once with correct argument', (): void => { @@ -110,11 +106,11 @@ describe('readTargetsCache', (): void => { expect(readJsonFileSpy).toHaveBeenCalledWith(PATH); }); - it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => { + it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH !== false', (): void => { expect(readTargetsCache(PATH)).toEqual(MOCK_TARGET_CACHE); }); - it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => { + it('should return empty object if NX_CACHE_PROJECT_GRAPH == false', (): void => { vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); expect(readTargetsCache(PATH)).toEqual({}); }); @@ -124,7 +120,7 @@ describe('readTargetsCache', (): void => { expect(readTargetsCache(PATH)).toEqual({}); }); - it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH = false', (): void => { + it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH == false', (): void => { existsSyncSpy.mockReturnValue(false); vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false'); expect(readTargetsCache(PATH)).toEqual({}); @@ -143,8 +139,8 @@ describe('writeTargetsToCache', (): void => { .mockImplementation((): string => 'preventing writing to file by mocking imp'); }); afterEach((): void => { - writeJsonFileSpy.mockRestore(); vi.clearAllMocks(); + writeJsonFileSpy.mockRestore(); }); it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => { From 2a5bfaee398ab0172955f1e5f945e254d5c019d1 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Sun, 5 Jan 2025 15:59:30 +0100 Subject: [PATCH 26/31] tests: put import with * for spying as the last one for better readability --- .../nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 dadffe87..a3979a7b 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 @@ -5,9 +5,10 @@ import { expect, type MockInstance, } from 'vitest'; -import * as fileHasher from 'nx/src/hasher/file-hasher'; import { cacheKey } from './caching.utils'; +import * as fileHasher from 'nx/src/hasher/file-hasher'; + describe('cacheKey', (): void => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; From bbfec582d898f9d59ab1accb37cd4012680b4f47 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Mon, 6 Jan 2025 09:26:31 +0100 Subject: [PATCH 27/31] chore: revert changelog changes --- CHANGELOG.md | 69 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f0ca649..cf5b8ab0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,207 +1,227 @@ ## 0.0.0-alpha.27 (2024-12-11) + ### 🚀 Features - cleanup git status in folders that are checked in ([5e2c9b5](https://github.com/push-based/nx-verdaccio/commit/5e2c9b5)) - add skip install ([b1628ef](https://github.com/push-based/nx-verdaccio/commit/b1628ef)) - **nx-verdaccio:** add skipInstall and post script ([f2c0c86](https://github.com/push-based/nx-verdaccio/commit/f2c0c86)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.26 (2024-10-23) + ### 🩹 Fixes - add skipTeardown to env-teardown executor ([ff52749](https://github.com/push-based/nx-verdaccio/commit/ff52749)) - add skipTeardown to env-teardown executor ([664c6bd](https://github.com/push-based/nx-verdaccio/commit/664c6bd)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.25 (2024-10-22) + ### 🩹 Fixes - pkg-install unit tests ([9c6e64d](https://github.com/push-based/nx-verdaccio/commit/9c6e64d)) - npmWorkspace tests unit tests ([7485fe4](https://github.com/push-based/nx-verdaccio/commit/7485fe4)) - e2e ([10a7c76](https://github.com/push-based/nx-verdaccio/commit/10a7c76)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.24 (2024-10-20) + ### 🩹 Fixes - install optional deps with package ([abf41db](https://github.com/push-based/nx-verdaccio/commit/abf41db)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.23 (2024-10-19) + ### 🩹 Fixes - detach if not windows ([5f0e94e](https://github.com/push-based/nx-verdaccio/commit/5f0e94e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.22 (2024-10-18) + ### 🩹 Fixes - envRoot derivation in executor ([ae6b9aa](https://github.com/push-based/nx-verdaccio/commit/ae6b9aa)) - **nx-verdaccio:** move envRoot into executors ([3818fba](https://github.com/push-based/nx-verdaccio/commit/3818fba)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.21 (2024-10-15) + ### 🩹 Fixes - remove detached ([558a25e](https://github.com/push-based/nx-verdaccio/commit/558a25e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.20 (2024-10-15) + ### 🩹 Fixes - docs ([4921af8](https://github.com/push-based/nx-verdaccio/commit/4921af8)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.19 (2024-10-15) + ### 🩹 Fixes - deps ([daec92d](https://github.com/push-based/nx-verdaccio/commit/daec92d)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.18 (2024-10-15) + ### 🩹 Fixes - use info to log ([fce0cab](https://github.com/push-based/nx-verdaccio/commit/fce0cab)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.17 (2024-10-15) + ### 🩹 Fixes - forward params of nxv-e2e target ([8348d94](https://github.com/push-based/nx-verdaccio/commit/8348d94)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.16 (2024-10-15) + ### 🩹 Fixes - mark package ([8a0630b](https://github.com/push-based/nx-verdaccio/commit/8a0630b)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.15 (2024-10-15) + ### 🩹 Fixes - nx17 fix ([ebbfe6b](https://github.com/push-based/nx-verdaccio/commit/ebbfe6b)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.14 (2024-10-15) + ### 🩹 Fixes - version ([55a8bb1](https://github.com/push-based/nx-verdaccio/commit/55a8bb1)) - version 1 ([9cfa53e](https://github.com/push-based/nx-verdaccio/commit/9cfa53e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.13 (2024-10-15) + ### 🩹 Fixes - add command to make it work in older nx versions ([8cca3b7](https://github.com/push-based/nx-verdaccio/commit/8cca3b7)) - add description ([9af5dd2](https://github.com/push-based/nx-verdaccio/commit/9af5dd2)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.12 (2024-10-14) + ### 🩹 Fixes - readme 3 ([7b2422b](https://github.com/push-based/nx-verdaccio/commit/7b2422b)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.11 (2024-10-14) + ### 🩹 Fixes - readme 2 ([2540ebd](https://github.com/push-based/nx-verdaccio/commit/2540ebd)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.10 (2024-10-14) + ### 🩹 Fixes - readme ([26f1b13](https://github.com/push-based/nx-verdaccio/commit/26f1b13)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.9 (2024-10-14) + ### 🩹 Fixes - deps ([ceeb41b](https://github.com/push-based/nx-verdaccio/commit/ceeb41b)) - deps ([4f5303e](https://github.com/push-based/nx-verdaccio/commit/4f5303e)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.8 (2024-10-14) + ### 🚀 Features - **nx-verdaccio:** update plugin to enable env-setup caching ([f129325](https://github.com/push-based/nx-verdaccio/commit/f129325)) @@ -214,12 +234,13 @@ - set default values for cacheSizeTasks and options in plugin ([b782914](https://github.com/push-based/nx-verdaccio/commit/b782914)) - mock exec implementation to handle callbacks in unit tests ([88ebbd4](https://github.com/push-based/nx-verdaccio/commit/88ebbd4)) -### ❤️ Thank You +### ❤️ Thank You - getlarge ## 0.0.0-alpha.7 (2024-10-13) + ### 🚀 Features - modify package json before publishing ([3faf91c](https://github.com/push-based/nx-verdaccio/commit/3faf91c)) @@ -234,29 +255,31 @@ - refactor testing API usage ([f3e4c29](https://github.com/push-based/nx-verdaccio/commit/f3e4c29)) - refactor testing ([7425758](https://github.com/push-based/nx-verdaccio/commit/7425758)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular - Michael Hladky @BioPhoton ## 0.0.0-alpha.6 (2024-09-29) + ### 🩹 Fixes - make package public ([046a788](https://github.com/push-based/-nx-verdaccio-e2e-setup/commit/046a788)) - change JS esm module type to CommonJS ([9d9d60d](https://github.com/push-based/-nx-verdaccio-e2e-setup/commit/9d9d60d)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular ## 0.0.0-alpha.5 (2024-09-29) + ### 🩹 Fixes - fix typo ([8830e1a](https://github.com/push-based/-nx-verdaccio-e2e-setup/commit/8830e1a)) -### ❤️ Thank You +### ❤️ Thank You - Michael @rx-angular From 0f6d52348ed0b8479c9c9a8b7f6e15236c002aaa Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Mon, 6 Jan 2025 09:31:06 +0100 Subject: [PATCH 28/31] chore: disables prettier formatting for .nx directory --- .prettierignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.prettierignore b/.prettierignore index fd36f863..e93a1301 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,6 +1,5 @@ # Add files here to ignore them from prettier formatting /dist /coverage -/.nx/cache -/.nx/workspace-data +/.nx CHANGELOG.md From 1af8d1cbed834506e90b0e1eb962c08f83175f79 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Mon, 6 Jan 2025 09:50:57 +0100 Subject: [PATCH 29/31] tests: change names of describe blocks to match function names --- .../src/plugin/caching.unit-test.ts | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index ba0dc6ad..2d524719 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -8,8 +8,8 @@ import { import { type JsonWriteOptions, type JsonReadOptions } from 'nx/src/utils/fileutils'; import { getCacheRecord, - readTargetsCache, setCacheRecord, + readTargetsCache, writeTargetsToCache, } from './caching'; @@ -17,14 +17,13 @@ import * as nodeFs from 'node:fs'; import * as nxDevKit from '@nx/devkit'; import * as cachingUtils from './utils/caching.utils'; +const PREFIX = 'warcraft'; +const CACHE_KEY = 'ragnaros'; const PATH = 'azeroth'; const MOCK_CACHE_ITEM = { name: 'mocked-name' }; const MOCK_TARGET_CACHE = { ragnaros: MOCK_CACHE_ITEM }; -describe('cacheRecord', (): void => { - const prefix = 'warcraft'; - const cacheKey = 'ragnaros'; - +describe('getCacheRecord', (): void => { let cacheKeySpy: MockInstance< [prefix: string, MOCK_CACHE_ITEM: Record], string @@ -33,42 +32,58 @@ describe('cacheRecord', (): void => { beforeEach((): void => { cacheKeySpy = vi .spyOn(cachingUtils, 'cacheKey') - .mockReturnValue(cacheKey); + .mockReturnValue(CACHE_KEY); }); afterEach((): void => { cacheKeySpy.mockRestore(); }); it('should call cacheKey once with correct arguments', (): void => { - getCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM); + getCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM); expect(cacheKeySpy).toHaveBeenCalledTimes(1); - expect(cacheKeySpy).toHaveBeenCalledWith(prefix, MOCK_CACHE_ITEM); + expect(cacheKeySpy).toHaveBeenCalledWith(PREFIX, MOCK_CACHE_ITEM); }); it('should return the correct cache record if there is a cache hit', (): void => { - expect(getCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM)).toEqual(MOCK_CACHE_ITEM); + expect(getCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM)).toEqual(MOCK_CACHE_ITEM); }); it('should return undefined if there is no cache hit', (): void => { cacheKeySpy.mockReturnValue('non-existent-key'); - expect(getCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM)).toBeUndefined(); + expect(getCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM)).toBeUndefined(); + }); +}); + +describe('setCacheRecord', (): void => { + let cacheKeySpy: MockInstance< + [prefix: string, MOCK_CACHE_ITEM: Record], + string + >; + + beforeEach((): void => { + cacheKeySpy = vi + .spyOn(cachingUtils, 'cacheKey') + .mockReturnValue(CACHE_KEY); + }); + afterEach((): void => { + cacheKeySpy.mockRestore(); }); it('should set the cache record, and return it', (): void => { - expect(setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM)).toBe( - MOCK_CACHE_ITEM - ); - expect(MOCK_TARGET_CACHE).toHaveProperty(cacheKey, MOCK_CACHE_ITEM); + expect( + setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM) + ).toBe(MOCK_CACHE_ITEM); + expect(MOCK_TARGET_CACHE).toHaveProperty(CACHE_KEY, MOCK_CACHE_ITEM); }); it('should update existing cache data, and return it', (): void => { const recordToUpdate = { name: 'Soul of Sylvanas' }; - setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); - expect(setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, recordToUpdate)).toBe( - recordToUpdate - ); - expect(MOCK_TARGET_CACHE).toHaveProperty(cacheKey, recordToUpdate); + setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); + expect( + setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, recordToUpdate) + ).toBe(recordToUpdate); + expect(MOCK_TARGET_CACHE).toHaveProperty(CACHE_KEY, recordToUpdate); }); }); From 4f8c5f2b6f8e1b17c4f868dd937c9a1581917ea2 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Mon, 6 Jan 2025 10:07:38 +0100 Subject: [PATCH 30/31] self-review: add spy checking test for setCacheRecord --- projects/nx-verdaccio/src/plugin/caching.unit-test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 2d524719..6adf6be5 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -69,6 +69,12 @@ describe('setCacheRecord', (): void => { cacheKeySpy.mockRestore(); }); + it('should call cacheKey once with correct arguments', (): void => { + setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM) + expect(cacheKeySpy).toHaveBeenCalledTimes(1); + expect(cacheKeySpy).toHaveBeenCalledWith(PREFIX, MOCK_CACHE_ITEM); + }); + it('should set the cache record, and return it', (): void => { expect( setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM) From 4e88caf10a1678daa9825c5b907a627e3194bb07 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Mon, 6 Jan 2025 10:13:22 +0100 Subject: [PATCH 31/31] self-review: run nx format --- .../src/plugin/caching.unit-test.ts | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index 6adf6be5..98abb8f4 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -5,7 +5,10 @@ import { expect, type MockInstance, } from 'vitest'; -import { type JsonWriteOptions, type JsonReadOptions } from 'nx/src/utils/fileutils'; +import { + type JsonWriteOptions, + type JsonReadOptions, +} from 'nx/src/utils/fileutils'; import { getCacheRecord, setCacheRecord, @@ -30,9 +33,7 @@ describe('getCacheRecord', (): void => { >; beforeEach((): void => { - cacheKeySpy = vi - .spyOn(cachingUtils, 'cacheKey') - .mockReturnValue(CACHE_KEY); + cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(CACHE_KEY); }); afterEach((): void => { cacheKeySpy.mockRestore(); @@ -45,12 +46,16 @@ describe('getCacheRecord', (): void => { }); it('should return the correct cache record if there is a cache hit', (): void => { - expect(getCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM)).toEqual(MOCK_CACHE_ITEM); + expect(getCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM)).toEqual( + MOCK_CACHE_ITEM + ); }); it('should return undefined if there is no cache hit', (): void => { cacheKeySpy.mockReturnValue('non-existent-key'); - expect(getCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM)).toBeUndefined(); + expect( + getCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM) + ).toBeUndefined(); }); }); @@ -61,23 +66,26 @@ describe('setCacheRecord', (): void => { >; beforeEach((): void => { - cacheKeySpy = vi - .spyOn(cachingUtils, 'cacheKey') - .mockReturnValue(CACHE_KEY); + cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(CACHE_KEY); }); afterEach((): void => { cacheKeySpy.mockRestore(); }); it('should call cacheKey once with correct arguments', (): void => { - setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM) + setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM); expect(cacheKeySpy).toHaveBeenCalledTimes(1); expect(cacheKeySpy).toHaveBeenCalledWith(PREFIX, MOCK_CACHE_ITEM); }); it('should set the cache record, and return it', (): void => { expect( - setCacheRecord(MOCK_TARGET_CACHE, PREFIX, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM) + setCacheRecord( + MOCK_TARGET_CACHE, + PREFIX, + MOCK_CACHE_ITEM, + MOCK_CACHE_ITEM + ) ).toBe(MOCK_CACHE_ITEM); expect(MOCK_TARGET_CACHE).toHaveProperty(CACHE_KEY, MOCK_CACHE_ITEM); }); @@ -102,9 +110,7 @@ describe('readTargetsCache', (): void => { beforeEach((): void => { vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true'); - existsSyncSpy = vi - .spyOn(nodeFs, 'existsSync') - .mockReturnValue(true); + existsSyncSpy = vi.spyOn(nodeFs, 'existsSync').mockReturnValue(true); readJsonFileSpy = vi .spyOn(nxDevKit, 'readJsonFile') .mockReturnValue(MOCK_TARGET_CACHE); @@ -157,7 +163,9 @@ describe('writeTargetsToCache', (): void => { beforeEach((): void => { writeJsonFileSpy = vi .spyOn(nxDevKit, 'writeJsonFile') - .mockImplementation((): string => 'preventing writing to file by mocking imp'); + .mockImplementation( + (): string => 'preventing writing to file by mocking imp' + ); }); afterEach((): void => { vi.clearAllMocks();