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

Add support for asserting that a promise has resolved or rejected (regardless of value) #5248

Closed
Guardiannw opened this issue Jan 8, 2018 · 10 comments

Comments

@Guardiannw
Copy link

Do you want to request a feature or report a bug?
Feature Request

What is the current behavior?
Currently, there is no declarative and easy way to check if a promise resolves or rejects.

What is the expected behavior?
I would like a way to be able to check if a promise resolves or rejects, regardless of the value that it produces, or if it even produces a value, it is irrelevant. In particular, i want to easily be able to easily check to make sure that a promise was not rejected. I don't care at all if it actually sent an error, undefined, or whatever, I just want to make sure it was not rejected. I would like a matcher like expect(promise).toResolve() or expect(promise).not.toReject(). Right now, as far as I can tell, there is no simple way to go about asserting whether or not a promise has resolved or rejected without taking into account its value. I want to be able to assert this without regard to its response, the error message, or otherwise.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

Jest version 22.0.4

@Guardiannw Guardiannw changed the title Add support for asserting that a promise has resolved or rejected (irregardless of value) Add support for asserting that a promise has resolved or rejected (regardless of value) Jan 8, 2018
@thymikee
Copy link
Collaborator

thymikee commented Jan 8, 2018

@SimenB
Copy link
Member

SimenB commented Jan 8, 2018

Does resolves.toBe(expect.anything()) work? Still a bit verbose either way, I guess

@Guardiannw
Copy link
Author

No, it doesn't work. expect.anything() doesn't cover the situation when it is null or undefined. Also, the .resolves does not work because it requires a matcher on the end of it. There doesn't seem to be a simple way just to check to make sure it was resolved or rejected.

@Guardiannw
Copy link
Author

Hey @SimenB, is there any update on this?

Thanks!

@SimenB
Copy link
Member

SimenB commented May 23, 2018

We've landed support for asynchronous matchers, so you can add a toResolve or toReject yourself if you want 🙂 See #5919 (available in jest@beta)

@mattphillips something for jest-extended?

Closing as I think it can be solved in user land. Happy to reopen if proven wrong

@SimenB SimenB closed this as completed May 23, 2018
@mattphillips
Copy link
Contributor

@Guardiannw I like this! Mind opening this up in jest-extended and I’ll help get this working in user land when Jest@23 lands? 😄

@Guardiannw
Copy link
Author

@mattphillips Will do! Thanks!

@slifty
Copy link

slifty commented Apr 30, 2020

Just a note that I would find this useful!

My use case is that I am testing an abstract class and I want to be able to test that async methods that have been overridden will properly resolve in the context of the parent objects that call them.

@ysfaran
Copy link

ysfaran commented Dec 21, 2020

For anyone who wants a simple solution for asserting promise resolution or rejection:

extendExpect.js

async function toReject(promise) {
  let rejected
  try {
    await promise
    rejected = false
  } catch {
    rejected = true
  }

  return {
    pass: rejected,
    message: () => `expected promise to ${rejected ? "resolve" : "reject"}`,
  }
}

async function toResolve(promise) {
  let resolved
  try {
    await promise
    resolved = true
  } catch {
    resolved = false
  }

  return {
    pass: resolved,
    message: () => `expected promise to ${resolved ? "reject" : "resolve"}`,
  }
}

expect.extend({
  toReject,
  toResolve,
})

Add setup file to your jest config:

jest.config.js

module.exports = {
  // ...
  setupFilesAfterEnv: ["<rootDir>/setupTests.js"],
}

Import extendsExpect file in your setup file:

setupTests.js

import "./extendExpect"

Usage:

expect(promise).toResolve()
expect(promise).toReject()

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants