当我尝试检查输入整数数组是否为空时遇到问题。错误消息只会显示:找不到符号。
如果prices数组不为空,则我希望能够运行其余代码。
如果是,我希望它返回0。
// import { greet } from "../index.js"; =====> Import on each test case(it)
// import * as greeter from "../greeter"; =====> Mock on each test case(it)
// greeter.sayHello = jest.fn(); =====> Would be not good to do this, it will mess with the entire import and this jest.fn will be share across your tests making it gives you false positives.
describe("greet", () => {
it("Should delegate the call to greeter.sayHello", () => {
const name = "John";
jest.mock('../greeter', () => ({ sayHello: jest.fn() })); // here you mock the import, remember it needs to be before you require your index.js
const greet = require('../index.js').greet; // require after the mock
greet(name);
expect(greeter.sayHello).toHaveBeenCalledWith(name);
});
});
答案 0 :(得分:0)
如果要检查价格数组不为空,请使用以下代码if(null!= prices && prices.length!= 0)