Using the latest version of Mocha and ts-node I was getting an Unexpected token import issue. Using the below settings with ts-mocha worked for me:
tsconfig.json
{
"files": [
"src/main.ts"
],
"compilerOptions": {
"noImplicitAny": true,
"target": "es2015",
"types": ["mocha"],
"module": "commonjs"
}
}
package.json
"scripts": {
"mocha": "ts-mocha -p library/tsconfig.json library/test/**/*.ts"
},
launch.json
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"runtimeArgs": [
"${workspaceFolder}/node_modules/ts-mocha/bin/ts-mocha",
"--timeout", "999999",
"-p",
"${workspaceFolder}/library/tsconfig.json",
"${workspaceFolder}/library/test/**/*.ts"
],
"internalConsoleOptions": "openOnSessionStart"
}
and gulp.js just incase you want to use gulp too
const gulp = require('gulp');
const ts = require('gulp-typescript');
const mocha = require('gulp-mocha');
const tsProject = ts.createProject('tsconfig.json');
gulp.task('build', () => tsProject.src()
.pipe(tsProject())
.js.pipe(gulp.dest('dist')));
gulp.task('test', () => gulp.src('test/*.spec.ts')
.pipe(mocha({
reporter: 'nyan',
require: ['ts-node/register'],
})));
/* single command to hook into VS Code */
gulp.task('default', gulp.series('build', 'test'));