reactjs - 以下内容的单元测试:
问题描述
尝试为以下代码设置单元测试,但我不断得到这个:比较两种不同类型的值。预期的字符串,但收到未定义。作为错误
Test :
import {prepadSigned} from './utils';
describe('prepadSigned', () => {test('should prepend `00` to the input <', () => {
const str = '-10';
const actual = prepadSigned(str);
const expected = '00-10';
expect(actual).toEqual(expected);
})
代码:
function prepadSigned(hexStr) {
const msb = hexStr[0];
if (msb < '0' || msb > '7') {
return `00${hexStr}`;
}
return hexStr;
}
我希望单元测试能够通过一些输入/输出。我不断收到以下信息:
expect(received).toEqual(expected)
Expected value to equal:
"00-10"
Received:
undefined
解决方案
export
除非你写的时候故意漏掉,
改变
function prepadSigned(hexStr) {
...
}
至
export function prepadSigned(hexStr) {
...
}
可以解决问题。
推荐阅读
- reactjs - 在反应应用程序中获取快照侦听器错误
- typescript - 如何将已定义的接口与 io-ts 库一起使用?
- javascript - 根据允许的值/字符串数组验证字符串
- php - 在textarea中按Enter时如何添加一行而不是空格?
- java - 如果我们从地图上掉下来,二维数组和检测(JAVA)
- json - 从维基百科获取标题
- java - 如何确定在 recycelview 布局上单击了哪个卡片视图?
- svelte - 没有变化的苗条更新总是触发重新渲染
- python - 在 Django 中向特定用户显示私人内容
- system-verilog - 我无法编译 .sv 文件 (SystemVerilog)