ava: SyntaxError: Unexpected token import

前端 未结 3 1910
醉酒成梦
醉酒成梦 2021-01-01 10:16

So ava comes with build-in ES2015 support, which works fine for my actual test files. However, when I do

import {newUser, createUser, login} from \'./helpers         


        
相关标签:
3条回答
  • 2021-01-01 10:43

    AVA only transpile the test files. Not test dependencies so you will need to setup babel in your project (I suppose you did it because you're using ES6 anyway).

    Then in AVA's setting, add this :

    "ava": {
      ...
      "babel": "inherit"
    }
    

    It means that use your project babel setting to transpile the test dependencies. See more information in AVA docs: https://github.com/sindresorhus/ava/blob/master/docs/recipes/babelrc.md

    0 讨论(0)
  • 2021-01-01 10:45

    Unfortunately standard solution didn't work for my case. Here is my solution which worked for ava + quasar + vue project

    .babelrc

    {
      "presets": [
        "es2017",
        "@ava/stage-4",
        "stage-3"
      ],
      "plugins": [
        "transform-runtime"
      ]
    }
    

    package.json

    "ava": {
      "require": [
        "babel-register"
      ],
      "babel": "inherit"
    },
    "scripts": {
      "ava": "NODE_ENV=test ava",
      "test": "ava",
      "test:watch": "ava --watch --verbose"
    }
    

    install modules

    yarn add babel-register babel-preset-es2017 @ava/babel-preset-stage-4 babel-plugin-transform-runtime babel-preset-stage-3 --dev
    
    0 讨论(0)
  • 2021-01-01 10:46

    Using rweng, my solution came out a bit simpler.

    1. .babelrc
    {
      "presets": [
        "es2015"
      ],
      "plugins": [
        "transform-runtime"
      ]
    }
    
    1. package.json:
    "ava": {
      "require": ["babel-register"],
      "babel": "inherit"
    }
    
    0 讨论(0)
提交回复
热议问题