如何在笑话中测试条件和 return 语句?

how to test if condition and return statement inside jest?

我刚开始学习 Jest。 使用 Jest 针对 If case 和 return 语句测试此函数的其他方法有哪些? 这是用 Jest

测试的功能
const extractInfo = (description: string) => {
        const info= description.match('descriptionInformation');
        if (info) {
          return info[0];
          }
         return 'hello jest';
        };

尝试了以下测试用例来检查 Info.test.ts

  test('extractInfo method to be defined', () => {
    expect(extractInfo ).toBeDefined();
  });

  test('extractInfo to be called when Info has blank string', () => {
    const BLANK_STRING = '';
    expect(extractInfo ).toHaveBeenCalled();
    expect(extractInfo ).toHaveBeenCalledWith(BLANK_STRING);
  })

    const extractInfo = (Info: string) => {
        const info= description.match(jestinformation);
        if (info) {
          return info[0];
          }
         return 'hello jest';
        };

请提供覆盖每一行的方法。谢谢

让我们归结为:

I want to test this function:

const extractInfo = (Info: string) => {
        const info= description.match(jestinformation);
        if (info) {
          return info[0];
          }
         return 'hello jest';
        };

因此,要测试此功能,您需要提供一些输入并期待输出。所以你可以像这样写一个测试:

describe('extractInfo', () => {
   test('test 1', () => {
      //inputs
      const Info = '';
      const description = '';
      const jestinformation = '';

      //test
      const result = extractInfo(Info);
    
      //expect
      expect(result).toEqual('hello jest');
   });
});

因此构建您的输入、运行您的测试、断言/期望 没错。这是适用于所有 languages/frameworks.

中所有单元测试的基本单元测试模式

所有人都说你的输入和输出以及你的测试仍然是错误的,但希望这能回答