|
1 | 1 | import cache from '../memoization/cache';
|
2 |
| -import { reset as cacheReset } from '../memoization/inMemoryCacheStorage'; |
| 2 | +import { reset as inDiskCacheReset } from '../memoization/inFileCacheStorage'; |
| 3 | +import { reset as inMemoryCacheReset } from '../memoization/inMemoryCacheStorage'; |
3 | 4 | import waitFor from '../timers/waitFor';
|
4 | 5 | import { StorageOptions } from './cacheStorage';
|
5 | 6 |
|
6 | 7 | describe(`utils/memoization/cache.ts`, () => {
|
7 | 8 | beforeEach(() => {
|
8 | 9 | // Silent console log (used by logger.debug)
|
9 |
| - // @ts-ignore-error |
10 |
| - global.console = { debug: jest.fn(), log: jest.fn() }; |
| 10 | + // @ts-ignore |
| 11 | + global.console = global.muteConsole(); |
11 | 12 | });
|
12 | 13 |
|
13 | 14 | describe(`cache`, () => {
|
14 | 15 | describe(`should not fetch multiple times in a row but rely on the cache instead`, () => {
|
15 |
| - beforeEach(() => { |
16 |
| - cacheReset(); |
| 16 | + afterEach(() => { |
| 17 | + inMemoryCacheReset(); |
17 | 18 | });
|
18 | 19 | const expectationResult = { key: 'value' };
|
19 | 20 |
|
@@ -62,47 +63,39 @@ describe(`utils/memoization/cache.ts`, () => {
|
62 | 63 | });
|
63 | 64 |
|
64 | 65 | describe(`when using file storage (on disk)`, () => {
|
65 |
| - const storageOptions: StorageOptions = { storage: { type: 'disk', options: { filename: '.test-cache1' } } }; |
| 66 | + const storageOptions: StorageOptions = { storage: { type: 'disk', options: { filename: 'test-cache1.cache' } } }; |
| 67 | + const key = 'key'; |
66 | 68 |
|
67 | 69 | test(`when using the default TTL`, async () => {
|
68 |
| - const cacheHitsBefore = require('../memoization/inMemoryCacheStorage').cacheHits; |
69 |
| - const cacheMissBefore = require('../memoization/inMemoryCacheStorage').cacheMiss; |
70 |
| - expect(await cache('key', async () => { |
| 70 | + expect(await cache(key, async () => { |
71 | 71 | await waitFor(1);
|
72 | 72 | return expectationResult;
|
73 | 73 | }, storageOptions)).toEqual(expectationResult);
|
74 |
| - expect(await cache('key', async () => { |
| 74 | + expect(await cache(key, async () => { |
75 | 75 | await waitFor(1);
|
76 | 76 | return Promise.resolve(expectationResult);
|
77 | 77 | }, storageOptions)).toEqual(expectationResult);
|
78 | 78 |
|
79 |
| - const cacheHitsAfter = require('../memoization/inMemoryCacheStorage').cacheHits; |
80 |
| - const cacheMissAfter = require('../memoization/inMemoryCacheStorage').cacheMiss; |
81 |
| - expect(cacheHitsAfter).toBeGreaterThan(cacheHitsBefore); |
82 |
| - expect(cacheMissAfter).toEqual(cacheMissBefore + 1); // Cache should have been missed only for the first call |
| 79 | + await inDiskCacheReset(key, storageOptions); |
83 | 80 | });
|
84 | 81 |
|
85 | 82 | describe(`should fetch multiple times and miss the cache`, () => {
|
86 | 83 | const expectationResult = { key2: 'value2' };
|
| 84 | + const key = 'key2'; |
87 | 85 |
|
88 | 86 | test(`when using TTL of 1 second and waiting more than 1 second between calls`, async () => {
|
89 |
| - const cacheHitsBefore = require('../memoization/inMemoryCacheStorage').cacheHits; |
90 |
| - const cacheMissBefore = require('../memoization/inMemoryCacheStorage').cacheMiss; |
91 | 87 | await waitFor(1001);
|
92 |
| - expect(await cache('key2', async () => { |
| 88 | + expect(await cache(key, async () => { |
93 | 89 | await waitFor(1);
|
94 | 90 | return Promise.resolve(expectationResult);
|
95 | 91 | }, storageOptions)).toEqual(expectationResult);
|
96 | 92 | await waitFor(1001);
|
97 |
| - expect(await cache('key2', async () => { |
| 93 | + expect(await cache(key, async () => { |
98 | 94 | await waitFor(1);
|
99 | 95 | return Promise.resolve(expectationResult);
|
100 | 96 | }, storageOptions)).toEqual(expectationResult);
|
101 | 97 |
|
102 |
| - const cacheHitsAfter = require('../memoization/inMemoryCacheStorage').cacheHits; |
103 |
| - const cacheMissAfter = require('../memoization/inMemoryCacheStorage').cacheMiss; |
104 |
| - expect(cacheHitsAfter).toEqual(cacheHitsBefore + 1); |
105 |
| - expect(cacheMissAfter).toBeGreaterThan(cacheMissBefore); |
| 98 | + await inDiskCacheReset(key, storageOptions); |
106 | 99 | });
|
107 | 100 | });
|
108 | 101 | });
|
|
0 commit comments