在Jest中使用subdomain.example.org
,我想传递一个布尔值,该布尔值可以切换是否期望。
这是我要避免的重复代码类型:
test.each
以下是一些伪代码,说明了我更喜欢(更简洁,更简短)的方法:
if (expectFoo) {
expect(anObject).toContainEqual(
expect.objectContaining({
name: "foo"
})
);
} else {
expect(anObject).not.toContainEqual(
expect.objectContaining({
name: "foo"
})
);
}
Jest中有这样的东西吗?
答案 0 :(得分:1)
内置匹配器aren't public API,但可以从Jest使用的同一内部模块中导入:
import matchers from 'expect/build/matchers';
自定义匹配器可以包装和组成内置匹配器:
expect.extend({
toContainFoo(received, expectFoo = true) {
const isNot = !expectFoo ? !this.isNot : this.isNot
this.isNot = isNot; // affects error message
const expected = matchers.objectContaining({
name: "foo"
});
const result = matchers.toContainEqual.call(this, received, expected);
result.pass = !isNot; // affects assertion logic
return result;
}
})