Mugetsu
Mugetsu2y ago

Vitest context router caller

Hi, Im trying to setup vitest to test trpc. I would like to have a trpc approuter caller to be accessible from vitest context but im struggling with typings of appRouter.createCaller(ctx) return type
//vitest.setup.ts

// eslint-disable-next-line import/no-extraneous-dependencies
import { afterEach, beforeEach, vi } from 'vitest'
import { createInnerTRPCContext } from './src/server/api/trpc'
import { appRouter } from './src/server/api/root'

const contextCaller = async (schemaFilters) => {
const options = {
req: {
authorisation: { authorised: true },
logger: {
info: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
error: vi.fn(),
},
},
res: vi.fn(),
currentSchema: schemaFilters ? { filters: { ...schemaFilters } } : {},
}

const ctx = await createInnerTRPCContext(options)
return appRouter.createCaller(ctx)
}

beforeEach((context) => {
vi.mock('./src/server/tlsHttpClient', () => ({
httpClient: {
get: vi.fn().mockReturnValue({ execute: vi.fn() }),
post: vi.fn(),
delete: vi.fn(),
put: vi.fn(),
},
}))

context.trpcContextCaller = contextCaller
})

afterEach(() => {
vi.clearAllMocks()
})
//vitest.setup.ts

// eslint-disable-next-line import/no-extraneous-dependencies
import { afterEach, beforeEach, vi } from 'vitest'
import { createInnerTRPCContext } from './src/server/api/trpc'
import { appRouter } from './src/server/api/root'

const contextCaller = async (schemaFilters) => {
const options = {
req: {
authorisation: { authorised: true },
logger: {
info: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
error: vi.fn(),
},
},
res: vi.fn(),
currentSchema: schemaFilters ? { filters: { ...schemaFilters } } : {},
}

const ctx = await createInnerTRPCContext(options)
return appRouter.createCaller(ctx)
}

beforeEach((context) => {
vi.mock('./src/server/tlsHttpClient', () => ({
httpClient: {
get: vi.fn().mockReturnValue({ execute: vi.fn() }),
post: vi.fn(),
delete: vi.fn(),
put: vi.fn(),
},
}))

context.trpcContextCaller = contextCaller
})

afterEach(() => {
vi.clearAllMocks()
})
How I could type the contextCaller function? It currently inffers Promise<any>
0 Replies
No replies yetBe the first to reply to this messageJoin