【问题标题】:How to mock variables in action of vuex store with jest如何在 vuex 存储中用 jest 模拟变量
【发布时间】:2019-11-13 16:21:12
【问题描述】:

我有一个这样的js代码:

const Repository = axios.create( {
  baseURL: process.env.VUE_APP_API_HOST
} );

const state = { ... }
const getters = { ... }
const mutations = { ... }


const actions = {
  fetch: async ( { commit, getters }, { apiPath, id } ) => {

    const response = await Repository.get( apiPath );

    commit( 'setArticleList', { response.data, id } );
    commit( 'changeApiStatus', { status: 'success', id } );
  }
};

export const articleList = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};

我有一个这样的笑话代码:

import { store as store2 } from './store';

describe( 'store article_list test: ', () => {

  let store;
  beforeEach( () => {
    store = new Vuex.Store( store2 );
  } );

  describe( 'test actions', () => {
    test( 'get value correctly', async () => {
      const apiPath = 'api/v1/article_list.json';
      const id = 1;
        await store.dispatch( 'articleList/fetch', { apiPath, id } );
    } );
  } );


} );

这是商店代码:

store.js

import Vue from 'vue';
import Vuex from 'vuex';
import { articleList } from '@/store/modules/article_list';

Vue.use( Vuex );

export const store = {
  modules: {
    articleList
  }
};
export default new Vuex.Store( store );

await store.dispatch( 'articleList/fetch', { apiPath, id } );

目前此代码有一个Error: Error: connect ECONNREFUSED 127.0.0.1: 8080 错误

我想嘲讽 这部分 const response = await Repository.get( apiPath );

但我不知道如何在 vuex 存储中用 jest 模拟变量。

如果你知道如何模拟这部分,请告诉我。

感谢您阅读我的问题。

【问题讨论】:

    标签: vue.js jestjs vuex


    【解决方案1】:

    我正在使用moxios 模拟我的 axios 请求,效果很好,它允许您设置自己的响应数据。

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 2021-10-09
      • 2020-02-23
      • 2017-03-19
      • 2021-12-07
      • 2021-09-21
      • 2018-12-15
      • 2018-05-24
      • 2022-08-17
      相关资源
      最近更新 更多