2

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.

2 Answers 2

2
  1. Use mocha -compilers <path to ts-node> eg. node_modules\ts-node\register test/test-node-hooks.ts

  2. If the above does not work, recompile the .ts file with tsc test-node-hooks.ts and run the mocha command again.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice, if the issue resurfaces and my answer doesn't fix it, I'll remember this.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.