我正在尝试使用Mocha(和Chai)来测试我的Node.js TypeScript代码。
当我使用WebStorm的Mocha运行配置时,它运行以下命令并引发错误
/usr/local/bin/node /Users/george.pai/Workspace/fd/fd-lambdas/node_modules/mocha/bin/_mocha --require ts-node/register --ui bdd --reporter "/Users/george.pai/Library/Application Support/JetBrains/Toolbox/apps/WebStorm/ch-0/173.3727.108/WebStorm.app/Contents/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js" /Users/george.pai/Workspace/fd/fd-lambdas/src/test/slack.handler.spec.ts --grep "slack\.handler "
但是,如果我从命令行运行完全相同的命令,它可以正常工作。
我的package.json依赖项
"dependencies": {
"aws-lambda": "0.1.2",
"json2csv": "^3.11.5",
"request": "^2.83.0",
"request-promise-native": "^1.0.5",
"uuid": "^3.1.0"
},
"devDependencies": {
"@types/aws-lambda": "0.0.19",
"@types/chai": "^4.0.7",
"@types/mocha": "^2.2.44",
"@types/node": "^8.0.53",
"@types/request": "^2.0.8",
"@types/request-promise-native": "^1.0.10",
"@types/uuid": "^3.4.3",
"chai": "^4.1.2",
"mocha": "^4.0.1",
"mocha-typescript": "^1.1.12",
"serverless-offline": "^3.16.0",
"serverless-webpack": "^3.0.0",
"ts-loader": "^2.3.7",
"ts-node": "^3.3.0",
"tslint": "^5.8.0",
"tslint-config-standard": "^7.0.0",
"typescript": "^2.5.2",
"webpack": "^3.6.0"
}
我的tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": [
"es6",
"dom"
],
"moduleResolution": "node",
"rootDir": "./src",
"sourceMap": true,
"inlineSources": true,
"allowJs": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"forceConsistentCasingInFileNames": true
},
"exclude": [
"node_modules",
"build",
"webpack"
],
"types": [
"aws-sdk",
"chai",
"mocha",
"node",
"request",
"request-promise-native",
"typePatches",
"uuid"
]
}
以下是错误的完整输出
/usr/local/bin/node /Users/george.pai/Workspace/fd/fd-lambdas/node_modules/mocha/bin/_mocha --require ts-node/register --ui bdd --reporter "/Users/george.pai/Library/Application Support/JetBrains/Toolbox/apps/WebStorm/ch-0/173.3727.108/WebStorm.app/Contents/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js" /Users/george.pai/Workspace/fd/fd-lambdas/src/test/slack.handler.spec.ts --grep "slack\.handler "
/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:307
throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
^
TSError: ⨯ Unable to compile TypeScript
fd-lambdas/src/main/node/slack/slack.handler.ts (57,18): 'Promise' only refers to a type, but is being used as a value here. (2693)
fd-lambdas/src/main/node/slack/slack.handler.ts (69,12): 'Promise' only refers to a type, but is being used as a value here. (2693)
fd-lambdas/src/main/node/slack/slack.handler.ts (199,14): 'Promise' only refers to a type, but is being used as a value here. (2693)
fd-lambdas/src/main/node/slack/slack.handler.ts (202,14): 'Promise' only refers to a type, but is being used as a value here. (2693)
fd-lambdas/src/main/node/slack/slack.handler.ts (230,16): 'Promise' only refers to a type, but is being used as a value here. (2693)
at getOutput (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:307:15)
at /Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:336:16
at Object.compile (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:498:11)
at Module.m._compile (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:392:43)
at Module._extensions..js (module.js:623:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:395:12)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Module.require (module.js:556:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/george.pai/Workspace/fd/fd-lambdas/src/test/slack.handler.spec.ts:4:1)
at Module._compile (module.js:612:30)
at Module.m._compile (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:623:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/ts-node/src/index.ts:395:12)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Module.require (module.js:556:17)
at require (internal/module.js:11:18)
at /Users/george.pai/Workspace/fd/fd-lambdas/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/mocha/lib/mocha.js:514:10)
at Object.<anonymous> (/Users/george.pai/Workspace/fd/fd-lambdas/node_modules/mocha/bin/_mocha:484:18)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Function.Module.runMain (module.js:653:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
Process finished with exit code 1
答案 0 :(得分:1)
原来混淆可能阻碍了找到解决方案。 Mocha运行配置/Users/george.pai/Workspace/fd
的工作目录是项目的根目录,但不是我的节点项目的根/Users/george.pai/Workspace/fd/fd-lambdas
将工作目录更改为正确的目录解决了这个问题。我的终端已经在正确的目录/Users/george.pai/Workspace/fd/fd-lambdas
中,所以这就是它工作的原因。