在将@ testing-library / jest-dom版本从4.0.0更新到4.2.0时,用于通过使用toHaveStyles检查样式的测试用例失败。 使用react-testing-library进行测试:
test('renders component', () => {
const { getByTestId } = render(<Temp labels={labels} justify="end" />);
expect(getByTestId('temp')).toHaveStyle('justifyContent: end');
});
上述测试失败,并引发如下错误:
● Temp › renders component
expect(element).toHaveStyle()
- Expected
- justifyContent: end;
+
24 | test('renders component', () => {
25 | const { getByTestId } = render(<Temp labels={labels} justify="end" />);
> 26 | expect(getByTestId('temp')).toHaveStyle('justifyContent: end');
| ^
27 | });
28 | });
29 |
at Object.toHaveStyle (src/components/Temp/tests/Temp.test.js:26:35)
有人知道此问题的解决方法吗?
答案 0 :(得分:1)
public class Main {
/**
* Expensive, lazy object instantiation
*/
public static Mono<ExpensiveObject> initExpensiveObject() {
return Mono.fromCallable(ExpensiveObject::new);
}
/**
* Expensive, async mapping (i.e. database access, network request):
* ExpensiveObject -> int
*/
public static Mono<Integer> initExpensiveField(ExpensiveObject expObj) {
return Mono.just(1);
}
public static final class ExpensiveObject {
private final int expensiveField;
public ExpensiveObject() {
expensiveField = -1;
}
private ExpensiveObject(int expensiveField) {
this.expensiveField = expensiveField;
}
public int getExpensiveField() {
return expensiveField;
}
public Mono<ExpensiveObject> withExpensiveField(Mono<Integer> expensiveField) {
return expensiveField.map(ExpensiveObject::new);
}
}
}
使用渲染的样式,因此您的测试需要:
with