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

feat(testing/asserts): return error from assertRejects and assertThrows #2226

Merged
merged 6 commits into from
May 25, 2022

Conversation

mrkldshv
Copy link
Contributor

@mrkldshv mrkldshv commented May 13, 2022

Makes assertRejects and assertThrows return caught error.

  • Deprecate error callback from assertRejects and assertThrows signatures
  • Allow throwing non-error values

Closes #1362, #2220.

@CLAassistant
Copy link

CLAassistant commented May 13, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@KyleJune KyleJune left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not on the core team, just another contributor. Giving my feedback because I worked in this area before and think the change in call signature issue is a good opportunity to also fix another issue related to assertThrows and assertRejects.

testing/asserts.ts Outdated Show resolved Hide resolved
@@ -22,7 +22,7 @@ function slidingWindowsThrowsTest<T>(
size: number,
config?: { step?: number; partial?: boolean },
],
ErrorClass?: ErrorConstructor | undefined,
ErrorClass: ErrorConstructor,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to required, because none of the signatures of assertThrows function matches the way this function is called. I'm open to refactoring it to keep it optional though. It can be something like:

if (ErrorClass) {
    assertThrows(
    () => {
      slidingWindows(...input);
    },
    ErrorClass,
    msgIncludes,
    msg,
  );
} else {
  assertThrows(
    () => {
      slidingWindows(...input);
    },
    msg,
  );
}

testing/asserts.ts Show resolved Hide resolved
@@ -641,66 +641,86 @@ export function assertIsError<E extends Error = Error>(
* callback which will be passed the error, usually to apply some custom
* assertions on it.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking to remove this bit about error callback from the documentation not to encourage developers to use it, because we're deprecating this parameter. What do you think?

testing/asserts.ts Outdated Show resolved Hide resolved
testing/asserts.ts Outdated Show resolved Hide resolved
testing/asserts_test.ts Outdated Show resolved Hide resolved
Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrkldshv LGTM. Nice work!

@kt3k
Copy link
Member

kt3k commented May 25, 2022

Note: This is going to be a breaking change in 2 points.

  • Now errorCallback signature is deprecated. The usages are need to be migrated to the pattern of returning error and asserting on it. This doesn't break code immediately, but the users see the deprecation warnings in IDE.
  • Now ErrorClass parameter is non optional. This can break the type checking if a user uses undefined for it. The users need to pass something there

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.

std assertThrows is acting like assertThrowsError
4 participants