我找遍了所有的地方,但是我找不到任何关于如何用nestjs装饰器创建测试的文档,我在网站上也找不到太多。
能帮我一下吗?
这是我的室内设计师
import { createParamDecorator, ExecutionContext } from '@nestjs/common'
import { GqlExecutionContext } from '@nestjs/graphql'
export type AuthUser = {
id: string
}
export const CurrentUser = createParamDecorator((data: unknown, context: ExecutionContext): AuthUser => {
const ctx = GqlExecutionContext.create(context)
const req = ctx.getContext().req
return req.currentUser
})
字符串
jest typescript nestjs
1条答案
按热度按时间7xllpg7q1#
这些问题都很相似,像GraphQL,TypeOrm和class-validator这样的装饰器,..但它们都有一个相同的点,它在装饰器内部发送一个回调函数。因为所有这些都是函数,你可以做的就是创建一个文件,其中包含所有你将要使用的函数,然后你可以开始为它们编写测试。这比尝试测试深层库方法更容易。
在这种情况下,你可以试试这个。
auth.decorator.ts
字符串
auth.decorator.test.ts
型
另外,在这里也有一些相同主题的讨论:How to cover TypeORM @Column decorator with Jest unit testing?