vue.js - Vue3 计算属性仅在运行测试时才重新评估(通过 karma/jasmine)
问题描述
我正在尝试为具有业力/茉莉花的 Vue 3 组件的计算属性编写测试断言值。计算属性确实会发生变化,并在应用程序运行时不断被重新评估,但在测试中似乎没有被重新评估。下面是要测试的组件和测试本身的非常简化的代码:
// Component to test
const Component1 = defineComponent({
setup() {
const someString = inject("someString");
return { someString };
},
computed: {
isSuccess(): boolean {
return this.someString == "success";
}
}
});
export default Component1;
// Test
import "Component1" from "/path/to/component";
describe("Component1 tests", function() {
let wrapper: VueWrapper<any>;
let inTest: any;
beforeEach(function(){
wrapper = mount(Component1, {
global: {
provide: {
someString: 'failure'
}
}
});
inTest = wrapper.vm;
});
describe("isSuccess should return true if someString is 'success'", function(){
expect(inTest.isSuccess).toBeFalse(); // this passes
inTest.someString = 'success';
expect(inTest.isSuccess).toBeTrue(); // but this fails
});
});
我在这里错过了什么吗?
解决方案
ref
除非您传递属性或reactive
对象 ,否则提供的值不是反应性的。见这里。
所以我相信你可以通过几个小的调整让它工作。
// Test
import { ref } from 'vue'
import "Component1" from "/path/to/component";
describe("Component1 tests", function() {
let wrapper: VueWrapper<any>;
let providedValue = ref('failure')
let inTest: any;
beforeEach(function(){
wrapper = mount(Component1, {
global: {
provide: {
someString: providedValue
}
}
});
inTest = wrapper.vm;
});
describe("isSuccess should return true if someString is 'success'", function(){
expect(inTest.isSuccess).toBeFalse(); // this passes
providedValue.value = 'success';
expect(inTest.isSuccess).toBeTrue(); // but this fails
});
});
推荐阅读
- ios - iOS 上的 OpenVPN 3 客户端连接,但无法发送数据,“未知 IP 版本”
- c# - 为什么我不能将自定义文本框放在面板或 groupBox 中?
- python - NLTK ImportError: DLL load failed: 找不到指定的模块
- python - 解释器何时以及为什么会通过假设相同长度的子列表来解开?
- javascript - 追加到猫鼬填充中的数组?
- javascript - 如何在 GoJS 中向端口添加数字?
- .htaccess - 301 重定向到带有 HSTS 预加载标头的 WWW
- saml-2.0 - Kentor/Sustainsys 在收到来自 IDP 的 LogoutResponse 后重定向回 SP
- uwp - UWP 设备信息文件
- angular - 量角器:.spec 和 .po.ts 文件执行的时间安排?