首页 > 解决方案 > 以下内容的单元测试:

问题描述

尝试为以下代码设置单元测试,但我不断得到这个:比较两种不同类型的值。预期的字符串,但收到未定义。作为错误

 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

标签: reactjsunit-testingjestjs

解决方案


export除非你写的时候故意漏掉,

改变

function prepadSigned(hexStr) {
...
}

export function prepadSigned(hexStr) {
...
}

可以解决问题。


推荐阅读