使用自定义功能验证Mocha/Chai



我是Chai/Mocha的新手。我有一个自定义函数,可以使用正则表达式验证字符串(例如(。我想使用这个功能。这是我的代码:

describe('Custom Function', () => {
it('Function 1', () => {
// I'm calling a function here which returns a string, I want to validate that(only as example)
expect(someFunction())
});
});

这可能看起来很简单,但我是新手,对此感到抱歉。现在,关于示例验证函数,我知道我可以使用.to.match(/SOME_REGEX/),但我在其他地方使用相同的函数,我不想重复代码。我的函数将从API获取数据,并相应地验证测试结果

不是Mocha Chai自定义比较功能的副本

您可以使用to.be.true,也可以使用RegExp.test((方法,例如:

const regex = /^(H|HH)/;
expect(regex.test(string)).to.be.true;

最新更新