首页 > 语言 > JavaScript > 正文

详解在vue-test-utils中mock全局对象

2024-05-06 15:28:09
字体:
来源:转载
供稿:网友

vue-test-utils   提供了一种 mock 掉   Vue.prototype   的简单方式,不但对测试用例适用,也可以为所有测试设置默认的 mock。

mocks   加载选项

mocks   加载选项   是一种将任何属性附加到   Vue.prototype   上的方式。这通常包括:

$store , for Vuex $router , for Vue Router $t , for vue-i18n

以及其他种种。

vue-i18n   的例子

我们来看一个 vue-i18n   的例子。虽然可以为每个测试用到   createLocalVue   并安装   vue-i18n ,但那样可能会让事情难以处理并引入一堆样板。首先,组件   <Bilingual>   用到了   vue-i18n :

<template> <div class="hello">  {{ $t("helloWorld") }} </div></template><script> export default {  name: "Bilingual" }</script>

你先在另一个文件中弄好翻译,然后通过 $t   引用,这就是   vue-i18n   的工作方式。在本次测试中,虽然并不会真正关心翻译文件看起来什么样,不过还是看一看这次用到的:

export default { "en": {  helloWorld: "Hello world!" }, "ja": {  helloWorld: "こんにちは、世界!" }}

基于这个 locale,正确的翻译将被渲染出来。我们先不用 mock,尝试在测试中渲染该组件:

import { shallowMount } from "@vue/test-utils"import Bilingual from "@/components/Bilingual.vue"describe("Bilingual", () => { it("renders successfully", () => {  const wrapper = shallowMount(Bilingual) })})

通过 yarn test:unit   运行测试将抛出一堆错误堆栈。若仔细端详输出则会发现:

[Vue warn]: Error in config.errorHandler: "TypeError: _vm.$t is not a function"

这是因为我们并未安装 vue-i18n ,所以全局的   $t   方法并不存在。我们试试   mocks   加载选项:

import { shallowMount } from "@vue/test-utils"import Bilingual from "@/components/Bilingual.vue"describe("Bilingual", () => { it("renders successfully", () => {  const wrapper = shallowMount(Bilingual, {   mocks: {    $t: (msg) => msg   }  }) })})

现在测试通过了! mocks   选项用处多多,而我觉得最最常用的正是开头提到过的那三样。

(译注:通过这种方式就不能在单元测试中耦合与特定语言相关的内容了,因为翻译功能实际上已失效,也更无法处理可选参数等)

使用配置设置默认的 mocks

有时需要一个 mock 的默认值,这样就不用为每个测试用例都设置一遍了。可以用 vue-test-utils   提供的   config   API 来实现。还是   vue-i18n   的例子:

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选