karma-typescript: import JS file with Async keyword
Asked Answered
D

1

8

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 ?

Doom answered 21/11, 2018 at 13:13 Comment(1)
We ran into a similar issues. Do you know a way to fix it?Karat
K
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.

Karat answered 13/9, 2019 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.