我正尝试用开玩笑的方法来测试tryCatch代码,但仍会不断出错,目前这是我所尝试的
student.js
async checkStudentExist(studentName) {
try {
const studentExist = await this.StudentRepository.getOne({name: studentName})
if (studentExist) {
throw new Error(`Student already exist with the name: ${studentName}`)
}
return studentExist
} catch (error) {
throw error
}
}
student.test.js
it('should throw an error if the input name already exist in database', async () => {
mockGetOne.mockReturnValue(Promise.resolve(expectedOrganization))
await studentService.checkStudentExist('john doe')
expect(mockGetOne).toHaveBeenCalledWith({name: 'sample org'})
expect(studentService.checkStudentExist('john doe')).resolves.toThrowError(new Error(`Organization already exist with the name: john doe`))
})
这是我遇到的错误
FAIL test/specs/services/student_service.test.js
● Console
console.log src/services/student_service.js:53
111
console.log src/services/student_service.js:53
111
● Check if Student exist › should throw an error if the input name already exist in database
Student already exist with the name: john doe
55 | const studentExist = await this.StudentRepository.getOne({name: studentName})
56 | if (studentExist) {
> 57 | throw new Error(`Student already exist with the name: ${studentName}`)
| ^
58 | }
59 | return studentExist
60 | } catch (error) {
at StudentService.checkStudentExist (src/services/student_service.js:57:23)