|
1 |
| -import { CacheManager, MemoryCacheAdapter } from "../cache.ts"; // Adjust the import based on your project structure |
| 1 | +import { CacheManager, MemoryCacheAdapter } from "../cache.ts"; |
| 2 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
2 | 3 |
|
3 |
| -// Now, let’s fix the test suite. |
4 |
| - |
5 |
| -describe.only("CacheManager", () => { |
| 4 | +describe("CacheManager", () => { |
6 | 5 | let cache: CacheManager<MemoryCacheAdapter>;
|
7 | 6 |
|
8 |
| - jest.useFakeTimers(); |
9 |
| - |
10 | 7 | beforeEach(() => {
|
| 8 | + vi.useFakeTimers(); |
11 | 9 | cache = new CacheManager(new MemoryCacheAdapter());
|
12 |
| - jest.setSystemTime(Date.now()); |
| 10 | + vi.setSystemTime(Date.now()); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + vi.useRealTimers(); |
13 | 15 | });
|
14 | 16 |
|
15 | 17 | it("should set/get/delete cache", async () => {
|
16 | 18 | await cache.set("foo", "bar");
|
17 | 19 |
|
18 | 20 | expect(await cache.get("foo")).toEqual("bar");
|
19 | 21 |
|
20 |
| - expect(cache.adapter.data.get("foo")).toEqual( |
21 |
| - JSON.stringify({ value: "bar", expires: 0 }) |
22 |
| - ); |
23 |
| - |
24 | 22 | await cache.delete("foo");
|
25 | 23 |
|
26 | 24 | expect(await cache.get("foo")).toEqual(undefined);
|
27 |
| - expect(cache.adapter.data.get("foo")).toEqual(undefined); |
28 | 25 | });
|
29 | 26 |
|
30 |
| - it("should set/get/delete cache with expiration", async () => { |
31 |
| - const expires = Date.now() + 5 * 1000; |
| 27 | + it("should handle expiring cache", async () => { |
| 28 | + const expires = Date.now() + 1000; |
32 | 29 |
|
33 |
| - await cache.set("foo", "bar", { expires: expires }); |
| 30 | + await cache.set("foo", "bar", { expires }); |
34 | 31 |
|
35 | 32 | expect(await cache.get("foo")).toEqual("bar");
|
36 | 33 |
|
37 | 34 | expect(cache.adapter.data.get("foo")).toEqual(
|
38 | 35 | JSON.stringify({ value: "bar", expires: expires })
|
39 | 36 | );
|
40 | 37 |
|
41 |
| - jest.setSystemTime(expires + 1000); |
| 38 | + vi.setSystemTime(expires + 1000); |
42 | 39 |
|
43 | 40 | expect(await cache.get("foo")).toEqual(undefined);
|
44 | 41 | expect(cache.adapter.data.get("foo")).toEqual(undefined);
|
|
0 commit comments