nest-trpc-prisma-starter/apps/server/test/helper/create-service-unit.ts

35 lines
925 B
TypeScript
Raw Permalink Normal View History

2024-04-25 18:12:44 +00:00
import { ModuleMetadata } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { Test, TestingModule } from '@nestjs/testing'
import * as config from '@server/config'
type ClassType<T> = new (...args: any[]) => T
export function createServiceUnitTestApp<T>(Service: ClassType<T>, module?: ModuleMetadata) {
const proxy = {} as {
service: T
app: TestingModule
}
beforeAll(async () => {
const { imports, providers } = module || {}
const app = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: [`.env.${process.env.NODE_ENV}`, '.env'],
load: [...Object.values(config)],
}),
...(imports || []),
],
providers: [Service, ...(providers || [])],
}).compile()
await app.init()
proxy.service = app.get<T>(Service)
proxy.app = app
})
return proxy
}