Javascript TypeError:routingState.equals不是函数| Jest测试用例
这是我的selector.js文件:-Javascript TypeError:routingState.equals不是函数| Jest测试用例,javascript,reactjs,react-redux,jestjs,enzyme,Javascript,Reactjs,React Redux,Jestjs,Enzyme,这是我的selector.js文件:- const makeSelectLocationState = () => { let prevRoutingState; let prevRoutingStateJS; return (state) => { const routingState = state.get('route'); // or state.route if (!routingState.equals(prevRoutingState))
const makeSelectLocationState = () => {
let prevRoutingState;
let prevRoutingStateJS;
return (state) => {
const routingState = state.get('route'); // or state.route
if (!routingState.equals(prevRoutingState)) {
prevRoutingState = routingState;
prevRoutingStateJS = routingState.toJS();
}
return prevRoutingStateJS;
};
};
下面是我编写的测试用例:-
it('makeSelectLocationState function testing', () => {
const selectLocationState = makeSelectLocationState();
const mockedState = fromJS({
route: '',
});
expect(selectLocationState(mockedState)).toEqual('');
});
使用jest运行测试用例时,我遇到以下错误:-
TypeError: routingState.equals is not a function
问题在于:
const routingState = state.get('route');
感觉您没有获得routingState,可能未定义,因此您将收到上述错误消息。因此请检查routingState的日期。
在测试文件中,将mockedState传递给selectLocationState
selectLocationState(mockedState)
但是,makeSelectLocationState函数没有属性。可能更改为:
const makeSelectLocationState = (state) => {}
看起来route
是一个字符串(空字符串)。字符串没有equals
方法,因此routingState.equals()
不存在
也许您可以将mockedState.route
设置为具有equals
功能的对象:
const mockedState = fromJS({
route: { equals: item => item === item },
});
TypeError:无法读取未定义的属性“equals”。在做了更改之后,我得到了上面的错误@root@Shwetank请在makeSelectLocationState中提供console.log(routingState)的输出。console.log(routingState)的输出为映射{size:1、\u root:ArrayMapNode、\uuuuOwnerId:undefined、\uuuuuuHash:undefined、\uuuuuuuu Changed:false}@root@Shwetank虽然routingState的类型是Map,和Map实际上没有equals函数。您可以使用它:routingState.toString()===prevRoutingState.toString()或使用forEach检查routingState的每个项目。
const mockedState = fromJS({
route: { equals: item => item === item },
});