gpt4 book ai didi

node.js - TestsNodejs - mocha 和 babel 中的编译器弃用

转载 作者:太空宇宙 更新时间:2023-11-04 00:09:16 25 4
gpt4 key购买 nike

我正在通过集成测试(Babel、chai、mocha)在 Node 上构建 API Rest

我变了

 --compilers js:babel-core/register 

对于

--require babel-core/register

按照文档的建议: https://github.com/mochajs/mocha/wiki/compilers-deprecation

但是当我在 mocha.opts 文件中进行此更改时,出现错误:

C:\Users\Ranulfo\Desktop\noderest>npm run test-integration

> noderest@1.0.0 test-integration C:\Users\Ranulfo\Desktop\noderest
> mocha --opts test/integration/mocha.opts test/integration/*.js

C:\Users\Ranulfo\Desktop\noderest\test\integration\helpers.js:1
(function (exports, require, module, __filename, __dirname) { import supertest f
rom 'supertest';
^^^^^^

SyntaxError: Unexpected token import

有关代码的更多详细信息:

mocha.opts(测试/集成/mocha):

--require test/integration/helpers.js
--reporter spec
--require babel-core/register
--slow 5000

helpers.js(测试/集成/helpers.js)

import supertest from 'supertest';
import chai from 'chai';
import app from '../../app';

global.app = app;
global.request = supertest(app);
global.expect = chai.expect;

package.json

{
"name": "noderest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "babel-node ./index.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test-integration": "mocha --opts test/integration/mocha.opts test/integration/*.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-node6": "^11.0.0",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.26.0",
"chai": "^4.1.2",
"mocha": "^5.2.0",
"supertest": "^3.1.0"
},
"dependencies": {
"express": "^4.16.3",
"sequelize": "^4.37.10",
"sqlite3": "^4.0.0"
}
}

.babelrc

{
"presets": ["env"]
}

最佳答案

对于那些因为使用编译器的弃用通知错误而来到这里并且对 babel 没有其他问题的人。

  • 只需安装依赖项@babel/register即可。

  • 并将测试脚本从 mocha --compilers js:babel-core/register 更改为 mocha --require @babel/register

这应该可以解决您关于 Mocha 错误通知的错误。

关于node.js - TestsNodejs - mocha 和 babel 中的编译器弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50653904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com