#node.js #express #ecmascript-6 #mocha.js #babeljs
#node.js #экспресс #ecmascript-6 #mocha.js #babeljs
Вопрос:
В настоящее время существует проблема с импортом es6 в тестовых примерах Mocha. Я пытался добавить babel и последние пресеты, но это ничего не решает. Я попробовал несколько доступных решений, но не смог решить свою проблему.
Это мой package.json `
{
"name": "ep-backend",
"main": "app.js",
"scripts": {
"tsc": "tsc",
"dev": "ts-node-dev --respawn --transpileOnly $NODE_DEBUG_OPTION ./app/app.ts",
"prod": "tsc amp;amp; node ./build/app.js",
"test": "NODE_DEBUG="fs" DEBUG="*" mocha --require @babel/register $(find test -name '*.ts')"
},
"license": "ISC",
"dependencies": {
"@types/express": "^4.16.1",
"axios": "^0.18.0",
"chai": "^4.2.0",
"chai-http": "^4.2.1",
"dotenv": "^7.0.0",
"express": "^4.16.4",
"http-status-codes": "^1.3.1",
"mocha": "^6.0.2",
"nock": "^10.0.6",
"ts-node-dev": "^1.0.0-pre.32",
"typescript": "^3.3.3333"
},
"devDependencies": {
"@babel/core": "^7.4.0",
"@babel/register": "^7.4.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-latest": "^6.24.1"
},
"babel": {
"presets": [
"latest"
]
}
}
`
это структура моего проекта
ep-backend -> app -> services -> service.ts
ep-backend -> package.json
ep-backend -> tsconfig.json
ep-backend -> .babelrc
ep-backend -> test -> services -> service.test.ts
Содержимое .babelrc является
`{ "presets": ["latest"]}`
мой tsconfig.json `
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./build",
"esModuleInterop": true
}
}
`
В моем service.test.ts я импортирую service.ts как
import { getAllTests, createTest, getTestByID } from "../../app/services/replicar-service";
Мой журнал ошибок консоли
NODE_DEBUG="fs" DEBUG="*" mocha --require @babel/register $(find test -name '*.ts')
mocha:cli:options no config found in /home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/package.json 0ms
mocha:cli:options No mocha.opts found at ./test/mocha.opts 3ms
mocha:cli:mocha loaded opts { _: [ 'test/controllers/test-controllers.test.ts' ],
require: [ '@babel/register' ],
r: [ '@babel/register' ],
config: false,
package: false,
opts: false,
diff: true,
extension: [ 'js' ],
'watch-extensions': [ 'js' ],
reporter: 'spec',
R: 'spec',
slow: 75,
s: 75,
timeout: 2000,
t: 2000,
timeouts: 2000,
ui: 'bdd',
u: 'bdd' } 0ms
mocha:cli:mocha final node args {} 2ms
mocha:cli:mocha exec /home/haseeb/.nvm/versions/node/v10.6.0/bin/node w/ args: [ '/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/bin/_mocha',
'test/controllers/test-controllers.test.ts',
'--require',
'@babel/register',
'--no-config',
'--no-package',
'--no-opts',
'--diff',
'--extension',
'js',
'--reporter',
'spec',
'--slow',
'75',
'--timeout',
'2000',
'--ui',
'bdd' ] 3ms
mocha:cli:cli entered main with raw args [ 'test/controllers/test-controllers.test.ts',
'--require',
'@babel/register',
'--no-config',
'--no-package',
'--no-opts',
'--diff',
'--extension',
'js',
'--reporter',
'spec',
'--slow',
'75',
'--timeout',
'2000',
'--ui',
'bdd' ] 0ms
mocha:cli:run:helpers loaded require "@babel/register" 0ms
mocha:cli:run post-yargs config { _: [],
diff: true,
require: [ '@babel/register' ],
r: [ '@babel/register' ],
config: false,
package: false,
opts: false,
extension: [ 'js' ],
'watch-extensions': [ 'js' ],
watchExtensions: [ 'js' ],
reporter: 'spec',
R: 'spec',
slow: 75,
s: 75,
timeout: 2000,
t: 2000,
timeouts: 2000,
ui: 'bdd',
u: 'bdd',
spec: [ 'test/controllers/test-controllers.test.ts' ],
'$0': 'mocha' } 0ms
mocha:suite bail undefined 0ms
mocha:suite slow 75 0ms
mocha:suite timeout 2000 0ms
mocha:cli:run:helpers files (in order): [ '/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/test/controllers/test-controllers.test.ts' ] 3ms
mocha:cli:run running tests with files [ '/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/test/controllers/test-controllers.test.ts' ] 3ms
babel:config:config-chain Ignored '/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/interfaces/common.js' because it matched one of [ /^/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend(?:/.*)?/node_modules//i ] from '/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend' 0ms
/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/test/controllers/test-controllers.test.ts:4
import { getAllTests, createTest, getTestByID } from "../../app/services/replicar-service";
^
SyntaxError: Unexpected token {
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.newLoader [as .js] (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at /home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/mocha.js:327:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/mocha.js:324:14)
at Mocha.run (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/mocha.js:801:10)
at Object.exports.singleRun (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/cli/run-helpers.js:207:16)
at exports.runMocha (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/cli/run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/cli/run.js:296:3)
at Object.runCommand (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/yargs/lib/command.js:238:44)
at Object.parseArgs [as _parseArgs] (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/yargs/yargs.js:1072:28)
at Object.parse (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/yargs/yargs.js:566:25)
at Object.exports.main (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/lib/cli/cli.js:62:6)
at Object.<anonymous> (/home/haseeb/workplace/ai.aai.research/EP-2.0/ep-backend/node_modules/mocha/bin/_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:236:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)
npm ERR! Test failed. See above for more details.
Ответ №1:
Из документации @babel/register
, .es6, .es, .jsx, .mjs, and .js are the only extensions will be processed by default
. Вы можете переопределить это, потребовав модуль в вашем тестовом файле. Проверьте следующий URL, чтобы увидеть доступные параметры.
https://babeljs.io/docs/en/babel-register#specifying-options