0

我是这个框架Nest JS的新手。当我尝试实施单元测试时,它会抛出节点邮件错误。我不知道如何在单元测试中配置邮件选项。所以,我谷歌它如何解决这个问题。我得到了答案,我尝试了那个答案,但它不起作用。许多实现单元测试的第三方模块会抛出错误,例如(JWT_module 选项,节点邮件程序)

代码

import { MailerService, MAILER_OPTIONS } from '@nestjs-modules/mailer';
import { ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { options } from 'joi';
import { MailService } from 'src/mail/mail.service';
import PublicFile from 'src/models/files/publicFile.entity';
import { LoginTypeEntity } from 'src/models/login-type/login-type.entity';
import { UserEntity } from 'src/models/user/user.entity';
import { UserLoginTypeService } from 'src/user-login-type/user-login-type.service';
import { FilesService } from 'src/user/files.service';
import { UserModule } from 'src/user/user.module';
import { UserService } from 'src/user/user.service';
import { Repository } from 'typeorm';
import { AuthService } from './auth.service';
import { jwtConstants } from './constants';
import { JwtStrategy } from './strategies/jwt.strategy';
import { LocalStrategy } from './strategies/local.strategy';

describe('SampleService', () => {
  let authService: AuthService;
  let userService: UserService

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [UserService, MailService, MailerService, UserLoginTypeService,FilesService,
        ConfigService,
        {
          name: MAILER_OPTIONS,
          provide: MAILER_OPTIONS,
          useValue: options,
        },
        {
          // how you provide the injection token in a test instance
          provide: getRepositoryToken(UserEntity),
          // as a class value, Repository needs no generics
          useClass: Repository,
        },
        {
          provide: getRepositoryToken(LoginTypeEntity),
          useValue: {}
        },
        {
          provide: getRepositoryToken(PublicFile),
          useValue: {}
        }],
    }).compile();

    userService = module.get<UserService>(UserService);
    authService = module.get<AuthService>(AuthService);
  });


  describe('validateUser', () => {
    it('should return User based on username and password', async () => {
      var usrpass = "harismith";
      let findByEmail_result:any = {
        "email":'harismith001@gmail.com',
        "password":usrpass
      }
      // jest.spyOn(userService, 'findbyemail').mockImplementation(() => findByEmail_result)
      const excepted_result = {
        "email":'harismith001@gmail.com',
      }
      expect(await authService.validateUser('harismith001@gmail.com', "harismith@001") ).toEqual(excepted_result);
    });
  });

});

面对这个错误

在此处输入图像描述

如何解决此问题

4

0 回答 0