您好,我正在尝试使用Jest测试以下代码,但是在运行测试时,我收到未定义的mData错误。请查看下面的代码,如果可能的话,请让我知道我在哪里出错。提前致谢。
function createMilkShakeData(mData) {
}
describe("milkshake Data Generator", () => {
it("creates correct data structure ", () => {
const mData = [
"milkshake1/milkshake1flavour",
"milkshake1/milkshake1flavor2",
"milkshake2/milkshake2flavour",
"milkshake2/milkshake2flavour2",
"milkshake1/milkshake1flavour3",
"flavour3",
"milkshake3/flavour3flavour1",
"milkshake4"
];
const expectedResult = [
{
title: "milkshake1",
data: ["milkshake1flavour", "milkshake1flavour2", "milkshake1flavour3"]
},
{ title: "milkshake2", data: ["milkshake2flavour", "milkshake2flavour2"] },
{ title: "milkshake3", data: ["milkshake3flavour1"] }
];
const actualResult = createMilkShakeData(mData);
expect(actualResult).toMatchObject(expectedResult);
});
});
module.exports = createMilkShakeData
module.exports = mData
下面的测试文件中的代码
const createMilkShakeData = require('./milkshake')
const data = require('./milkshake')
test('milkshake Data Generator', () => {
const actualResult = createMilkShakeData(mData);
expect(actualResult).toMatchObject(expectedResult)}
)
答案 0 :(得分:0)
mData
不在您的第一个文件的全局范围内定义-它仅在函数的范围中定义-在这种情况下,来自Jest it
。如果您需要导出,请在全局范围内(即在“描述”之前)进行定义。