Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: Promise resolver is not a function

I'm trying to run this jestJS unit-test, but I do get TypeError: Promise resolver undefined is not a function. What am I doing wrong?

it('_onSubmit() should throw error if data is missing', (done) => {
  const createUserMutation = () => new Promise()
  const wrapper = shallow(<CreateAccount createUserMutation={createUserMutation} />)
  wrapper.update().find(Form).simulate('submit', {
    preventDefault: () => {}
  })
  createUserMutation.resolve().then(() => {
    expect(console.error).toHaveBeenCalled()
    done()
  })
})
like image 999
user3142695 Avatar asked Dec 14 '22 19:12

user3142695


1 Answers

new Promise() is trying to create a promise without an executor function*. The correct usage is to pass in a function that accepts up to two parameters (resolve and reject), e.g.:

var p = new Promise((resolve, reject) => {
    // ...code that does something, ultimately calls either resolve or reject
});

More on MDN:

  • The Promise constructor
  • Using Promises

Later, you appear to be trying to call resolve on the promise instance. You can't do that either (unless you're not using standard promises). You resolve a promise using the resolve function passed into the executor. (I tried to modify your code for you, but too much context info is required.)


* I'm surprised to see V8's error message say "resolver function" but I've verified it does. The spec calls it an executor, and I would have thought a "resolver functioN" would be the function the Promise constructor passes as the first argument to it...

like image 132
T.J. Crowder Avatar answered Dec 28 '22 18:12

T.J. Crowder