我正在尝试在Jest中创建一个类似于stringMatching的自定义匹配器,但它接受空值.但是,文档未显示如何重用现有匹配器.到目前为止,我有这样的事情:
expect.extend({
stringMatchingOrNull(received, argument) {
if (received === null) {
return {
pass: true,
message: () => 'String expected to be null.'
};
}
expect(received).stringMatching(argument);
}
});
Run Code Online (Sandbox Code Playgroud)
我不确定这是否是正确的方法,因为我在调用stringMatching匹配器时没有返回任何内容(这是在这里建议的).当我尝试使用这个匹配器时,我得到:expect.stringMatchingOrNull is not a function,即使在相同的测试用例中声明:
expect(player).toMatchObject({
playerName: expect.any(String),
rank: expect.stringMatchingOrNull(/^[AD]$/i)
[...]
});
Run Code Online (Sandbox Code Playgroud)
拜托,有人可以帮我展示正确的方法吗?
我正在使用Jest 20.0.4和Node.js 7.8.0运行测试.