如何测试导出函数内的减少函数Jest(反应)

时间:2017-08-18 21:39:48

标签: javascript reactjs unit-testing testing redux

我有一个反应器中的reducer,我正在测试我想在exports默认函数中测试reduce的返回值。而且我不确定如何访问该函数中的reduce。

MyReducer:

exports default function myReducer({
 people = [0,1,2];
 people.reduce(function(sum, peeps){
 return sum + peeps;
 }0);
 return "hey";
})

在开玩笑测试中:

import myReducer from 'myReducer';
expectedPeeps = 3;
expect(Something needs to go here).toEqual(expectedPeeps);

我需要弄清楚如何获得people.reduce的返回值。我可以通过调用myReducer()来获得“嘿”。但是如何在我的测试中获得people.reduce的返回值! ?提前感谢窥视。

1 个答案:

答案 0 :(得分:0)

首先,MyReducer中存在多个语法问题。我认为它应该是这样的:

export default function myReducer() {
  const people = [0,1,2];
  people.reduce(function(sum, peeps){
    return sum + peeps;
  }, 0);
  return "hey";
}

如果您需要自行测试reduce,可以将其移动到自己的函数中并导出它:

export function reducePeople(people) {
  return people.reduce(function(sum, peeps){
    return sum + peeps;
  }, 0);
}

export default function myReducer() {
  people = [0,1,2];
  reducePeople(people);
  return "hey";
}

在测试中导入并测试它:

import { reducePeople } from 'myReducer';
expectedPeeps = 3;
expect(reducePeople([0,1,2])).toEqual(expectedPeeps);