当前,Mocha测试用例中存在es6导入问题。我尝试添加最新的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 && 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.
答案 0 :(得分:1)
来自@babel/register
,.es6, .es, .jsx, .mjs, and .js are the only extensions will be processed by default
的文档。您可以覆盖它,而这需要测试文件中的模块。检查以下网址以查看可用选项。
https://babeljs.io/docs/en/babel-register#specifying-options