1
- import { redisStore } from '../src' ;
2
- import { RedisStore } from "../src/types" ;
3
- import { describe , beforeEach , it , expect } from "vitest" ;
1
+ import { redisStore } from '../src' ;
2
+ import { RedisStore } from "../src/types" ;
3
+ import { describe , beforeEach , it , expect , afterEach } from "vitest" ;
4
4
5
5
let redisClient : RedisStore
6
6
const config = {
@@ -12,10 +12,16 @@ const config = {
12
12
db : 0 ,
13
13
ttl : 1000 * 60 ,
14
14
} ;
15
+
15
16
beforeEach ( async ( ) => {
16
17
redisClient = await redisStore ( config ) ;
17
18
await redisClient . reset ( ) ;
18
19
} ) ;
20
+
21
+ afterEach ( async ( ) => {
22
+ await redisClient . flushAll ( )
23
+ } ) ;
24
+
19
25
describe ( 'Redis Store' , ( ) => {
20
26
21
27
it ( 'should set and get a value' , async ( ) => {
@@ -80,7 +86,7 @@ describe('Redis Store', () => {
80
86
expect ( retrievedTtl ) . toBeLessThanOrEqual ( ttl / 1000 ) ; // Redis returns TTL in seconds
81
87
} ) ;
82
88
83
- it ( ` should return scan result by pattern` , async ( ) => {
89
+ it ( ' should return scan result by pattern' , async ( ) => {
84
90
const key1 = 'ttl:a:b' ;
85
91
const key2 = 'ttl1:a:b' ;
86
92
const key3 = 'ttl:a:b1' ;
@@ -106,4 +112,16 @@ describe('Redis Store', () => {
106
112
expect ( thirdScanWithCount . keys ) . toEqual ( [ ] ) ;
107
113
expect ( thirdScanWithCount . cursor ) . equal ( 0 ) ;
108
114
} ) ;
115
+
116
+ it ( 'should inc value by one' , async ( ) => {
117
+ await redisClient . set ( 'test' , { a : 1 } ) ;
118
+ const res = await redisClient . atomicGetAndSet ( 'test' , ( obj ) => {
119
+ const parsedVal = obj ;
120
+ parsedVal . a = parsedVal . a + 1 ;
121
+ return JSON . stringify ( parsedVal ) ;
122
+ } ) ;
123
+ expect ( JSON . parse ( res [ 1 ] ) ) . to . deep . equal ( { a : 2 } ) ;
124
+ expect ( res [ 0 ] ) . to . deep . equal ( "OK" ) ;
125
+ } )
126
+
109
127
} ) ;
0 commit comments