如何强迫Jest看到嵌套的javascript函数?

时间:2018-06-30 10:36:15

标签: javascript jestjs create-react-app

我的项目中有这样的结构

|...
|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

1 个答案:

答案 0 :(得分:0)

将其移至顶部或使其成为常规function,以便将其吊起。使用该函数后会对其进行分配,但会声明该变量。

export const getSurroundingCells = (numRows, numCols, shipPositions) => {

    const cellIsSuitable = (position, shipPositions) => {
        ...
        // some logic that uses numRows, numCols
        ...
    }

    ...
    // some logic that calls cellIsSuitable()
    ...
}