@@ -25,10 +25,96 @@ const convexModules = (import.meta as ImportMetaWithGlob).glob([
2525] ) ;
2626const relations = requireSchemaRelations ( schema ) ;
2727
28+ type TestIdentity = Parameters <
29+ ReturnType < typeof baseConvexTest > [ 'withIdentity' ]
30+ > [ 0 ] ;
31+
32+ const serializeDatesForConvexTest = ( value : unknown ) : unknown => {
33+ if ( value instanceof Date ) {
34+ return value . getTime ( ) ;
35+ }
36+
37+ if ( Array . isArray ( value ) ) {
38+ let serialized : unknown [ ] | undefined ;
39+
40+ for ( let index = 0 ; index < value . length ; index += 1 ) {
41+ const entry = value [ index ] ;
42+ const encoded = serializeDatesForConvexTest ( entry ) ;
43+ if ( encoded !== entry ) {
44+ if ( ! serialized ) {
45+ serialized = value . slice ( ) ;
46+ }
47+ serialized [ index ] = encoded ;
48+ }
49+ }
50+
51+ return serialized ?? value ;
52+ }
53+
54+ if ( ! value || typeof value !== 'object' ) {
55+ return value ;
56+ }
57+
58+ const prototype = Object . getPrototypeOf ( value ) ;
59+ const isSimpleObject =
60+ prototype === null ||
61+ prototype === Object . prototype ||
62+ prototype ?. constructor ?. name === 'Object' ;
63+ if ( ! isSimpleObject ) {
64+ return value ;
65+ }
66+
67+ const record = value as Record < string , unknown > ;
68+ let serialized : Record < string , unknown > | undefined ;
69+
70+ for ( const key in record ) {
71+ if ( ! Object . hasOwn ( record , key ) ) {
72+ continue ;
73+ }
74+
75+ const entry = record [ key ] ;
76+ const encoded = serializeDatesForConvexTest ( entry ) ;
77+ if ( encoded !== entry ) {
78+ if ( ! serialized ) {
79+ serialized = { ...record } ;
80+ }
81+ serialized [ key ] = encoded ;
82+ }
83+ }
84+
85+ return serialized ?? value ;
86+ } ;
87+
88+ const wrapConvexTestDateReturns = < Test extends object > ( test : Test ) : Test => {
89+ const runnable = test as Test & {
90+ run : < Output > ( fn : ( ctx : unknown ) => Promise < Output > ) => Promise < Output > ;
91+ withIdentity ?: ( identity : TestIdentity ) => object ;
92+ } ;
93+ const withIdentity = runnable . withIdentity ;
94+
95+ const wrapped = {
96+ ...runnable ,
97+ run : async < Output > ( fn : ( ctx : unknown ) => Promise < Output > ) =>
98+ runnable . run (
99+ async ( ctx ) => serializeDatesForConvexTest ( await fn ( ctx ) ) as Output
100+ ) ,
101+ } ;
102+
103+ if ( ! withIdentity ) {
104+ return wrapped as Test ;
105+ }
106+
107+ return {
108+ ...wrapped ,
109+ withIdentity : ( identity : TestIdentity ) =>
110+ wrapConvexTestDateReturns ( withIdentity ( identity ) ) ,
111+ } as Test ;
112+ } ;
113+
28114export function convexTest < Schema extends SchemaDefinition < any , any > > (
29115 schema : Schema
30116) {
31- return baseConvexTest ( schema , convexModules ) ;
117+ return wrapConvexTestDateReturns ( baseConvexTest ( schema , convexModules ) ) ;
32118}
33119
34120export const withOrm = <
0 commit comments