karma-typescript: import JS file with Async keyword

664 views Asked by At

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

There are 1 answers

0
andreas On

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.