我想通过玩笑的 test 声明检查一些数字。我希望此数字显示在测试名称中,以获得更清晰的输出,所以我这样做是这样的:
// code
const balance = 500;
test("My balance is 500 EUR", async () => {
expect(balance).toBe(500)
});
//output
PASS tests/scenarios/qwer.test.ts
√ My balance is 500 EUR
但是,此数字现在在代码中定义了两次。我想在测试名称中声明它并在测试主体中使用。这对于某些外部变量是可行的,但我想避免使用它:
// code
const testName = "so awesome name";
test(`Test name is "${testName}"`, async () => {
expect(1).toBeTruthy();
});
//output
PASS tests/scenarios/qwer.test.ts
√ Test name is "so awesome name" (4ms)
它也可以用于 test.each 声明,但是我只有一个测试:
// code
test.each`
a | b | sum
${20} | ${30} | ${50}`
("$a + $b is $sum", async (currentCase) => {
expect(currentCase.sum).toBe(currentCase.a + currentCase.b)
});
//output
PASS tests/scenarios/qwer.test.ts
√ 20 + 30 is 50 (1ms)