karma-typescript: import JS file with Async keyword

不羁岁月 提交于 2020-01-14 07:48:11

问题


I'm using karma-typescript, with this karma config file :

karmaTypescriptConfig: {
  compilerOptions: {
    target: "es5",
    lib: ["dom", "es2015", "es2017"]
  },
  bundlerOptions: {
    transforms: [require("karma-typescript-es6-transform")()]
  }
},

In my spec files, I have this code :

import {} from './local/lib.js'

In my lib.js, I have this code :

async function() {}

When executing my tests with npm test, I have this error :

ERROR [source-reader.karma-typescript] Error parsing code: Unexpected token (X:Y) in /local/lib.js

If I remove the async keyword, everything is alright.

How can I edit my karma config file to fix the error ?


回答1:


According to an issue in the Github of the karma-typescript package (https://github.com/monounity/karma-typescript/issues/344), there is an undocumented flag which may help you test code which contains ES2017 code:

karmaTypescriptConfig: {
  compilerOptions: {
    target: "es5",
    lib: ["dom", "es2015", "es2017"]
  },
  bundlerOptions: {
    acornOptions: {
      ecmaVersion: 8,
    },
    transforms: [require("karma-typescript-es6-transform")()]
  }
},

This flag made appear our issues with the async keyword. However, there is still an issue with the spread syntax (...array) in our code, even using this flag. If anyone knows an answer how to also fix that, I will happily extend my answer.



来源:https://stackoverflow.com/questions/53412866/karma-typescript-import-js-file-with-async-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!