when i learn vue2 + vitest by vue2 demo, miss this line, and get unexpect result, make me confused #1901
-
here is component: <script setup>
import { computed, ref } from "vue";
const counter = ref(0);
const doubleCount = computed(() => counter.value * 2);
</script>
<template>
<div>
<div>{{ counter }} * 2 = {{ doubleCount }}</div>
<button @click="counter += 1">+1</button>
</div>
</template> vite.config.js without this line export default defineConfig({
resolve: {
alias: {
"~": fileURLToPath(new URL("./src", import.meta.url)),
},
},
test: {
environment: "jsdom",
//alias: [{ find: /^vue$/, replacement: "vue/dist/vue.runtime.common.js" }],
},
plugins: [vue2()],
}); this result is unexpected. after 'click', counter should be 1 and doubleCount should be 2 test("renders content", async () => {
const wrapper = mount(HelloWorld);
expect(wrapper.html()).toMatchInlineSnapshot(`
"<div>
<div>0 * 2 = 0</div><button>+1</button>
</div>"
`);
await wrapper.get("button").trigger("click");
expect(wrapper.html()).toMatchInlineSnapshot(`
"<div>
<div>0 * 2 = 0</div><button>+1</button>
</div>"
`);
}); why this is important? need help |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
vue test utils uses CJS version of vue. if you don't alias it in your application, Vitest will use ESM version. this is why we have this line in example |
Beta Was this translation helpful? Give feedback.
-
thanks for your reply. another problem. <script setup>
import { storeToRefs } from "pinia";
import { useCounterStore } from "~/stores/counter.js";
const { counter, doubleCount, increment } = storeToRefs(useCounterStore());
</script>
<template>
<div>
<div>{{ counter }} * 2 = {{ doubleCount }}</div>
<button @click="increment">+1</button>
</div>
</template>
test("renders content", async () => {
const localVue = createLocalVue();
localVue.use(PiniaVuePlugin);
const wrapper = mount(HelloWorld, {
localVue,
pinia: createTestingPinia({
createSpy: vi.fn,
}),
});
const counter = useCounterStore();
expect(counter.counter).toBe(0);
await wrapper.get("button").trigger("click");
expect(counter.increment).toHaveBeenCalledTimes(1);
expect(wrapper.html()).toMatchInlineSnapshot(`
"<div>
<div>0 * 2 = 0</div><button>+1</button>
</div>"
`);
await wrapper.get("button").trigger("click");
expect(counter.increment).toHaveBeenCalledTimes(2);
expect(wrapper.html()).toMatchInlineSnapshot(`
"<div>
<div>0 * 2 = 0</div><button>+1</button>
</div>"
`);
}); |
Beta Was this translation helpful? Give feedback.
vue test utils uses CJS version of vue. if you don't alias it in your application, Vitest will use ESM version. this is why we have this line in example