无法在Webpacker中的vuex突变测试中导入vue.esm.js

sde*_*lis 5 tdd ruby-on-rails vue.js vuex webpacker

我正在通过这种方法使用Webpacker (要求我导入vue.esm.js)。我想按照测试vuex docs所述测试我的vuex突变。它在我使用import Vue from 'vue' 时起作用,但在我使用时不起作用import Vue from 'vue/dist/vue.esm'。但是,如果我不在商店中使用vue.esm,则我的Webpacker Vue应用程序将中断。

这是我的商店:

// store.js
import Vue from 'vue/dist/vue.esm' // changing this to import from 'vue' works
import Vuex from 'vuex'
Vue.use(Vuex)

const state = {
  count: 0
}

// export `mutations` as a named export
export const mutations = {
  increment: state => state.count++
}

export default new Vuex.Store({
  state,
  mutations
})
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

import { mutations } from './store'

// destructure assign `mutations`
const { increment } = mutations

describe('mutations', () => {
  it('INCREMENT', () => {
    // mock state
    const state = { count: 0 }
    // apply mutation
    increment(state)
    // assert result
    expect(state.count).toBe(1)
  })
})
Run Code Online (Sandbox Code Playgroud)

以上测试输出:

 FAIL  app/javascript/test/unit/specs/mutations.spec.js
  ? Test suite failed to run

    myproject/node_modules/vue/dist/vue.esm.js:10671
    export default Vue$3;
    ^^^^^^

    SyntaxError: Unexpected token export
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

ell*_*ynz 4

不确定你是否解决了这个问题,但我遇到了同样的问题。对我有用的是使用“vue”的别名。与这里相同: https: //github.com/rails/webpacker/blob/master/docs/webpack.md#configuration

// custom.js
const vueFile = process.env.NODE_ENV === 'production' ? 'vue/dist/vue.min.js' : 'vue/dist/vue.js';
module.exports = {
  resolve: {
    alias: {
      vue: vueFile,
      vue_resource: 'vue-resource/dist/vue-resource',
    },
  },
};

// environment.js
const { environment } = require('@rails/webpacker');
const customConfig = require('./custom');
const vue = require('./loaders/vue');

environment.config.merge(customConfig);
environment.loaders.append('vue', vue);

module.exports = environment;
Run Code Online (Sandbox Code Playgroud)

然后更新我的商店和应用程序 JS 中的导入 ( import Vue from 'vue';),我们就解决了!