【发布时间】:2022-01-08 17:48:04
【问题描述】:
我正在尝试模拟 bcrypt 哈希方法实现,但出现以下错误:
Error: thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
我尝试将超时时间增加到 30000。我还尝试模拟整个 bcrypt 模块,例如 jest.mock('bcrypt')。 我是测试新手,可能存在一些逻辑错误或不良做法。如果您指出它们,我将不胜感激。
我的代码:
import { UserService } from '../user.service';
import { Test, TestingModule } from '@nestjs/testing';
import { Repository } from 'typeorm';
import { getRepositoryToken } from '@nestjs/typeorm';
import * as bcrypt from 'bcrypt';
import { UserEntity } from '../user.entity';
import { UserRepository } from '../user.repository';
import { CreateUserDto } from '../dto/create-user.dto';
import { userStub } from './stubs/user.stub';
describe('UserService', () => {
let userService: UserService;
let userRepository: Repository<UserEntity>;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UserService,
{
provide: getRepositoryToken(UserRepository),
useClass: Repository,
},
],
}).compile();
userService = module.get<UserService>(UserService);
userRepository = module.get<Repository<UserEntity>>(
getRepositoryToken(UserRepository),
);
});
it('should define UserService', () => {
expect(userService).toBeDefined();
});
it('should define userRepository', () => {
expect(userRepository).toBeDefined();
});
describe('createUser method', () => {
it('has called with valid data', async () => {
const createUserDto: CreateUserDto = {
email: userStub().email,
firstName: userStub().firstName,
lastName: userStub().lastName,
password: userStub().password,
};
const user: UserEntity = userStub();
const spiedBcryptHashMethod = jest
.spyOn(bcrypt, 'hash')
.mockImplementation(() => Promise.resolve(''));
const spiedRepositoryCreateMethod = jest
.spyOn(userRepository, 'create')
.mockReturnValue(user);
const spiedRepositorySaveMethod = jest
.spyOn(userRepository, 'save')
.mockResolvedValue(user);
const createUserResult = await userService.createUser(createUserDto);
expect(spiedBcryptHashMethod).toHaveBeenCalled();
expect(spiedRepositoryCreateMethod).toHaveBeenCalled();
expect(spiedRepositorySaveMethod).toHaveBeenCalledWith(user);
expect(createUserResult).toEqual(user);
});
});
});
这里出现错误:
const spiedBcryptHashMethod = jest
.spyOn(bcrypt, 'hash')
.mockImplementation(() => Promise.resolve(''));
我的用户服务代码:
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import * as bcrypt from 'bcrypt';
import { UserRepository } from './user.repository';
import { CreateUserDto } from './dto/create-user.dto';
import { UserEntity } from './user.entity';
@Injectable()
export class UserService {
constructor(
@InjectRepository(UserRepository) private userRepository: UserRepository,
) {}
public async createUser(createUserDto: CreateUserDto): Promise<UserEntity> {
return await this.userRepository.save(
this.userRepository.create({
...createUserDto,
password: await new Promise((resolve, reject) => {
bcrypt.hash(createUserDto.password, 10, (err, encrypted) => {
if (err) {
reject(err);
}
resolve(encrypted);
});
}).then((onFilled: string) => onFilled),
}),
);
}
}
【问题讨论】:
-
您的服务方法也可以添加吗?
-
@JayMcDoniel 你是什么意思?我在我的用户服务中使用 bycrypt.hash()。也许我不明白你。你能澄清一下吗?
-
嗯,你的模拟实现看起来不错,所以我问你是否可以展示你正在尝试测试的整个服务方法。也许其他东西卡住了,但我不明白为什么 bcrypt 模拟会是
-
@JayMcDoniel 我已经添加了我的 userService 代码。请看。
标签: javascript node.js unit-testing jestjs nestjs