最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JEST throws .finally is not a function - Stack Overflow

programmeradmin2浏览0评论

Here is my react component:

import { sendAnalytics } from 'analytics';

class MyComponent extends React.Component {
    myMethod() {
        console.log('do something!');
    }

    render() {
        return (
            <Button 
                onClick={submitAnalytics({name: 'foo'}).finally(this.myMethod())} 
                dataset={{"data-id": "button"}}
            > Send Analytics
            </Button>
            }
        )
    }
}

And my test is like so:

import * as analytics from 'analytics';
jest.mock('analytics');

describe('Analytics', () => {
    it('Should call analytics when button is clicked', () => {
        analytics.submitAnalytics.mockResolvedValue(Promise.resolve(true));
        const tree = ReactTestRenderer.create(<MyComponent />);

        // Actual implementation of following 3 lines is slightly different.
        const button = tree.root.findByProps({"data-id": "button"});
        button.props.onClick();
        expect(analytics.submitAnalytics).toHaveBeenCalled();
    });
});

I tried several different mocking strategies like:

analytics.submitAnalytics.mockImplementation(() => {
    return Promise.resolve(true)
});

Nothing seems to work out. I keep getting the following error:

TypeError: (0 , analytics.submitAnalytics)(...).finally is not a function.

I don't know why. Any help appreciated. Please let me know if you need any more contextual code.

Here is my react component:

import { sendAnalytics } from 'analytics';

class MyComponent extends React.Component {
    myMethod() {
        console.log('do something!');
    }

    render() {
        return (
            <Button 
                onClick={submitAnalytics({name: 'foo'}).finally(this.myMethod())} 
                dataset={{"data-id": "button"}}
            > Send Analytics
            </Button>
            }
        )
    }
}

And my test is like so:

import * as analytics from 'analytics';
jest.mock('analytics');

describe('Analytics', () => {
    it('Should call analytics when button is clicked', () => {
        analytics.submitAnalytics.mockResolvedValue(Promise.resolve(true));
        const tree = ReactTestRenderer.create(<MyComponent />);

        // Actual implementation of following 3 lines is slightly different.
        const button = tree.root.findByProps({"data-id": "button"});
        button.props.onClick();
        expect(analytics.submitAnalytics).toHaveBeenCalled();
    });
});

I tried several different mocking strategies like:

analytics.submitAnalytics.mockImplementation(() => {
    return Promise.resolve(true)
});

Nothing seems to work out. I keep getting the following error:

TypeError: (0 , analytics.submitAnalytics)(...).finally is not a function.

I don't know why. Any help appreciated. Please let me know if you need any more contextual code.

Share Improve this question edited Jan 18, 2019 at 9:13 skyboyer 23.7k7 gold badges61 silver badges71 bronze badges asked Jan 17, 2019 at 7:37 darth-coderdarth-coder 7628 silver badges20 bronze badges 2
  • Have you tried returning a new Promise instead of returning the result of the resolve method? – Arcturus Commented Jan 17, 2019 at 8:15
  • Yes I did that. – darth-coder Commented Jan 17, 2019 at 8:37
Add a comment  | 

5 Answers 5

Reset to default 7

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)

I faced same challenge. Reason- Jest runs in Node enviroment,and "Finally" is supported from Node version- 10.0.0. But my Jenkins versions was 8.x so As a quick fix you can remove finally from code. Later we can upgrade local and remote server version.

Below is MDN link- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally

发布评论

评论列表(0)

  1. 暂无评论