【发布时间】:2018-08-12 06:14:42
【问题描述】:
我正在使用 create-react-app 并尝试编写一个 jest 测试来检查 console.log 的输出。
我要测试的功能是:
export const log = logMsg => console.log(logMsg);
我的测试是:
it('console.log the text "hello"', () => {
console.log = jest.fn('hello');
expect(logMsg).toBe('hello');
});
这是我的错误
FAIL src/utils/general.test.js
● console.log the text hello
expect(received).toBe(expected) Expected value to be (using ===): "hello"
Received:
undefined
Difference:
Comparing two different types of values. Expected string but received undefined.
【问题讨论】:
-
测试中函数的调用在哪里?您应该在进行断言之前调用它。
-
对不起,我不明白你的意思?
-
能否请您包含整个测试文件?你在哪里声明
logMsg? -
这就是我所拥有的,logMsg 是传入的文本。
标签: javascript reactjs jestjs