Reputation: 101
I am trying to use Mocha (and Chai) to test my Node.js TypeScript code.
When I use WebStorm's Mocha Run Configuration it runs the following command and throws an error
/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 "
However if I run the exact same command from the command line it works fine.
Here is my run configuration (some directory names obfuscated for privacy)
My package.json dependencies
"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"
}
My 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"
]
}
Here is the full output of the error
/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
Upvotes: 2
Views: 483
Reputation: 101
Turns out the obfuscation probably hindered finding the solution. The working directory for the Mocha Run Configuration /Users/george.pai/Workspace/fd
was the root of the project, but not the root of my node project /Users/george.pai/Workspace/fd/fd-lambdas
Changing the working directory to the proper directory solved this. My terminal was already in the proper directory /Users/george.pai/Workspace/fd/fd-lambdas
so that is why it worked.
Upvotes: 2