如何使用 Vue 测试库测试 Vue Router 导航守卫?

How to test Vue Router's navigation guards using Vue Testing Library?

我想在我的 Vue 2 项目中测试 Vue Router 的“beforeRouteUpdate”和“beforeRouteLeave”导航守卫。

尽管关于如何使用 Vue Test Utils 测试它们的答案很多,但我找不到如何使用 Vue Testing Library 进行测试。在这些答案中(对于 Vue Test Utils),他们建议通过

获取函数
wrapper.vm.$options.beforeRouteUpdate[0]

不幸的是,Vue 测试库中似乎the wrapper is not exposed,所以我卡住了。

那么...使用 Vue 测试库测试 Vue Router 的导航守卫的正确方法是什么?或者我应该为此目的使用 Vue Test Utils 吗?

谈论测试库:

他们希望您专注于测试用户观察到的组件的输出和功能,避免担心组件的实现细节。

那么如果你真的想测试路由器、名称、mixins 之类的东西,你应该使用 Test utils。但是你可以同时使用两者 =)

你可以在这里看到:

https://testing-library.com/docs/dom-testing-library/faq/

https://testing-library.com/docs/guiding-principles/