如何断言包含一些固定单词和一些可变数字的字符串?

How to assert a string containing some fixed words along with some variable numbers?

我想断言:“30 天内有 10 个自动结果”,其中 10 和 30 来自 API 调用,所以它可以是任何东西。

我正在寻找 Jest 断言,例如:

expect(pageobject.webelement.getText()).toEqual("10 Automated results in 30 days"); // but it fails some other day because 10 & 30 can be any number at any point of time.

我如何断言这一点,以便我的代码查找准确的字符串,但不包括指定位置的任何数字?

你可以像这样使用正则表达式来匹配字符串,

describe('stringMatching', () => {
   const expected = /[0-9]* Automated results in [0-9]* days/;

   it('matches if the received value does not match the expected regex', () => {
       expect(pageobject.webelement.getText()).toMatch(expected);
   });
});