Search code examples
typescriptmocha.jsts-node

Cannot test typescript with mocha syntax error


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.


Solution

  • 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.