-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathtestingFunctions.ts
More file actions
61 lines (57 loc) · 1.87 KB
/
Copy pathtestingFunctions.ts
File metadata and controls
61 lines (57 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
customAction,
customMutation,
customQuery,
} from "convex-helpers/server/customFunctions";
import { action, mutation, query } from "./_generated/server";
import schema from "./schema";
// Wrappers to use for function that should only be called from tests
export const testingQuery = customQuery(query, {
args: {},
input: async (_ctx, _args) => {
if (process.env.IS_TEST === undefined) {
throw new Error(
"Calling a test only function in an unexpected environment",
);
}
return { ctx: {}, args: {} };
},
});
export const testingMutation = customMutation(mutation, {
args: {},
input: async (_ctx, _args, { devOnly }: { devOnly: boolean }) => {
if (process.env.IS_TEST === undefined) {
throw new Error(
"Calling a test only function in an unexpected environment",
);
}
if (devOnly && process.env.IS_PROD) {
throw new Error("This function is only available in development");
}
return { ctx: {}, args: {} };
},
});
export const testingAction = customAction(action, {
args: {},
input: async (_ctx, _args) => {
if (process.env.IS_TEST === undefined) {
throw new Error(
"Calling a test only function in an unexpected environment",
);
}
return { ctx: {}, args: {} };
},
});
export const clearAll = testingMutation({
devOnly: true,
handler: async ({ db, scheduler, storage }) => {
for (const table of Object.keys(schema.tables)) {
const docs = await db.query(table as any).collect();
await Promise.all(docs.map((doc) => db.delete(table as any, doc._id)));
}
const scheduled = await db.system.query("_scheduled_functions").collect();
await Promise.all(scheduled.map((s) => scheduler.cancel(s._id)));
const storedFiles = await db.system.query("_storage").collect();
await Promise.all(storedFiles.map((s) => storage.delete(s._id)));
},
});