Skip to content

Commit

Permalink
tests: read targets cache optimization, better naming, polishing code
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianRomanski committed Dec 12, 2024
1 parent 11109d7 commit be99d62
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions projects/nx-verdaccio/src/plugin/caching.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof vi.spyOn>;
Expand Down Expand Up @@ -89,16 +90,22 @@ 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],
object
>;

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<string, Partial<ProjectConfiguration>> => {
return { mockKey: MOCK_PROJECT_CONFIGURATION }
});
process.env.NX_CACHE_PROJECT_GRAPH = 'true';
});

Expand All @@ -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<string, Partial<ProjectConfiguration>> => {
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<string, Partial<ProjectConfiguration>> => {
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({});
});
});
Expand Down

0 comments on commit be99d62

Please sign in to comment.