如何通过在函数中提取代码的一部分或使用代码来减少测试中的重复代码,each in Jest

oknwwptz  于 2023-03-06  发布在  Jest
关注(0)|答案(1)|浏览(150)

我被要求在我的测试中通过使用它来减少重复。each或者创建一个函数。我不确定如何去做。我在测试套件中有其他函数不期望错误。我假设我可以把下面两个重复的Assert放在一个帮助函数中,但是如果你有更好的建议,让我知道。谢谢。

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.`
                );
            });
        });

我尝试使用它。每个,但未能成功地设置它,我不确定它是否可能与给定的输入。

w8f9ii69

w8f9ii691#

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.

相关问题