Skip to content

Commit 9052763

Browse files
committed
test: use .toHaveBeenCalledOnce() and .not.toHaveBeenCalled() for easier reading of tests
1 parent a1d62a0 commit 9052763

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

src/run/storage/storage.test.ts

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('getMemoizedKeyValueStoreBackedByRegionalBlobStore', () => {
9090
const store = getMemoizedKeyValueStoreBackedByRegionalBlobStore()
9191
const get1 = await store.get(TEST_KEY, OTEL_SPAN_TITLE)
9292

93-
expect(mockedStore.getWithMetadata, 'Blobs should be requested').toHaveBeenCalledTimes(1)
93+
expect(mockedStore.getWithMetadata, 'Blobs should be requested').toHaveBeenCalledOnce()
9494
expect(get1, 'Expected blob should be returned').toBe(TEST_DEFAULT_VALUE)
9595

9696
const get2 = await store.get(TEST_KEY, OTEL_SPAN_TITLE)
@@ -103,14 +103,14 @@ describe('getMemoizedKeyValueStoreBackedByRegionalBlobStore', () => {
103103
const store = getMemoizedKeyValueStoreBackedByRegionalBlobStore()
104104

105105
const get1 = await store.get(TEST_KEY, OTEL_SPAN_TITLE)
106-
expect(mockedStore.getWithMetadata, 'Blobs should be requested').toHaveBeenCalledTimes(1)
106+
expect(mockedStore.getWithMetadata, 'Blobs should be requested').toHaveBeenCalledOnce()
107107
expect(get1, 'Expected blob should be returned').toBe(TEST_DEFAULT_VALUE)
108108

109109
const get2 = await store.get(TEST_KEY, OTEL_SPAN_TITLE)
110110
expect(
111111
mockedStore.getWithMetadata,
112112
'Blobs should be requested just once',
113-
).toHaveBeenCalledTimes(1)
113+
).toHaveBeenCalledOnce()
114114
expect(get2, 'Expected second .get to return the same as first one').toBe(get1)
115115
})
116116
})
@@ -126,12 +126,10 @@ describe('getMemoizedKeyValueStoreBackedByRegionalBlobStore', () => {
126126

127127
await store.set(TEST_KEY, writeValue, OTEL_SPAN_TITLE)
128128

129-
expect(mockedStore.setJSON, 'Blobs should be posted').toHaveBeenCalledTimes(1)
129+
expect(mockedStore.setJSON, 'Blobs should be posted').toHaveBeenCalledOnce()
130130

131131
const get = await store.get(TEST_KEY, OTEL_SPAN_TITLE)
132-
expect(mockedStore.getWithMetadata, 'Value should be read from memory').toHaveBeenCalledTimes(
133-
0,
134-
)
132+
expect(mockedStore.getWithMetadata, 'Value should be read from memory').not.toHaveBeenCalled()
135133
expect(get, 'Value from memory should be correct').toBe(writeValue)
136134
})
137135
})
@@ -213,16 +211,14 @@ describe('getMemoizedKeyValueStoreBackedByRegionalBlobStore', () => {
213211
await runWithRequestContext(requestContext1, async () => {
214212
const get = await store.get(TEST_KEY, OTEL_SPAN_TITLE)
215213
expect(get, 'Value from memory should be the same as before').toBe(TEST_DEFAULT_VALUE)
216-
expect(mockedStore.getWithMetadata, 'Blobs should be requested').toHaveBeenCalledTimes(1)
214+
expect(mockedStore.getWithMetadata, 'Blobs should be requested').toHaveBeenCalledOnce()
217215
})
218216

219217
await runWithRequestContext(requestContext2, async () => {
220218
mockedStore.getWithMetadata.mockClear()
221219
await store.set(TEST_KEY, writeValue, OTEL_SPAN_TITLE)
222220
const get = await store.get(TEST_KEY, OTEL_SPAN_TITLE)
223-
expect(mockedStore.getWithMetadata, 'Value should be read from memory').toHaveBeenCalledTimes(
224-
0,
225-
)
221+
expect(mockedStore.getWithMetadata, 'Value should be read from memory').not.toHaveBeenCalled()
226222
expect(get, 'Value from memory should be correct').toBe(writeValue)
227223
})
228224

@@ -233,9 +229,7 @@ describe('getMemoizedKeyValueStoreBackedByRegionalBlobStore', () => {
233229
get,
234230
'Value from memory should be the same as before and not affected by other request context',
235231
).toBe(TEST_DEFAULT_VALUE)
236-
expect(mockedStore.getWithMetadata, 'Value should be read from memory').toHaveBeenCalledTimes(
237-
0,
238-
)
232+
expect(mockedStore.getWithMetadata, 'Value should be read from memory').not.toHaveBeenCalled()
239233
})
240234
})
241235

@@ -253,65 +247,51 @@ describe('getMemoizedKeyValueStoreBackedByRegionalBlobStore', () => {
253247

254248
await runWithRequestContext(requestContext1, async () => {
255249
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
256-
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledTimes(
257-
1,
258-
)
250+
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledOnce()
259251
mockedStore.getWithMetadata.mockClear()
260252

261253
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
262-
expect(mockedStore.getWithMetadata, 'Value should be read from memory').toHaveBeenCalledTimes(
263-
0,
264-
)
254+
expect(mockedStore.getWithMetadata, 'Value should be read from memory').not.toHaveBeenCalled()
265255
mockedStore.getWithMetadata.mockClear()
266256

267257
await store.get('heavy-route-2', OTEL_SPAN_TITLE)
268-
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledTimes(
269-
1,
270-
)
258+
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledOnce()
271259
mockedStore.getWithMetadata.mockClear()
272260

273261
// at this point we should exceed the memory limit and least recently used value should be evicted
274262
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
275263
expect(
276264
mockedStore.getWithMetadata,
277265
'Previously stored in-memory value should be evicted and fresh value should be read from blobs',
278-
).toHaveBeenCalledTimes(1)
266+
).toHaveBeenCalledOnce()
279267
mockedStore.getWithMetadata.mockClear()
280268

281269
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
282270
expect(
283271
mockedStore.getWithMetadata,
284272
'Value should be read from memory again',
285-
).toHaveBeenCalledTimes(0)
273+
).not.toHaveBeenCalled()
286274
mockedStore.getWithMetadata.mockClear()
287275
})
288276

289277
await runWithRequestContext(requestContext2, async () => {
290278
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
291-
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledTimes(
292-
1,
293-
)
279+
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledOnce()
294280
mockedStore.getWithMetadata.mockClear()
295281

296282
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
297-
expect(mockedStore.getWithMetadata, 'Value should be read from memory').toHaveBeenCalledTimes(
298-
0,
299-
)
283+
expect(mockedStore.getWithMetadata, 'Value should be read from memory').not.toHaveBeenCalled()
300284
mockedStore.getWithMetadata.mockClear()
301285
})
302286

303287
await runWithRequestContext(requestContext1, async () => {
304288
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
305289
// operations in requestContext2 should result in evicting value for requestContext1
306-
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledTimes(
307-
1,
308-
)
290+
expect(mockedStore.getWithMetadata, 'Value should be read from blobs').toHaveBeenCalledOnce()
309291
mockedStore.getWithMetadata.mockClear()
310292

311293
await store.get('heavy-route-1', OTEL_SPAN_TITLE)
312-
expect(mockedStore.getWithMetadata, 'Value should be read from memory').toHaveBeenCalledTimes(
313-
0,
314-
)
294+
expect(mockedStore.getWithMetadata, 'Value should be read from memory').not.toHaveBeenCalled()
315295
mockedStore.getWithMetadata.mockClear()
316296
})
317297
})

0 commit comments

Comments
 (0)