开玩笑的测试总是通过的

时间:2020-10-25 00:40:26

标签: node.js unit-testing jestjs automated-tests

?错误报告

你好,我试图用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) <-通过

envinfo

  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
}

0 个答案:

没有答案