winsoroaks
winsoroaks•15mo ago

is there a test implementation of `GenericMutationCtx`?

hi team! i wonder if it's possible to get a test version of GenericMutationCtx that i can override in the test files? i've been doing @ts-ignore for the ctx in myFunc and it's been tripping me all over 😂
5 Replies
Michal Srb
Michal Srb•15mo ago
Sooner than later we will provide a way to test queries and mutations. Stay tuned!
winsoroaks
winsoroaksOP•15mo ago
thanks michal. in the meantime, do u know how can i make my test more robust? i want to do a match on the arg im passing in
it("validateUserNotFound should throw if user exists", async () => {
const ctx = {
db: {
query: vi.fn().mockImplementation((db) => {
if (db === "user") {
return {
filter: vi.fn().mockReturnValue({
unique: vi.fn().mockReturnValue({
_id: "user id",
}),
}),
}
}
}),
},
auth: mockAuth(),
}

await expect(
validateUserNotFound(ctx, { email: "hello@world.com" })
).rejects.toThrow("User already exists")
})

// my actual function
const user = await ctx.db
.query(USER_DB)
.filter((q) => q.eq(q.field("email"), args.email))
.unique()
if (user) {
throw new Error("User already exists")
}
it("validateUserNotFound should throw if user exists", async () => {
const ctx = {
db: {
query: vi.fn().mockImplementation((db) => {
if (db === "user") {
return {
filter: vi.fn().mockReturnValue({
unique: vi.fn().mockReturnValue({
_id: "user id",
}),
}),
}
}
}),
},
auth: mockAuth(),
}

await expect(
validateUserNotFound(ctx, { email: "hello@world.com" })
).rejects.toThrow("User already exists")
})

// my actual function
const user = await ctx.db
.query(USER_DB)
.filter((q) => q.eq(q.field("email"), args.email))
.unique()
if (user) {
throw new Error("User already exists")
}
is it possible to do some sort of filter: vi.fn().IF_EMAIL_MATCHES_hello@world.com.mockReturnValue()?
Michal Srb
Michal Srb•15mo ago
You can do mockImplementation instead of mockReturnValue
winsoroaks
winsoroaksOP•15mo ago
thanks. ok i think im close,
filter: vi.fn().mockImplementation((func) => {
if (func({ _id: "user id" })) {
return {
q: vi.fn().mockImplementation(() => ({
field: vi.fn().mockImplementation(() => ({
unique: vi.fn().mockReturnValue({
_id: "user id",
}),
})),
})),
}
} else {
})),
})),
}
filter: vi.fn().mockImplementation((func) => {
if (func({ _id: "user id" })) {
return {
q: vi.fn().mockImplementation(() => ({
field: vi.fn().mockImplementation(() => ({
unique: vi.fn().mockReturnValue({
_id: "user id",
}),
})),
})),
}
} else {
})),
})),
}
but still getting TypeError: q.field is not a function im not sure if im doing the lambda func wrongly...? do u have any idea?
Michal Srb
Michal Srb•15mo ago
I think this is super cumbersome. Could you pull out the whole DB read into a function and mock that instead?

Did you find this page helpful?