Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fail expectations that throw non-Errors #843

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AaronMoat
Copy link

@AaronMoat AaronMoat commented Jul 20, 2024

I hit a very strange error in one of my codebases (private, unfortunately, else I'd link to it). Here's a pretend example:

.expect((res) => {
  Buffer.from(undefined);
});

In normal environments, Buffer.from(undefined) throws a TypeError, which then correctly passes all instanceof Error checks. For reasons which I haven't fully diagnosed, but appear related to something to do with my setup (Jest? JSDom? Unsure.) and this warning, the below actually logs false:

.expect((res) => {
  try {
    Buffer.from(undefined);
  } catch (error) {
    console.log(error instanceof Error);
  }
});

Now, this is a silly example, but in my test I was trying to convert a buffer to a string and assert its string result. Something like:

.expect((res) =>
  expect(Buffer.from(res.body).toString('utf-8')).toBe(
    'some value',
  ),
);

For my example, what I actually wanted was res.body.toString (the Buffer.from was unnecessary), but it was throwing, and silently failing because of the above-described problem.

I tried a couple things, like trying to see if something "looks like" an error, but the simplest thing I could land on was to just simply covert non-errors to errors, and then the rest of the codebase is happy. Incidentally, and I would argue this is a good thing, it also now fails if you do throw 123, which while not a great throw, should probably fail, and previously did not.

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant