首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReferenceError:在Vitest测试套件中未定义计算

ReferenceError:在Vitest测试套件中未定义计算
EN

Stack Overflow用户
提问于 2022-07-20 06:15:02
回答 1查看 561关注 0票数 2

描述

我正在将测试套件从Jest迁移到Vitest。但我在运行测试套件时遇到了问题,当组件具有计算属性时会发生错误。

常见的错误是:

代码语言:javascript
复制
ReferenceError: computed is not defined

 - /components/Ui/Avatar.vue:13:30
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:157:22
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7084:29
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7039:11
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5401:13
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5376:17
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:4978:21
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5515:21
 - /node_modules/@vue/reactivity/dist/reactivity.cjs.js:189:25
 - /node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5622:56

版本

  • “最暴力”:"^0.18.1“
  • "jsdom":"^20.0.0“
  • "@vue/test-utils":"^2.0.2“

以下是我的组件代码:

代码语言:javascript
复制
<template>
  <image
    :src="src"
    :onerror="onErrorLoadImage"
    :class="['avatar', { big }]"
  />
</template>

<script setup lang="ts">
const props = withDefaults(defineProps<{
  src?: string
  big?: boolean
  errorImage?: string
}>(), {
  src: '',
  big: false,
  errorImage: '/no-avatar.png',
})

const onErrorLoadImage = computed(() => `this.src='${props.errorImage}';this.onerror='';`)
</script>

还有我的测试

代码语言:javascript
复制
import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import UiAvatar from './Avatar.vue'

const componentName = 'img'
const src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='
const big = true
const errorImage = '/no-avatar.png'

describe('UiAvatar', () => {
  it('should be render the component', () => {
    const wrapper = mount(UiAvatar, {
      propsData: {
        src,
        big,
        errorImage
      }
    })
    expect(wrapper.element.tagName).toBe(componentName)
  })
})

谢谢:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-16 02:46:48

这可以通过使用以下npm包来解决:unplugin components/vite unplugin-auto/vite

他们只需要指定一个生成导入文件的路径,在我的例子中,我使用一个故事书文件夹,因为我也在那里使用插件,但它可以是任何其他路径。

vitest.config.ts

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73046743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档