【问题标题】:WebStorm Mocha run configuration throws error, command line does notWebStorm Mocha 运行配置抛出错误,命令行没有
【发布时间】:2017-12-04 19:16:30
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 错误来自 tsnode。不知道为什么它不使用你的 tsconfig.json,但它看起来不像是 IDE 问题

标签: typescript mocha.js webstorm


【解决方案1】:

事实证明,混淆可能阻碍了找到解决方案。 Mocha 运行配置/Users/george.pai/Workspace/fd 的工作目录是项目的根目录,但不是我的节点项目的根目录/Users/george.pai/Workspace/fd/fd-lambdas

将工作目录更改为正确的目录解决了这个问题。我的终端已经在正确的目录 /Users/george.pai/Workspace/fd/fd-lambdas 中,这就是它起作用的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-09
    • 2021-09-23
    • 1970-01-01
    • 2019-07-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多