我正在尝试使用一个使用 For Each 的纯函数,该函数将更改输入数组以返回“x”。有人可以帮我解释为什么我会收到这个错误吗?
功能:
let functions = {
helper: (x) => {
return x;
},
changeArray: (x) => {
let arr1 = [x];
arr1.forEach(functions.helper);
return arr1[0];
}
};
测试文件:
test('For Each', () => {
expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']));
})
结果/错误:
TypeError: _syntax.default.changeArray is not a function
73 |
74 | test('For Each', () => {
> 75 | expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']));
| ^
76 | })
变化:
const syntax{
helper: (x) => x,
changeArray: (arr) => {
return arr.map(syntax.helper);
}
}
测试文件:
test('For Each', () => {
expect(syntax.changeArray(['hey', 'hi'])).toBe(['x','x']);
})
结果:
expect(received).toBe(expected) // Object.is equality
- Expected
+ Received
Array [
- "x",
- "x",
+ "hey",
+ "hi",
]