programing

TypeError로 인해 Vuex+Jest 테스트가 실패함

prostudy 2022. 5. 25. 22:18
반응형

TypeError로 인해 Vuex+Jest 테스트가 실패함

응용 프로그램에서 Vuex 스토어 모듈을 테스트하려고 하는데 계속 반환됨TypeError: Cannot read property 'getters' of undefined무슨 수를 써서라도제발 도와주세요.암호는 다음과 같다.

store/index.ts:

import Vuex from 'vuex';

import auth from './auth';

Vue.use(Vuex);

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

store/auth.ts:

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

store/__built__/auth.ts:

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",

나는 문제를 발견했다.문제는 auth.ts가 axios 인스턴스를 가져오고 있었다는 것인데, 이것은 조롱되지 않았다.공리 호출이 필요한 작업 제거 또는 추가jest.mock('API_PATH', jest.fn); 테스트하여 문제를 해결하십시오.

참조URL: https://stackoverflow.com/questions/54299519/vuexjest-test-fails-with-typeerror

반응형