Reputation: 5204
I've set up my mocha test command like so
mocha --require test/ts-node-hooks.js test/**/*.spec.ts
And my ts-node-hooks.js
file like so
const path = require('path');
require("ts-node").register({
project: path.resolve(__dirname, 'tsconfig.json'),
});
My tsconfig.json
file in the /test
directory is set to use ESNEXT
as the javascript target
{
"compilerOptions": {
/* Basic Options */
"target": "ESNEXT", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"types": ["@3846masa/axios-cookiejar-support"] /* Type declaration files to be included in compilation. */
}
}
But I keep getting this error
$ mocha --require test/ts-node-hooks.js test/**/*.spec.ts
/src/Call.ts:41
return (async () => this._callClass = await this.getCallValue('callclass'))();
^
SyntaxError: Unexpected token (
tsc version 2.6.2
has no problem compiling the code.
Upvotes: 2
Views: 1079
Reputation: 5204
In my case it was a simple issue of an existing globally installed ts-node
that I had forgotten about. Uninstalling that fixed the problem.
Upvotes: 0
Reputation: 489
Use mocha -compilers <path to ts-node>
eg. node_modules\ts-node\register test/test-node-hooks.ts
If the above does not work, recompile the .ts file with tsc test-node-hooks.ts and run the mocha command again.
Upvotes: 2