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

javascript - Promise.allSettled is not a function in jest test, how to mock? - Stack Overflow

programmeradmin10浏览0评论

I have an action that uses Promise.allSettled to return multiple stat objects from an API. When I run the tests with mocking I get the error Promise.allSettled is not a function

I have a get endpoint to returns different types of stats. myapi/get_stats/:type

I have an action for this API as follows

  const types = ['seo', 'referrers', 'clicks', 'posts', 'videos'];
  const promises = [];
  types.forEach(type =>
    promises.push(
      api().stats.getStats(type),
    ),
  );
  Promise.allSettled(promises).then(res => {
    const { statData, statErrors } = mapData(res); // Data manipulation
    dispatch({ type: FETCH_STATS_RESOLVED, payload: { statData, statErrors } });
  });

My Test set up

jest.mock('api-file.js', () => ({
  api: () => ({
     stats: {
         getStats: type => {
           switch (type) {
              case: 'seo':
                return mockSeoStats;
           }
         }
      } 
  })
}));

in beforeEach()

mockSeoStats.mockImplementation(() => Promise.resolve({ value: {data: myData} }));

I can see that the Action is receiving these mocked values, but Promise.allSettled is plaining

I assume it's having a hard time with the jest mock structure

So, how can i mock Promise.allSettled to just return what I expect it to, instead of looking at my mocked functions?

I have an action that uses Promise.allSettled to return multiple stat objects from an API. When I run the tests with mocking I get the error Promise.allSettled is not a function

I have a get endpoint to returns different types of stats. myapi./get_stats/:type

I have an action for this API as follows

  const types = ['seo', 'referrers', 'clicks', 'posts', 'videos'];
  const promises = [];
  types.forEach(type =>
    promises.push(
      api().stats.getStats(type),
    ),
  );
  Promise.allSettled(promises).then(res => {
    const { statData, statErrors } = mapData(res); // Data manipulation
    dispatch({ type: FETCH_STATS_RESOLVED, payload: { statData, statErrors } });
  });

My Test set up

jest.mock('api-file.js', () => ({
  api: () => ({
     stats: {
         getStats: type => {
           switch (type) {
              case: 'seo':
                return mockSeoStats;
           }
         }
      } 
  })
}));

in beforeEach()

mockSeoStats.mockImplementation(() => Promise.resolve({ value: {data: myData} }));

I can see that the Action is receiving these mocked values, but Promise.allSettled is plaining

I assume it's having a hard time with the jest mock structure

So, how can i mock Promise.allSettled to just return what I expect it to, instead of looking at my mocked functions?

Share Improve this question edited Jun 2, 2020 at 21:37 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jun 2, 2020 at 20:23 lyssaloolyssaloo 311 silver badge2 bronze badges 5
  • To get an useful answer, try to boil down your question to the essential information and provide examples of what happens versus what you would like to happen. – Nikita Malyschkin Commented Jun 2, 2020 at 20:44
  • Does promise.all work? – jasper Commented Jun 2, 2020 at 21:03
  • 2 it has been added in Node 12.9(and jest is run under NodeJS). So you either should update your Node or provide polyfill for this method. – skyboyer Commented Jun 2, 2020 at 21:39
  • 1 Promise.allSettled works in implementation. It is only in testing with Jest that I have an issue. The code itself is functional and works as expected. – lyssaloo Commented Jun 3, 2020 at 14:22
  • I was using [email protected] and [email protected] with node v8.9.4 and the application was working but the tests for code using allSettled was failing. I just updated node to v12.16.1 keeping jest and ts-jest versions and the tests work. – ctowers Commented Jun 30, 2020 at 6:58
Add a ment  | 

2 Answers 2

Reset to default 5

A similar question was asked at Execute batch of promise with Promise.allSettled() Promise.allSettled is available from Node version 12.0 +

You can update node using Node's version manager

nvm ls
# Look for a 12.17.0 version (latest)
nvm install 12.17.0
# Automatically switches to new version
npm run test
# Should properly run your Promise.all 

Hope that helped.

Try wrapping Promise.allSettled in try-catch block if you don't want to update node js.

Example:

try {
  Promise.allSettled([
    // your code
  ]).finally(() => {
    // your code`enter code here`
  });
} catch (e) {
  console.log('promise.allSettled', e);
}
发布评论

评论列表(0)

  1. 暂无评论