如何强制 Jest 看到嵌套的 javascript 函数?
How to force Jest to see nested javascript function?
我的项目中有这样的结构
|...
|Utils
| Utils.js
| Utils.test.js
|...
在Utils.js
我有func,我想测试:
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 is not defined
如何强制 Jest
查看我的嵌套函数 cellIsSuitable
?
P.S。我的项目是自举的 with create-react-app
。
将其移至顶部或使其成为常规 function
以便将其提升。函数是在你使用后赋值的,但变量是声明的。
export const getSurroundingCells = (numRows, numCols, shipPositions) => {
const cellIsSuitable = (position, shipPositions) => {
...
// some logic that uses numRows, numCols
...
}
...
// some logic that calls cellIsSuitable()
...
}
我的项目中有这样的结构
|...
|Utils
| Utils.js
| Utils.test.js
|...
在Utils.js
我有func,我想测试:
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 is not defined
如何强制 Jest
查看我的嵌套函数 cellIsSuitable
?
P.S。我的项目是自举的 with create-react-app
。
将其移至顶部或使其成为常规 function
以便将其提升。函数是在你使用后赋值的,但变量是声明的。
export const getSurroundingCells = (numRows, numCols, shipPositions) => {
const cellIsSuitable = (position, shipPositions) => {
...
// some logic that uses numRows, numCols
...
}
...
// some logic that calls cellIsSuitable()
...
}