Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Netzer committed Mar 15, 2022
1 parent fcb1e9a commit 5d50764
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 45 deletions.
104 changes: 64 additions & 40 deletions packages/nx-distributed-task/src/app/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,79 @@ import { restoreNxCache, saveNxCache } from '@e-square/utils/cache';

import { restoreCache, saveCache } from './cache';
import { context } from '@actions/github';
import { Task } from '@e-square/utils/task';
import { TaskGraph } from '@nrwl/tao/src/shared/tasks';

jest.mock('@e-square/utils/cache');
jest.mock('@e-square/utils/logger');

describe('cache', () => {
let task: Task;
let depTask: Task;
let graph: TaskGraph;

beforeEach(() => {
task = {
id: 'test:build',
overrides: {},
outputs: ['test'],
target: { target: 'build', project: 'test' },
hash: 'test',
};
depTask = { ...task, id: 'dep:build' };

graph = {
tasks: {
'test:build': task,
},
roots: [],
dependencies: {},
};
});

it('should call cache utils', async () => {
await restoreCache(
context,
[
{
id: 'test:build',
overrides: {},
outputs: ['test'],
target: { target: 'build', project: 'test' },
hash: 'test',
},
],
{
tasks: {},
roots: [],
dependencies: {},
}
);
await saveCache(
context,
[
{
id: 'test:build',
overrides: {},
outputs: ['test'],
target: { target: 'build', project: 'test' },
hash: 'test',
},
],
{
await restoreCache(context, [task], graph);
await saveCache(context, [task], graph);

expect(saveNxCache).toHaveBeenCalled();
expect(restoreNxCache).toHaveBeenCalled();
});

describe('restoring deps', () => {
let secondTask: Task;

beforeEach(() => {
secondTask = { ...task, id: 'test2:build' };

graph = {
...graph,
tasks: {
'test:build': {
id: 'test:build',
overrides: {},
target: { target: 'build', project: 'test' },
hash: 'test',
},
...graph.tasks,
'test2:build': secondTask,
'dep:build': depTask,
},
roots: [],
dependencies: {},
}
);
dependencies: {
'test:build': ['dep:build'],
'test2:build': ['dep:build'],
},
};
});

expect(saveNxCache).toHaveBeenCalled();
expect(restoreNxCache).toHaveBeenCalled();
it('should restore deps cache if task cache is missing', async () => {
await restoreCache(context, [task], graph);

expect(restoreNxCache).toHaveBeenNthCalledWith(1, context, task);
expect(restoreNxCache).toHaveBeenNthCalledWith(2, context, depTask);
});

it('should not try to restore deps cache twice', async () => {
await restoreCache(context, [task, secondTask], graph);

expect(restoreNxCache).toHaveBeenNthCalledWith(1, context, task);
expect(restoreNxCache).toHaveBeenNthCalledWith(2, context, depTask);
expect(restoreNxCache).toHaveBeenNthCalledWith(3, context, secondTask);
expect(restoreNxCache).not.toHaveBeenNthCalledWith(4, context, depTask);
});
});
});
12 changes: 12 additions & 0 deletions packages/utils/src/lib/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ describe('cache', () => {

logger().debugMode = false;
});

it('should not restore cache if hash is missing', async () => {
await restoreNxCache(context, { ...task, hash: undefined });

expect(restoreCache).not.toHaveBeenCalled();
});
});

describe('saveNxCache', () => {
Expand Down Expand Up @@ -86,6 +92,12 @@ describe('cache', () => {
logger().debugMode = false;
});

it('should not save cache if hash is missing', async () => {
await saveNxCache(context, { ...task, hash: undefined });

expect(saveCache).not.toHaveBeenCalled();
});

it('should not save if cache hit occurred on primary key', async () => {
await saveNxCache(context, { ...task, cacheKey: 'nx-cache-test-0' });
});
Expand Down
5 changes: 0 additions & 5 deletions packages/utils/src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ export async function saveNxCache(context: typeof Context, task: Task): Promise<

const [primaryKey] = getCacheKeys(task.hash, context);

if (!primaryKey) {
info(`Couldn't find the primary key, skipping saving cache`);
return;
}

if (isExactKeyMatch(primaryKey, task.cacheKey)) {
info(`Cache hit occurred on the primary key ${task.cacheKey}, not saving cache.`);
return;
Expand Down

0 comments on commit 5d50764

Please sign in to comment.