From 5b1ad171fd340d2b12904b606c00da626a03b335 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 17:28:14 +0100 Subject: [PATCH] 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 +}