Ts-node: 'SyntaxError: Unexpected token import' with 'allowJs' option and Mocha tests

Created on 16 Nov 2016  Â·  6Comments  Â·  Source: TypeStrong/ts-node

I know that similar error has been reported couple of times before.
I've setup a TypeScript project with allowJs option set to true. The Mocha is configured to use multiple file extensions compilers (js:, ts:) with ts-node. The only problem with these types of projects is that Mocha always reports error when js extension is used for test source:

import { PI } from '../src/index';
^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:528:28)
...

Here is example content:
https://github.com/peterblazejewicz/typescript-playground/tree/master/tests

If I change extension of test file from .js to .ts, everything seems to be OK:

➜  tests git:(master) npm test

> [email protected] test /Users/piotrblazejewicz/git/typescript-playground/tests
> mocha --opts mocha.opts


  [▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬]

  1 passing (11ms)

The tsc works fine with allowJs option.

Thanks!

"ts-node": "^1.7.0",

bug

Most helpful comment

Fixed this with https://github.com/TypeStrong/ts-node/commit/9caa1c57d80de2f6b70def80de6e7f1c99f89154. Just went for a straight reversal. I'll avoid treating entry points differently going forward to avoid this issue. I haven't had any push with gulp (the original issue) but it's easy to work around - just cd first instead of using the --cwd flag.

All 6 comments

Just ran into this as well. Looking through the code, it seems to be because

  1. require('ts-node/register') enables lazy mode, whereas require('ts-node').register() does not (this should probably be mentioned in the README, which makes the commands seem equivalent).
  2. tsconfig.json is not consulted until TypeScript is initialized, which, in lazy mode, doesn't happen until the first .ts file is required somewhere

The end result is that if your first/root test file is .ts, allowJs works from then on, but if your first file is .js, TypeScript won't be called on it even with allowJs.

@peterblazejewicz As a workaround, you could create a bootstrap.js file with

require('ts-node').register();

and pass that to mocha instead of ts-node/register.

Seems like the equivalent to https://github.com/TypeStrong/ts-node/pull/238, but I haven't got a good solution yet. Seems reasonable that this sort of addition would bite me like it has, so I'll look at reverting it once I have time to work on it.

@cletusw Thanks! It worked!
@blakeembrey appreciated!

Fixed this with https://github.com/TypeStrong/ts-node/commit/9caa1c57d80de2f6b70def80de6e7f1c99f89154. Just went for a straight reversal. I'll avoid treating entry points differently going forward to avoid this issue. I haven't had any push with gulp (the original issue) but it's easy to work around - just cd first instead of using the --cwd flag.

Thanks! (

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joshua-tj picture joshua-tj  Â·  3Comments

sanex3339 picture sanex3339  Â·  4Comments

watzon picture watzon  Â·  3Comments

huan picture huan  Â·  3Comments

Borewit picture Borewit  Â·  3Comments