Skip to content

Commit

Permalink
tests: mockReturnValue instead of implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianRomanski committed Jan 5, 2025
1 parent 07bfa75 commit 42afe92
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions projects/nx-verdaccio/src/plugin/caching.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('cacheRecord', (): void => {
beforeEach((): void => {
cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(cacheKey);
});

afterEach((): void => {
cacheKeySpy.mockRestore();
});
Expand Down Expand Up @@ -68,8 +67,8 @@ describe('cacheRecord', (): void => {

it('should update existing cache data, and return it', (): void => {
const recordToUpdate = { name: 'Soul of Sylvanas' };
setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM);

setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, MOCK_CACHE_ITEM);
expect(setCacheRecord(MOCK_TARGET_CACHE, prefix, MOCK_CACHE_ITEM, recordToUpdate)).toBe(
recordToUpdate
);
Expand All @@ -87,15 +86,12 @@ describe('readTargetsCache', (): void => {
beforeEach((): void => {
existsSyncSpy = vi
.spyOn(nodeFs, 'existsSync')
.mockImplementation((): boolean => true);
.mockReturnValue(true);
readJsonFileSpy = vi
.spyOn(nxDevKit, 'readJsonFile')
.mockImplementation(() => {
return MOCK_TARGET_CACHE;
});
.mockReturnValue(MOCK_TARGET_CACHE);
vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true');
});

afterEach((): void => {
existsSyncSpy.mockRestore();
readJsonFileSpy.mockRestore();
Expand Down Expand Up @@ -124,12 +120,12 @@ describe('readTargetsCache', (): void => {
});

it('should return empty object if existsSync returns false', (): void => {
existsSyncSpy.mockImplementation((): boolean => false);
existsSyncSpy.mockReturnValue(false);
expect(readTargetsCache(PATH)).toEqual({});
});

it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH = false', (): void => {
existsSyncSpy.mockImplementation((): boolean => false);
existsSyncSpy.mockReturnValue(false);
vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'false');
expect(readTargetsCache(PATH)).toEqual({});
});
Expand All @@ -144,9 +140,8 @@ describe('writeTargetsToCache', (): void => {
beforeEach((): void => {
writeJsonFileSpy = vi
.spyOn(nxDevKit, 'writeJsonFile')
.mockImplementation((): string => 'preventing writing to file by mocking impl');
.mockImplementation((): string => 'preventing writing to file by mocking imp');
});

afterEach((): void => {
writeJsonFileSpy.mockRestore();
vi.clearAllMocks();
Expand Down

0 comments on commit 42afe92

Please sign in to comment.