代码之家  ›  专栏  ›  技术社区  ›  user762579

Vue Vue-SweetAlert2。测试错误:带jest的单元

  •  0
  • user762579  · 技术社区  · 7 年前

    根据 vue-sweetalert2 医生,在我的 main.js ,导入并使用插件:

    import VueSweetalert2 from "vue-sweetalert2";
    Vue.use(VueSweetalert2);
    

    在我的组件中, ContactForm.vue ,我可以使用:

    this.$swal(...)
    

    但是,当我测试:单元这个组件时,我需要添加导入和 Vue.use()

     import VueSweetalert2 from "vue-sweetalert2";
     Vue.use(VueSweetalert2);
    

    我得到一个错误:

    $ vue-cli-service test:unit ContactForm.spec.js
    

    失败测试/单元/联系人表单.spec.js 测试套件无法运行

    Jest encountered an unexpected token
    
    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
    
    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
    
    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
    
    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html
    
    Details:
    
    /Users/yves/Developments/WIP/VUE.JS-cli-3/3-chocha-home-content/chocha/node_modules/vue-sweetalert2/src/index.js:2
    import swal from 'sweetalert2/dist/sweetalert2.min.js';
    ^^^^^^
    
    SyntaxError: Unexpected token import
    

    怎么了?

    更新

    这个 vue-sweetalert2/src/index.js ,线路2故障线路,为:

    // @ts-check
    import swal from 'sweetalert2/dist/sweetalert2.min.js';
    

    这个包装器的开发人员添加了 index.d.ts

    import Vue, { PluginObject, PluginFunction } from 'vue';
    import * as swal from 'sweetalert2';
    ...
    

    但这似乎没有被考虑进去。

    1 回复  |  直到 7 年前
        1
  •  0
  •   user762579user762579    7 年前

    解决了的。。。

    我在package.json的jest配置中添加了transformignorepatterns

         "jest": {
        ....
        "transformIgnorePatterns": [
          "/node_modules/(?!vue-sweetalert2).+\\.js$"
        ],
        ....
      }
    
    推荐文章