简体   繁体   English

vuex-typex` npm软件包的`dist / index.js`的编译以及与Jest的兼容性

[英]Compilation of `vuex-typex` npm package's `dist/index.js` and compatibility with Jest

This question was first posed as an open issue on GitHub . 这个问题最初是在GitHub上公开提出的。

I've run into syntax errors when running Jest tests with Vue.js, Vuex, TypeScript, and vuex-typex . 使用Vue.js,Vuex,TypeScript和vuex-typex运行Jest测试时,我遇到语法错误。 I'm fairly new to working with Vue.js, TypeScript, and Jest. 我刚接触Vue.js,TypeScript和Jest。

It may be useful to mention that the tests in question are written directly against a Vuex store object ( .ts ), not a Vue component ( .vue ). 值得一提的是,有问题的测试是直接针对Vuex存储对象( .ts )而不是针对Vue组件( .vue.vue

Here's the error output I get: 这是我得到的错误输出:

Test suite failed to run

.../node_modules/vuex-typex/dist/index.js:19
import { Store } from "vuex";
^^^^^^

SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
  at Object.<anonymous> (src/store/main.ts:9:18)
  ...

I'm using the following versions of these tools: 我正在使用这些工具的以下版本:

"vue": "^2.5.10"
"vuex": "^3.0.0"
"typescript": "^2.6.2"
"vuex-typex": "^3.0.1"
"jest": "^21.2.1"
"jest-vue-preprocessor": "^1.3.1"
"@types/jest": "^21.1.8"
"ts-jest": "^21.2.4"
"typescript-babel-jest": "^1.0.5"

And here are the configuration settings that process test code: 这是处理测试代码的配置设置:

.babelrc

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": ["transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"]
    }
  }
}

package.json

...
"jest": {
    "moduleFileExtensions": [
      "js",
      "ts",
      "vue"
    ],
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1",
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/mocks/file.ts",
      "\\.(css|less)$": "<rootDir>/mocks/style.ts"
    },
    "transform": {
      ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue-preprocessor",
      ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js",
      ".*": "<rootDir>/node_modules/babel-jest"
    },
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!vuex-i18n).+\\.js$"
    ],
    "testMatch": [
      "**/?(*.)spec.ts"
    ],
    "verbose": true
  }
...

tsconfig.json

...
"jest": {
    "globals": {
      "ts-jest": {
        "compilerOptions": {
          "module": "commonjs"
        },
        "include": [
          "./test/**/*"
        ]
      }
    }
  }
...

I thought this might be some kind of compilation issue in the distribution code published to npm, so I tried manually processing the dist/index.js file with Babel to the targets I'm using: 我认为这可能是发布到npm的分发代码中的某种编译问题,所以我尝试使用Babel手动处理dist/index.js文件到我正在使用的目标:

es2015
stage-2
"browserslist": [ "> 1%", "last 2 versions", "not ie <= 8" ]

This appeared to resolve the issue for me, but I'm only partially confident in my diagnosis. 这似乎为我解决了这个问题,但是我对诊断只感到部分自信。 I don't see any other issues (open or closed) on this repository that would suggest others have run into this problem. 我在此存储库中没有看到任何其他问题(打开或关闭),这表明其他人已经遇到了这个问题。 I don't want to apply a misguided workaround or obscure an underlying implementation problem in my code. 我不想在我的代码中应用错误的解决方法或掩盖潜在的实现问题。

I'm hoping that this is just a simple oversight or misunderstanding on my part. 我希望这只是我的简单疏忽或误解。 Can you offer any insight? 您能提供任何见解吗? Have you, or others you know, used this particular set of tools together with Jest tests? 您或您认识的其他人是否已将这套特定的工具与Jest测试一起使用? It appears that the tests for vuex-typex were written with Mocha and Chai, but I imagine that others must be using this with Jest, too. 看来vuex-typex的测试是用Mocha和Chai编写的,但我想其他人也必须将其与Jest一起使用。

I ran across the same thing and struggled to find a fix. 我遇到了同样的事情,努力寻找解决方法。

Fortunately, someone smarter than I figured it out and submitted a pull request: 幸运的是,有一个比我更聪明的人提出了请求请求:

https://github.com/mrcrowl/vuex-typex/pull/13 https://github.com/mrcrowl/vuex-typex/pull/13

Utilize that until it is merged and you should be good to go. 利用它直到合并为止,您应该一切顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM