As a dedicated practitioner of TDD, I am currently working on implementing an Exception in my code.
Below is the test code I have written:
it.each([[{ id: '', token: '', skills: [''] }, 'Unknown resource']])(
'should return an Exception when incorrect dto data',
async (addSkillsDto: AddSkillsDto) => {
await expect(() => {
controller.addSkills(addSkillsDto)
}).rejects.toThrow()
}
)
Next, here is the relevant code snippet:
@Post('candidate/add-skills')
async addSkills(
@Body() skills: AddSkillsDto,
): Promise<StandardResponseObject<[]>> {
const data = await this.candidateService.addSkills(skills)
console.log(data, !data)
if (!data) throw new HttpException('Unknown resource', HttpStatus.NOT_FOUND)
else
return {
success: true,
data,
meta: null,
message: ResponseMessage.SKILLS_ADDED,
}
}
When running Jest, the console output shows an error:
● Candidate Controller › should return an Exception when incorrect dto data
expect(received).rejects.toThrow()
Matcher error: received value must be a promise or a function returning a promise
Received has type: function
Received has value: [Function anonymous]
88 | await expect(() => {
89 | controller.addSkills(addSkillsDto)
> 90 | }).rejects.toThrow()
| ^
91 | }
92 | )
93 |
at Object.toThrow (../node_modules/expect/build/index.js:226:11)
at candidate/candidate.controller.spec.ts:90:18
console.log
null true
at CandidateController.addSkills (candidate/candidate.controller.ts:75:13)
Test Suites: 1 failed, 1 total
At this point, I am unsure of the necessary steps to make the test pass successfully.