Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

karma-typescript: import JS file with Async keyword

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 ?

like image 262
Menencia Avatar asked Nov 21 '18 13:11

Menencia


1 Answers

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.

like image 55
andreas Avatar answered Oct 17 '22 13:10

andreas