Skip to content

Inconsistency in toHaveBeenCalledExactlyOnceWith for undefined values #753

@DesselBane

Description

@DesselBane

The toHaveBeenCalledExactlyOnceWith differentiates between a call where an argument with the value of undefined is passed and a call where nothing is passed. Technically this is a difference in JS but I would argue not a meaningfull one.

  it('should not make a difference', () => {
    const foo = {
      bar: (needed: string, maybe?: string) => {},
    }

    const barSpy = vi.spyOn(foo, 'bar')

    foo.bar('something')
    
    // Works
    expect(barSpy).toHaveBeenCalledExactlyOnceWith('something')

    barSpy.mockReset()

    const someArgument = undefined

    foo.bar('something', someArgument)

    // Works
    expect(barSpy).toHaveBeenCalledExactlyOnceWith('something', undefined)

   // Fails
    expect(barSpy).toHaveBeenCalledExactlyOnceWith('something')
  })

I would expect the matcher to pass in all 3 scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions