From 11109d7be8d2688c019fe62ca6f16aff2d712216 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 12 Dec 2024 16:25:42 +0100 Subject: [PATCH] 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 52d2901..0ea5141 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 0000000..eb49e01 --- /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.', + }, +};