Reputation: 95
I'm doing a project for the university in TypeScript and trying to debug on vscode while running a test using Mocha. I'm using the following launch .json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/*.test.ts"
],
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": [
"<node_internals>/**"
]
},
]
}
When I run this configuration I get the error:
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1063:16)
at Module._compile (internal/modules/cjs/loader.js:1111:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\mocha.js:334:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\mocha.js:331:14)
at Mocha.run (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\mocha.js:809:10)
at Object.exports.singleRun (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\run-helpers.js:108:16)
at exports.runMocha (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\run-helpers.js:142:13)
at Object.exports.handler (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\run.js:292:3)
at Object.runCommand (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\yargs\yargs.js:1096:28)
at Object.parse (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\yargs\yargs.js:575:25)
at Object.exports.main (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\cli.js:68:6)
at Object.<anonymous> (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:1144:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
The test i'm trying to run first is called q2-tests.test.ts and has these imports:
import fs from "fs";
import { expect } from 'chai';
import { evalL3program } from '../imp/L3-eval';
import { Value } from "../imp/L3-value";
import { Result, bind, makeOk } from "../imp/result";
import { parseL3 } from "../imp/L3-ast";
The imports work just fine when running without debug, and when I run npm test
there is no problem.
This seems to be the file causing the problem, but it seems to not be specific to it but generally a problem with running the imports. I tried changing the imports on the file and it just throws this error for the first import it gets to.
I'm very new to both of those tools (TypeScript\JavaScript and Mocha) and I'm probably out of my depth, so the info I provided might be lacking, or might be too much, I just don't know where to look for the problem. If you need anything more just tell me. Also, because I'm new, reference to the correct place to look for the answer and understand the problem better would be appreciated, as well as an answer, I'm just looking to learn from this.
Upvotes: 0
Views: 2081
Reputation: 95
Turns out I was using the wrong launch configuration. This page was a lot of help: https://github.com/microsoft/vscode-recipes/tree/master/debugging-mocha-tests. The right configurations were:
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"--require",
"ts-node/register",
"test/**/*.ts",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
Upvotes: 2