Vuex + Jest测试失败并出现TypeError

Teos Verdi

我正在尝试在我的应用程序上测试Vuex存储模块,但是TypeError: Cannot read property 'getters' of undefined无论我尝试什么,它都会不断返回请帮忙。这是代码:

商店/index.ts:

import Vuex from 'vuex';

import auth from './auth';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {},
  mutations: {},
  actions: {},
  modules: {
    auth,
  },
});

商店/身份验证:

export default {
  state: {
    isLoggedIn: getKey(),
  },
  namespaced: true,
  mutations: {
    setLoginState(state: any, isLoggedIn: boolean) {
      state.isLoggedIn = isLoggedIn;
    },
  },
  actions: {
   // Stuff
  },
};

商店/ __测试__ /身份验证:

import auth from '../auth';

describe('Auth.ts', () => {
  describe('Mutations', () => {
    test('setLoginState sets isLoggedIn correctly', () => {
      const state = {
        isLoggedIn: false,
      };
      auth.mutations.setLoginState(state, true);
      expect(state.isLoggedIn).toBe(true);
    });
  });
});

使用的软件包:

    "vue": "^2.5.22",
    "vuex": "^3.1.0",
    "webpack": "^4.29.0"
    "jest": "^23.6.0",
    "ts-jest": "^23.10.5",
Teos Verdi

我发现了问题。问题是auth.ts正在导入axios实例,但没有被模拟。删除需要axios调用的操作或添加jest.mock('API_PATH', jest.fn); 测试以解决问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

测试失败:Jest + Vue 3 + Vuex + Typescript:TypeError:无法读取未定义的属性“then”

用Jest测试Nuxt + Vuex + Vuetify的设置

测试VueX商店

如何正确测试Vuex?

Mocha测试失败并出现AssertionError

使用vuex模块时如何测试突变

使用Vuex时如何测试Vuejs组件

如何在 Vuex 模块中测试操作?

SpringBoot集成测试失败,并出现java.awt.HeadlessException

Maven测试失败,并出现ANTLR类的NoClassDefFoundError

如何为使用Vuex存储的Vue表单组件编写Jest单元测试?

我们可以修改 Vuex 操作以进行 Jest 单元测试吗?

在Vuex + Jest中,如何对调用商店的吸气剂进行单元测试?

如何使用 vuex-test-utils 测试组件中的 VueX 状态变化?

在 nuxt 中出现此错误:[vuex] 不要在变异处理程序之外改变 vuex 存储状态

Vue3 vuex“ TypeError:vuex__WEBPACK_IMPORTED_MODULE_1 __。Store.commit不是函数”

用Jest测试NUXT.js和Vue.js应用程序。获取“在mapState()中找不到[vuex]模块名称空间”和“ [vuex]未知操作类型”

使用“ vuex-orm-decorators”时出现“ TypeError:this.types不是函数”

Vue CLI 插件 Electron Builder 调度 vuex 操作失败

使用参数返回 undefined 测试 Vuex getter 不是函数

使用Vuex测试Vue组件时,为什么需要createLocalVue?

在Vuex动作返回的Promise中测试VueJS方法

组件单元测试中的模拟Vuex模块动作

Vuex返回“ TypeError:无法读取未定义的属性'getters'”

Vuex“ TypeError:无法读取未定义的属性'data'”

TypeError:state.categoriesState.push不是函数VUEX

Vue js Vuex TypeError:context.dispatch(...) 不是函数

量角器测试随机失败并出现错误“失败:ECONNREFUSED 连接 ECONNREFUSED 127.0.0.1:32572”

编译失败并出现错误