Skip to content

Commit

Permalink
tests: read target cache - mock read json file
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianRomanski committed Dec 12, 2024
1 parent 5528725 commit 89baebf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
4 changes: 1 addition & 3 deletions projects/nx-verdaccio/src/plugin/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ export function setCacheRecord<T>(
export function readTargetsCache(
cachePath: string
): Record<string, Partial<ProjectConfiguration>> {
// that part is ready
const mockedExistsSync = existsSync(cachePath);
return process.env.NX_CACHE_PROJECT_GRAPH !== 'false' && existsSync(cachePath)
? readJsonFile(cachePath)
? readJsonFile(cachePath)
: {};
}

Expand Down
76 changes: 73 additions & 3 deletions projects/nx-verdaccio/src/plugin/caching.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { afterEach, beforeEach, describe, expect } 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 { readTargetsCache, setCacheRecord } from './caching';
import { cacheKey } from './utils/caching.utils';
import { ProjectConfiguration } from '@nx/devkit';

describe('caching', () => {
const prefix = 'warcraft';
Expand Down Expand Up @@ -87,10 +89,76 @@ describe('caching', () => {
// : {};
// }
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<string, Partial<ProjectConfiguration>> => {
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.',
},
};


// beforeEach((): void => {
// existsSyncSpy = vi.spyOn(cachingUtils, 'cacheKey');
// });
Expand All @@ -99,11 +167,13 @@ describe('caching', () => {
existsSyncSpy.mockRestore();
});

it('should call cacheKey with the correct arguments', (): void => {
readTargetsCache('test');

it('should call cacheKey with the correct arguments and return from json file', (): void => {
process.env.NX_CACHE_PROJECT_GRAPH = 'true';
const targetsCacheResult = readTargetsCache('test');
expect(existsSyncSpy).toHaveBeenCalledWith('test');
expect(existsSyncSpy).toHaveBeenCalledTimes(1);
expect(readJsonFileSpy).toHaveBeenCalledTimes(1);
expect(targetsCacheResult).toStrictEqual({'mockKey': mockProjectConfiguration});
});
});
})

0 comments on commit 89baebf

Please sign in to comment.