JEST throws .finally is not a function

Importing @babel/polyfill before the test also solve this problem

import '@babel/polyfill';

// Your tests...

Upgrading my node version from

v8.10.0

to

v10.19.0

Resolved this error.

Looking at the Browser Compatibility chart on MDN it looks like .finally() is not supported in Node until 10.0.0


I figured it out folks! Here's what was to be done:

analytics.submitPayload = jest.fn().mockImplementation(() => {
  return {
    finally: () => {
      return true;
    }
  };
});

I don't know if this is right or wrong, but it works. Please let me know if there's a better way to do it.


import '@babel/polyfill'; worked for me, but is deprecated since babel 7.4.

Instead, import this works fine as well:

import "core-js/stable";
import "regenerator-runtime/runtime";

eventually, I just updated node version (from 8.10.2 to 12.16.1)