我刚开始在Vue中进行单元测试,并且已经在我的第一个组件上遇到了障碍。
这是我的测试:
import { shallowMount } from '@vue/test-utils';
import GeoframeLibrary from '@/views/GeoframeLibrary.vue';
describe('GeoframeLibrary', () => {
const wrapper = shallowMount(GeoframeLibrary);
it('renders', () => {
expect(wrapper.element).toMatchSnapshot();
});
});
问题是
GeoframeLibrary.vue
FAIL tests/unit/GeoframeLibrary.spec.ts
â Test suite failed to run
TypeError: Cannot read property 'use' of undefined
8 | import { AuthState } from './modules/auth';
9 |
> 10 | Vue.use(Vuex);
| ^
11 |
12 | const vuexPersist = new VuexPersistence({
13 | key: 'onspot',
at Object.<anonymous> (src/store/index.ts:10:5)
at Object.<anonymous> (src/adonis-api/api-call.ts:7:1)
at Object.<anonymous> (src/adonis-api/auth.ts:1:1)
at Object.<anonymous> (src/adonis-api/index.ts:1:1)
at src/views/GeoframeLibrary.vue:55:1
at Object.<anonymous> (src/views/GeoframeLibrary.vue:66:3)
at Object.<anonymous> (tests/unit/GeoframeLibrary.spec.ts:3:1)
大多数Vue单元测试教程都过于简单化,没有涵盖这些真实场景。我需要我的组件导入其他代码来执行专门的例程。我该如何解决这个问题并让我的测试
不
跟随进口,或者只是模仿
Vue.use
什么都不做?Vuex实际上根本不属于我的组件,它只是碰巧出现在错误中,因为它正在被导入。
编辑:这确实是一个奇怪的边缘案例。这是复制品回购协议:
https://github.com/ffxsam/repro-jest-importing
src/adonis-api/crud-factory.ts
,测试运行正常。
非常
很奇怪。哦,如果你把第8行注释掉
src/adonis-api/api-call.ts