Skip to content

Commit

Permalink
tests: writeTargetsToCache, final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianRomanski committed Dec 12, 2024
1 parent 17bfd35 commit 5b1ad17
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 90 deletions.
2 changes: 1 addition & 1 deletion projects/nx-verdaccio/src/plugin/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function readTargetsCache(
export function writeTargetsToCache(
cachePath: string,
results: Record<string, Partial<ProjectConfiguration>>
) {
): void {
process.env.NX_CACHE_PROJECT_GRAPH !== 'false' &&
writeJsonFile(cachePath, results);
}
110 changes: 76 additions & 34 deletions projects/nx-verdaccio/src/plugin/caching.unit-test.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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();
});
Expand All @@ -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();
});
});
Expand All @@ -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);
Expand All @@ -77,29 +88,38 @@ 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],
object
>;

beforeEach((): void => {
existsSyncSpy = vi.spyOn(nodeFs, 'existsSync')
existsSyncSpy = vi
.spyOn(nodeFs, 'existsSync')
.mockImplementation((): boolean => true);
readJsonFileSpy = vi.spyOn(nxDevKit, 'readJsonFile')
.mockImplementation((): Record<string, Partial<ProjectConfiguration>> => {
return { mockKey: MOCK_PROJECT_CONFIGURATION }
});
readJsonFileSpy = vi
.spyOn(nxDevKit, 'readJsonFile')
.mockImplementation(
(): Record<string, Partial<ProjectConfiguration>> => {
return MOCK_TARGETS_CACHE;
}
);
process.env.NX_CACHE_PROJECT_GRAPH = 'true';
});

Expand All @@ -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);
});
});
});
55 changes: 0 additions & 55 deletions projects/nx-verdaccio/src/plugin/constants.unit-test.ts

This file was deleted.

60 changes: 60 additions & 0 deletions testing/test-utils/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -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<string, Partial<ProjectConfiguration>> = {
mockKey: MOCK_PROJECT_CONFIGURATION
}

0 comments on commit 5b1ad17

Please sign in to comment.