Nest JS Jest 测试 .pre/.use 不是函数

时间:2021-05-25 08:47:31

标签: javascript mongoose jestjs passport.js nestjs

describe('Auth Service', () => {
  let authService: AuthService;

  beforeEach(async () => {
    const mod: TestingModule = await Test.createTestingModule({
      imports: [
        forwardRef(() => TenantModule),
        forwardRef(() => UserModule),
        AuthModule,
      ],
      providers: [AuthService],
    }).compile();
    authService = mod.get<AuthService>(AuthService);
  });

  describe('when creating a cookie', () => {
    it('should return a string', () => {
      const userId = '1';
      expect(typeof authService.getCookieWithJwtToken(userId)).toEqual(
        'string',
      );
    });
  });
});

我的 nest js 应用程序中有一个非常基本的测试设置,其中包含 jest,我在运行测试时遇到了一些类似的问题。首先,我发现 .pre 不是我的用户模型的函数。

UserSchema.pre('save', async function (next) {
  ...code
});

当我评论这个时,我得到 TypeError: passportInstance.use is not a function

我见过一些例子,人们在模拟护照时遇到问题,他们在测试中更改了护照,但在我的例子中,护照是导入的 authModdule 的一部分,我找不到任何有关如何处理的文档这个。

import { PassportStrategy } from '@nestjs/passport';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {

我有一个倾向,我需要模拟模型和护照,但我不知道从哪里开始,请帮忙。

0 个答案:

没有答案