Skip to content

Promise.withResolvers() implementation / mocks #5512

Answered by hi-ogawa
TheBlindHawk asked this question in Q&A
Discussion options

You must be logged in to vote

The feature like Promise.withResolvers is simply a runtime feature and it's not something "Vitest implements". As seen at the bottom of the same MDN page, it looks like Node simply doesn't have it yet.

I'm not sure whether "mocking" is relevant for your use case, but I think typically people would simply monkey-patch (i.e. polyfill) non-existing global. Have you tried something like this?

Promise.withResolvers = () => {
  let resolve, reject
  const promise = new Promise((res, rej) => { resolve = res, reject = rej })
  return { promise, resolve, reject }
}

If you want to "spy" its function calls, then you could probably do:

import { vi } from "vitest"
Promise.withResolvers = vi.fn(() => {
  

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@TheBlindHawk
Comment options

@anton-x-t
Comment options

Answer selected by TheBlindHawk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants