Skip to content

Enhance Chai assertions #8819

@VictorCazanave

Description

@VictorCazanave

Clear and concise description of the problem

Following this discussion, I think it would be good to provide Chai-like assertions for all Jest-like assertions.

Especially in the case of spies/mocks, the assertions could be similar to sinon-chai or consistent with the existing Jest assertions.

For examples:

  • toHaveBeenCalled() -> to.have.been.called or to.have.been.called()
  • toHaveBeenCalledTimes(n) -> to.have.callCount(n) or to.have.been.calledTimes(n)
  • toHaveBeenCalledWith(args) -> to.have.been.calledWith(args)
  • ...

Suggested solution

Assuming the new assertions should be consistent with the existing ones, the changes could be just adding new assertion names in jest-expect.ts. For example:

def(['toHaveBeenCalled', 'toBeCalled', 'called'], function () { ...}

However, the file might have to be renamed to something like jest-chai-expect.ts, because it's not only about Jest anymore.

Alternative

If we don't want to mix the the Jest and Chai assertions in the same file, another solution could be moving the assertion functions to a new shared module, then defining the Jest assertions in jest-expect.ts and the Chai assertions in chai-expect.ts.

Additional context

I'd be glad to create a PR, once this feature request is accepted and an implementation is recommended.

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions