ES6导入在此文件中有效,但在导入相关文件(例如我的Mongoose用户模型)时会生成意外的令牌import
错误。
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')
}
})
})
答案 0 :(得分:0)
不要认为nodejs支持import
。您必须使用require
。
const mongoose = require('mongoose');
答案 1 :(得分:0)
对我有用的答案是Serge Seletskyy here建议的以下内容。 ES2017功能需要es2017
预设,例如async await。
<强> .babelrc 强>
{
"presets": [
"es2017",
"@ava/stage-4",
"stage-3"
],
"plugins": [
"transform-runtime"
]
}
<强>的package.json 强>
"ava": {
"require": [
"babel-register"
],
"babel": "inherit"
}
安装模块
yarn add babel-register babel-preset-es2017 @ava/babel-preset-stage-4 babel-plugin-transform-runtime babel-preset-stage-3 --dev
现在应该运行./node_modules/.bin/ava --verbose
答案 2 :(得分:0)
通过.babelrc
加载非测试文件,这会应用您的{
"presets": [
["es2015", { "modules": false }]
],
"plugins": [
"transform-object-rest-spread",
"transform-async-to-generator",
"transform-export-extensions"
],
"env": {
"test": {
"presets": [
["es2015", { "modules": true }]
]
}
}
}
。但是,您已禁用模块转换。我在另一条评论中看到你正在使用Webpack。尝试为Babel添加环境配置,以恢复模块转换。从头到尾:
template <class Element>
class Node {
public:
Node(const Node& orig);
Node(Element e);
const Element& getElement();
Node<Element>* getNext() const;
void setNext(Node<Element> *next);
Node<Element>* getPrevious() const;
void setPrevious(Node<Element> *prev);
~Node();
private:
Node <Element>* next;
Node <Element>* prev;
Element element;
};
template <class Element>
Node<Element>::Node(Element e){
this->element = e;
this->next = nullptr;
this->prev= nullptr;
}
template <class Element>
Node<Element>::~Node(){
}
template <class Element>
const Element& Node<Element>::getElement(){
return this->element;
}
template <class Element>
Node<Element>* Node<Element>::getNext() const{
return this->next;
}
template <class Element>
Node<Element>* Node<Element>::getPrevious() const{
return this->prev;
}
template <class Element>
void Node<Element>::setNext(Node<Element>* next){
this->next = next;
}
template <class Element>
void Node<Element>::setPrevious(Node<Element>* prev){
this->prev = prev;
}
这应该适用于最新的AVA版本。
答案 3 :(得分:0)
我的Ava版本为1.0.0-beta.4
,以下是适用于我的解决方案:
将以下内容添加到package.json
"ava": {
"require": [
"@babel/register"
]
}
然后npm install --save-dev @babel/register
,再次运行测试。