且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何对组件进行单元测试以检查是否渲染了特定组件

更新时间:2023-11-29 15:21:40

删除app-navbar

compiled.querySelector('app-navbar')

实际上返回的是null(不是未定义),这就是toBeDefined检查通过的原因.

actually returns null, which is not undefined, that's why the toBeDefined check is passing.

在这里您可能要使用toBeTruthy().使用此功能,如果您的app-navbar在那里,您的测试用例将通过,如果您删除它,则将失败.

Here you may want to use toBeTruthy() instead. Using this, your test case will pass if you have the app-navbar there and will fail if you remove it.

在JavaScript中,有六个伪造的值:false,0,'',null,undefined和NaN.其他一切都是真实的. 在此处了解更多信息

In JavaScript, there are six falsy values: false, 0, '', null, undefined, and NaN. Everything else is truthy. Read more here