你好,我试图用Jest测试我的Node App,但是当我运行测试时,即使它失败了,测试也会始终通过
开玩笑--collect-coverage / --watch
我期望测试失败。但是我得到了:
expect(Array.prototype.splice).toHaveBeenCalledTimes(41238912)
<-通过
expect(Array.prototype.splice).toHaveBeenCalledTimes(1)
<-通过(如推测的那样)
expect(Array.prototype.splice).toHaveBeenCalledTimes(1239)
<-通过
expect(e).toBeInstanceOf(IdInUseError)
<-通过(如可能的那样)
expect(e).toBeInstanceOf(euisamndasdiuoa)
<-通过
System:
OS: Windows 10 10.0.18363
CPU: (4) x64 AMD Ryzen 3 3200G with Radeon Vega Graphics
Binaries:
Node: 12.18.3 - C:\Program Files\nodejs\node.EXE
npm: 6.14.6 - C:\Program Files\nodejs\npm.CMD
npmPackages:
jest: ^26.6.0 => 26.6.1
repository.spec.js :-
test("save a team correctly", async() => {
repository.getAll = jest.fn().mockImplementationOnce(() => [
{name:"team", id: "3", crestUrl: "/fake_route/12345"}
])
Array.prototype.findIndex = jest.fn().mockImplementationOnce(() => "0")
Array.prototype.splice = jest.fn().mockImplementationOnce(() => [])
const editedTeam = {name:"team2", id: "3"}
await repository.saveEditedTeam(editedTeam)
expect(Array.prototype.splice).toHaveBeenCalledTimes(1)
repository.js :-
async saveEditedTeam(editedTeam){
const teamList = await this.getAll()
const teamIndex = teamList.findIndex( team => team.id === editedTeam.id )
if(teamIndex === -1 ){
throw new IdNotFound()
}
if(!editedTeam.crestUrl){
editedTeam.crestUrl = teamList[teamIndex].crestUrl
}
teamList.splice(teamIndex, 1, editedTeam)
this.writeDb(teamList)
return teamList
}