我的项目中有这样的结构
|...
|Utils
| Utils.js
| Utils.test.js
|...
在Utils.js
中,我有功能,我想测试:
export const getSurroundingCells = (numRows, numCols, shipPositions) => {
...
// some logic that calls cellIsSuitable()
...
const cellIsSuitable = (position, shipPositions) => {
...
// some logic that uses numRows, numCols
...
}
}
当我想通过Jest
通过Utils.test.js
对其进行测试时:
import {getSurroundingCells} from "./Utils";
const shipPositions = [[1, 1], [1, 2], [1, 3]];
const expectedSurroundingCells = [[2, 1], [2, 0], [1, 0], [0,0], [0,1], [0,2], [2,2]];
test('takes positions and returns surrounding cells', () => {
expect(getSurroundingCells(10, 10, shipPositions)).toBe(expectedSurroundingCells);
});
我得到:
ReferenceError:未定义cellIsSuitable
如何强制Jest
来查看我的嵌套函数cellIsSuitable
?
P.S。我的项目启动了with create-react-app
。
答案 0 :(得分:0)
将其移至顶部或使其成为常规function
,以便将其吊起。使用该函数后会对其进行分配,但会声明该变量。
export const getSurroundingCells = (numRows, numCols, shipPositions) => {
const cellIsSuitable = (position, shipPositions) => {
...
// some logic that uses numRows, numCols
...
}
...
// some logic that calls cellIsSuitable()
...
}