From 3c5214545e73df68c16dfdf5f76261bf0e9df376 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 3 Jan 2025 13:56:41 +0100 Subject: [PATCH] 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 3ca4ba3..0000000 --- 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 92da088..e74ffbd 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); }); });