File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
2
* @vitest -environment happy-dom
3
3
*/
4
- import { test , expect , describe } from "vitest" ;
4
+ import { test , expect , describe , vi } from "vitest" ;
5
5
import ws from "ws" ;
6
6
7
7
import { ConvexReactClient , createMutation , useQuery } from "./client.js" ;
@@ -112,6 +112,22 @@ describe("useQuery", () => {
112
112
) ;
113
113
expect ( result . current ) . toStrictEqual ( undefined ) ;
114
114
} ) ;
115
+
116
+ test ( "Optimistic update handlers can’t be async" , ( ) => {
117
+ const client = testConvexReactClient ( ) ;
118
+ const mutation = createMutation (
119
+ anyApi . myMutation . default ,
120
+ client ,
121
+ // @ts -expect-error
122
+ ) . withOptimisticUpdate ( async ( ) => { } ) ;
123
+
124
+ // Calling the mutation should warn in the console
125
+ const consoleWarnSpy = vi . spyOn ( console , "warn" ) ;
126
+ void mutation ( ) ;
127
+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
128
+ "Optimistic update handler returned a Promise. Optimistic updates should be synchronous." ,
129
+ ) ;
130
+ } ) ;
115
131
} ) ;
116
132
117
133
// Intentionally disabled because we're only testing types
Original file line number Diff line number Diff line change @@ -68,8 +68,11 @@ export interface ReactMutation<Mutation extends FunctionReference<"mutation">> {
68
68
*
69
69
* @public
70
70
*/
71
- withOptimisticUpdate (
72
- optimisticUpdate : OptimisticUpdate < FunctionArgs < Mutation > > ,
71
+ withOptimisticUpdate < T extends OptimisticUpdate < FunctionArgs < Mutation > > > (
72
+ optimisticUpdate : T &
73
+ ( ReturnType < T > extends Promise < any >
74
+ ? "Optimistic update handlers must be synchronous"
75
+ : { } ) ,
73
76
) : ReactMutation < Mutation > ;
74
77
}
75
78
You can’t perform that action at this time.
0 commit comments