1
1
import * as fs from 'fs-extra' ;
2
2
import { DiskCacheServiceImpl } from '../disk-cache.service' ;
3
3
4
+ // To quickly skip past the debounced write to disk time.
5
+ jest . useFakeTimers ( ) ;
6
+
4
7
describe ( 'disk-cache-service' , ( ) => {
5
8
const filepath = '/tmp/dsa2d' ;
6
9
@@ -10,6 +13,8 @@ describe('disk-cache-service', () => {
10
13
11
14
afterEach ( ( ) => {
12
15
fs . removeSync ( filepath ) ;
16
+ jest . clearAllMocks ( ) ;
17
+ jest . clearAllTimers ( ) ;
13
18
} ) ;
14
19
15
20
test ( 'set - primitive' , async ( ) => {
@@ -21,6 +26,8 @@ describe('disk-cache-service', () => {
21
26
22
27
await cacheService . set ( 'key' , 42 ) ;
23
28
29
+ await jest . advanceTimersToNextTimerAsync ( ) ;
30
+
24
31
const cacheAfter = await fs . readJson ( filepath ) ;
25
32
26
33
expect ( cacheBefore . key ) . toEqual ( undefined ) ;
@@ -36,6 +43,8 @@ describe('disk-cache-service', () => {
36
43
37
44
await cacheService . set ( 'key' , { value : 42 } ) ;
38
45
46
+ await jest . advanceTimersToNextTimerAsync ( ) ;
47
+
39
48
const cacheAfter = await fs . readJson ( filepath ) ;
40
49
41
50
expect ( cacheBefore . key ) . toEqual ( undefined ) ;
@@ -87,6 +96,8 @@ describe('disk-cache-service', () => {
87
96
88
97
await cacheService . remove ( 'key' ) ;
89
98
99
+ await jest . advanceTimersToNextTimerAsync ( ) ;
100
+
90
101
const cacheAfter = await fs . readJson ( filepath ) ;
91
102
92
103
expect ( cacheBefore . key ) . toEqual ( 42 ) ;
@@ -104,6 +115,8 @@ describe('disk-cache-service', () => {
104
115
105
116
await cacheService . remove ( 'key' ) ;
106
117
118
+ await jest . advanceTimersToNextTimerAsync ( ) ;
119
+
107
120
const cacheAfter = await fs . readJson ( filepath ) ;
108
121
109
122
expect ( cacheBefore . key ) . toEqual ( { value : 42 } ) ;
@@ -119,6 +132,8 @@ describe('disk-cache-service', () => {
119
132
120
133
await cacheService . remove ( 'non-existant-key' ) ;
121
134
135
+ await jest . advanceTimersToNextTimerAsync ( ) ;
136
+
122
137
const cacheAfter = await fs . readJson ( filepath ) ;
123
138
124
139
expect ( cacheBefore . key ) . toEqual ( undefined ) ;
@@ -136,6 +151,8 @@ describe('disk-cache-service', () => {
136
151
137
152
await cacheService . clear ( ) ;
138
153
154
+ await jest . advanceTimersToNextTimerAsync ( ) ;
155
+
139
156
const cacheAfter = await fs . readJson ( filepath ) ;
140
157
141
158
expect ( cacheBefore ) . toEqual ( { key : { value : 42 } } ) ;
0 commit comments