gpt4 book ai didi

javascript - 方法在单元测试中不可用

转载 作者:行者123 更新时间:2023-12-01 16:10:47 24 4
gpt4 key购买 nike

我使用 TypeScript 在 VueJS 中创建了新项目。

我的组件带有测试方法:

<template>
<div></div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';

@Component
export default class Slider extends Vue {
private slide: number = 0;
private sliding: boolean = false;

public setSlide(slide: number): void {
this.slide = slide;
}

public setSliding(sliding: boolean): void {
this.sliding = sliding;
}

private onSliderStart(slide: any): void {
this.setSliding(true);
}

private onSlideEnd(slide: any): void {
this.setSliding(false);
}
}
</script>

测试:
import { shallowMount } from '@vue/test-utils';
import Slider from '@/components/Header.vue';

describe('Slider', () => {
const wrapper = shallowMount(Slider);

it('check Slider is a Vue Instance', () => {
expect(wrapper.isVueInstance()).toBeTruthy();
});

it('setSlide is func', () => {
expect(typeof wrapper.vm.setSlide).toBe('function')
})
});

现在我想做测试,但是方法 setSlide,setSliding 在包装器中不可用:(

最佳答案

尝试这个:

import YourComponentHere from "@/components/YourComponentHere";
import { shallowMount, Wrapper } from "@vue/test-utils";

describe("test", () => {
const wrapper: Wrapper<YourComponentHere & { [key: string]: any }>;
it("does something", () => {
expect(wrapper.vm.someThingWhatever).toBe(true);
});
});
这里的好处是你不需要转换 wrapper.vmany每次使用 wrapper.vm

关于javascript - 方法在单元测试中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57021241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com