chai icon indicating copy to clipboard operation
chai copied to clipboard

Consider a new, informative page in Chai's documentation for working with Errors

Open jojenki opened this issue 4 years ago • 2 comments

Simple working example:

class UntypedError {
  private readonly message;
  constructor(message: string) {
    this.message = message;
  }
}

class TypedError extends Error {
  constructor(message: string) {
    super(message);
  }
}

describe("Error Experimentation", () => {
  it("should pass in all of these cases", () => {
    // Pass
    expect(new UntypedError("untyped error no array")).to.deep.equal(new UntypedError("untyped error no array"));
    // Fail - This is the unexpected part, to me.
    expect(new TypedError("typed error no array")).to.deep.equal(new TypedError("typed error no array"));

    // This is closer to what I'm actually trying to test but is fundamentally the same thing.
    // Pass
    expect([new UntypedError("untyped error array")]).to.deep.include(new UntypedError("untyped error array"));
    // Fail - The fact that it's part of an array is probably irrelevant, but I cannot be certain.
    expect([new TypedError("typed error array")]).to.deep.include(new TypedError("typed error array"));
  });
});

I've been working on this for a couple of days now and have seen some resources / hints regarding Chai basically making the decision that two Errors with different call-stacks are evaluated as different, even if they are the same type and have the same message. So, I think that's what's going on here.

My ask is that the team create some dedicated documentation that explains this (so I can be certain that that's what is actually going on here) and propose some alternatives or provide a rationale on why this type of testing shouldn't work / shouldn't be done.

And, if such documentation does already exist, I apologize for wasting your time, and my feedback would be that, that documentation is hard to find. :)

jojenki avatar Jun 25 '21 21:06 jojenki

Hey @jojenki; Error equality is something that seems to be not implemented in chai right now due to some issues that are listed in https://github.com/chaijs/chai/issues/1065. Depending on your situation, you might want to assert on the error properties such as name and message rather than asserting the equality of the error objects themselves.

For the question of documentation, I'm not quite sure what would be the best thing to do. Where did you look for information on this? Maybe we can make a PR to the documentation that you were looking at to add this piece of information.

koddsson avatar Oct 04 '21 21:10 koddsson