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

javascript - How to mock Promise resolve function in Jest - Stack Overflow

programmeradmin0浏览0评论

I'm using Jest + Enzyme. I have a function,

     submitHandler = values => {
         return new Promise((resolve, _) => {
                saveSomething({values, resolve});
         }
     }

My test:

    it('Should call saveSomething on form submit', () => {
        const values = {firstName: 'FName', lastname: 'LName'};
        const {enzymeWrapper, props} = setup();
        enzymeWrapper.find('Formik').simulate('submit', values);
        expect(props.saveSomething).toBeCalledWith({
            values: {
                ...values,
                contactLanguage: LOCALE_TO_LANGUAGE_MAP[props.locale],
            },
        });
    });

Currently, my test is failed. Error:

    Error: expect(jest.fn()).toBeCalledWith(...expected)

    - Expected
    + Received

    @@ -1,6 +1,7 @@
      Object {
    +   "resolve": [Function anonymous],
        "values": Object {
          "contactLanguage": "FRENCH",
          "firstName": "FName",
          "lastname": "LName",
        },,

Question: How to mock resolve function?

I'm using Jest + Enzyme. I have a function,

     submitHandler = values => {
         return new Promise((resolve, _) => {
                saveSomething({values, resolve});
         }
     }

My test:

    it('Should call saveSomething on form submit', () => {
        const values = {firstName: 'FName', lastname: 'LName'};
        const {enzymeWrapper, props} = setup();
        enzymeWrapper.find('Formik').simulate('submit', values);
        expect(props.saveSomething).toBeCalledWith({
            values: {
                ...values,
                contactLanguage: LOCALE_TO_LANGUAGE_MAP[props.locale],
            },
        });
    });

Currently, my test is failed. Error:

    Error: expect(jest.fn()).toBeCalledWith(...expected)

    - Expected
    + Received

    @@ -1,6 +1,7 @@
      Object {
    +   "resolve": [Function anonymous],
        "values": Object {
          "contactLanguage": "FRENCH",
          "firstName": "FName",
          "lastname": "LName",
        },,

Question: How to mock resolve function?

Share Improve this question edited Apr 2, 2020 at 11:09 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Apr 2, 2020 at 10:46 D.MarkD.Mark 6072 gold badges12 silver badges24 bronze badges 2
  • The code you gave is inplete – Lin Du Commented Apr 3, 2020 at 4:27
  • @slideshowp2 what do you mean? – D.Mark Commented Apr 6, 2020 at 4:59
Add a ment  | 

1 Answer 1

Reset to default 3

Jest mocks have an inherent method to cope with Promise resolutions and rejections. Please refer to the Jest Mock API for details.

The mockResolvedValue method is used for this very purpose.

Just mock your submitHandler with a simple call to jest.fn() and add the mockResolvedValue method.

You can test rejections as well with the mockRejectValue method.

发布评论

评论列表(0)

  1. 暂无评论