1

The ES6 import works in this file, but generates an unexpected token import error when I'm importing relative files such as my Mongoose User model.

import mongoose from 'mongoose';
^^^^^^

SyntaxError: Unexpected token import

.babelrc

{
  "presets": [
    ["es2015", { "modules": false }]
  ],
  "plugins": [
    "transform-object-rest-spread",
    "transform-async-to-generator",
    "transform-export-extensions"
  ]
}

package.json

 "ava": {
    "require": [
      "babel-register"
    ]
  }

users.test.js

import test from 'ava'
import axios from 'axios'

import User from '../../models/user'
import { USER_REGISTRATION_ROUTES } from '../helpers/_test.properties.js'

test('user registration api works', async function (t) {
  const email = `test${Date.now()}@example.com`

  const userRegistrationData = {
    email,
    first_name: "john",
    last_name: "doe",
    password: "password"
  }

  await axios.post(USER_REGISTRATION_ROUTES, userRegistrationData)
    .then(response => {
      const data = response.data.data
      const user = data.user
      t.deepEqual(response.status, 200, 'Verify: response is successful')
      t.deepEqual(data.registered, true, 'Verify: user is registered')
      t.truthy(Object.keys(user).length > 0,
        'Verify: if registered, user object is returned')
      t.truthy(user.authentication_token,
        'Verify: token is generated on successful registration')
    })
    .catch((err) => {
      t.fail(`Cannot make requst to register user ${err}`)
    })

  User.remove({ email }, function (err) {
    if (err) {
      console.log('error')
    } else {
      console.log('success deleting test user')
    }
  })
})
Clement
  • 4,491
  • 4
  • 39
  • 69

4 Answers4

0

Don't think import is supported in nodejs yet. You have to use require.

const mongoose = require('mongoose');
barro32
  • 2,559
  • 23
  • 36
  • that's what my `.babelrc` file is for? it's used during the webpack build process (which works) :/ and import works in the test file itself. – Clement Apr 12 '18 at 15:24
0

The answer that worked for me was the following suggested by Serge Seletskyy here. The es2017 preset is required for ES2017 features such as async await.

.babelrc

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

package.json

"ava": {
  "require": [
    "babel-register"
  ],
  "babel": "inherit"
}

install modules

yarn add babel-register babel-preset-es2017 @ava/babel-preset-stage-4 babel-plugin-transform-runtime babel-preset-stage-3 --dev

Running ./node_modules/.bin/ava --verbose should now work

Clement
  • 4,491
  • 4
  • 39
  • 69
0

Non-test files are loaded through babel-register, which applies your .babelrc. However you've disabled module transpilation. I see in another comment that you're using Webpack. Try adding a environment config for Babel which restores module transpilation. From the top of my head:

{
  "presets": [
    ["es2015", { "modules": false }]
  ],
  "plugins": [
    "transform-object-rest-spread",
    "transform-async-to-generator",
    "transform-export-extensions"
  ],
  "env": {
    "test": {
      "presets": [
        ["es2015", { "modules": true }]
      ]
    }
  }
}

This should work with the latest AVA version.

Clement
  • 4,491
  • 4
  • 39
  • 69
Mark Wubben
  • 3,329
  • 1
  • 19
  • 16
0

My version of Ava is 1.0.0-beta.4, and below is the solution that worked for me:

Add the following to your package.json

"ava": {
  "require": [
    "@babel/register"
  ]
}

Then npm install --save-dev @babel/register, and run test again.

codingpop
  • 11
  • 2
  • 5