【问题标题】:How to reduce duplicated code in tests by extracting parts of it in a function or by using it.each in Jest如何通过在函数中提取部分代码或在 Jest 中使用 it.each 来减少测试中的重复代码
【发布时间】:2023-02-22 09:20:04
【问题描述】:

我被要求通过使用 it.each 或创建一个函数来减少测试中的重复。我不知道该怎么做。我在测试套件中还有其他预计不会出错的功能。我假设我可以将下面两个重复的断言放在辅助函数中,但如果您有更好的建议,请告诉我。谢谢。

expect(<jest.Mock>fetch).not.toHaveBeenCalled();
expect(e).toBeInstanceOf(TypeError);

测试文件:

it('should throw if no args were passed', async () => {
            expect.assertions(3);
            return readPricing().catch((e: Error) => {
                expect(<jest.Mock>fetch).not.toHaveBeenCalled();
                expect(e).toBeInstanceOf(TypeError);
                expect(e.message).toBe(
                    `The 'products' information is required to successfully be able to execute this request.`
                );
            });
        });

        it('should throw if no product IDs were passed', () => {
            expect.assertions(3);
            const noIdData = {
                products: [{ salesPrice: '60.00' }, { salesPrice: '80.00' }],
            };
            // @ts-expect-error
            return readPricing(noIdData).catch((e: Error) => {
                expect(<jest.Mock>fetch).not.toHaveBeenCalled();
                expect(e).toBeInstanceOf(TypeError);
                expect(e.message).toBe(`The 'productId' is required to successfully be able to execute this request.`);
            });
        });

        it('should throw if an empty array of products was passed', () => {
            expect.assertions(3);
            const noIdData = { products: [] };
            return readPricing(noIdData).catch((e: Error) => {
                expect(<jest.Mock>fetch).not.toHaveBeenCalled();
                expect(e).toBeInstanceOf(TypeError);
                expect(e.message).toBe(
                    `The 'products' information is required to successfully be able to execute this request.`
                );
            });
        });

我尝试使用 it.each 但未能成功设置它,我不确定给定的输入是否可行。

【问题讨论】:

    标签: javascript jestjs ts-jest salesforce-lightning lwc


    【解决方案1】:

    it.each() 应该可以解决问题,您可以实现以下目标:

    describe("temp", () => {
      const productInfoMissingErr = `The 'products' information is required to successfully be able to execute this request.`;
      const productIdMissingErr = `The 'products' information is required to successfully be able to execute this request.`;
      const noIdData = {
        products: [{ salesPrice: "60.00" }, { salesPrice: "80.00" }],
      };
    
      it.each([
        ["no args were passed", undefined, productInfoMissingErr],
        ["no product IDs were passed", noIdData, productInfoMissingErr],
        [
          "an empty array of products was passed",
          { products: [] },
          productIdMissingErr,
        ],
      ])("should throw if %s", (caseTitle, input, errMsg) => {
        return readPricing().catch((e: Error) => {
          expect.assertions(3);
          expect(<jest.Mock>fetch).not.toHaveBeenCalled();
          expect(e).toBeInstanceOf(TypeError);
          expect(e.message).toBe(errMsg);
        });
      });
    });
    

    哪个应该呈现相同的消息:

      temp
        ✓ should throw if no args were passed (1 ms)
        ✓ should throw if no product IDs were passed
        ✓ should throw if an empty array of products was passed (1 ms)
    
    Test Suites: 1 passed, 1 total
    Tests:       3 passed, 3 total
    Snapshots:   0 total
    Time:        0.226 s, estimated 1 s
    Ran all test suites.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 2017-10-01
      • 1970-01-01
      • 2021-07-07
      • 2021-07-03
      • 1970-01-01
      • 2013-01-28
      相关资源
      最近更新 更多