ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

'Unexpected token import' running mocha with TS 2.0

Open SethDavenport opened this issue 10 years ago • 34 comments

Hi, I was hoping you could help me troubleshoot something.

I've been using ts-node to run unit tests with mocha for a while:

mocha  --opts ./src/___tests___/mocha.opts

./src/**tests**/mocha.opts:

--compilers ts:ts-node/register
--compilers tsx:ts-node/register
**/*.spec.ts

This works great with typescript 1.8.10. However since upgrading to typescript 2.0.0 I'm getting this:

/Users/Seth/code/angular-redux/ng2-redux/examples/counter/node_modules/ng2-redux/src/___tests___/components/ng-redux.spec.ts:1
(function (exports, require, module, __filename, __dirname) { import 'reflect-metadata';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:513:28)
    at Module._extensions..js (module.js:550:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/Seth/code/angular-redux/ng2-redux/node_modules/ts-node/src/index.ts:304:16)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at /Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/lib/mocha.js:220:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/lib/mocha.js:217:14)
    at Mocha.run (/Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/lib/mocha.js:469:10)
    at Object.<anonymous> (/Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/bin/_mocha:404:18)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (bootstrap_node.js:352:7)
    at startup (bootstrap_node.js:144:9)
    at bootstrap_node.js:467:3

SethDavenport avatar Aug 20 '16 03:08 SethDavenport

I'm using node 6.3.0.

Here's my tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "rootDir": "src",
    "outDir": "lib/",
    "noImplicitAny": false,
    "declaration": true
  },
  "compileOnSave": false,
  "buildOnSave": false,
  "exclude": [
    "node_modules",
    "dist",
    "lib",
    "examples"
  ]
}

SethDavenport avatar Aug 20 '16 03:08 SethDavenport

This is using ts-node 1.2.3. tsc works fine.

Any ideas what I'm doing wrong?

SethDavenport avatar Aug 20 '16 03:08 SethDavenport

Here's the repo/branch I'm on: https://github.com/angular-redux/ng2-redux/tree/update/typescript-2

SethDavenport avatar Aug 20 '16 03:08 SethDavenport

ts-node does not compile files in node_modules.

blakeembrey avatar Aug 20 '16 03:08 blakeembrey

You can review a similar feature request in https://github.com/TypeStrong/ts-node/issues/155, but it's not high priority personally since it's unlikely you should be publishing raw TypeScript and running it. I would, of course, accept PRs that enable this behaviour behind a flag, but it's a terrible default for reasons I've mentioned in a few other issues.

blakeembrey avatar Aug 20 '16 03:08 blakeembrey

@blakeembrey thanks for the quick response.

I'm not actually trying to compile a dependency - just my own code. However for some reason stuff is getting pulled in from folders I've explicitly ignored in my tsconfig.json.

SethDavenport avatar Aug 20 '16 03:08 SethDavenport

Yep that was it.

Need to change **/*.spec.ts to src/**/spec.ts in my mocha.opts. Sorry for wasting your time :)

Not sure why this used to work...

SethDavenport avatar Aug 20 '16 03:08 SethDavenport

Great, good to know! It probably worked before 1.0 of ts-node (not sure if you updated both at the same time?). The previous behaviour just compiled every .ts file, but it's more important that this module is a good citizen and interacts with how the NPM ecosystem should be used. So in 1.0, I blacklisted node_modules and made it fallback to the old module compiler (in this case it falls back on .js) which is the behaviour you'd normally expect (your node module should be a box you control and shouldn't be affected by someone who's changed their ts-node config as that would result in weird issues) and hence it errors.

Edit: The easy way to catch this is by looking at the stack trace and you can see it was attempted to be loaded with the .js extension compiler.

blakeembrey avatar Aug 20 '16 03:08 blakeembrey

Yeah, I did update ts-node at the same time. That makes complete sense.

SethDavenport avatar Aug 20 '16 03:08 SethDavenport

in 1.0, I blacklisted node_modules and made it fallback to the old module compiler (in this case it falls back on .js) which is the behaviour you'd normally expect

This is also how tsc behaves. If you bundle a module depending on a TS module in node_modules, it won't be compiled. However, if you add that node_modules file to the files whitelist in tsconfig.json, I would expect ts-node to take that as precedence, which is exactly what tsc does.

OliverJAsh avatar Sep 20 '16 14:09 OliverJAsh

@OliverJAsh What you describe just isn't really possible. TypeScript follows the files or excludes, but ts-node does not as it wouldn't make a ton of sense to avoid compiling that server file that isn't technically in the tsconfig.json but was required. On top of which, you may also compile 100x more files than you do need during ts-node startup (think testing a single file using mocha, etc).

blakeembrey avatar Sep 20 '16 16:09 blakeembrey

Hello All,

i'm not sure if this is related but i'm getting the same issue "Unexpected token import" from an import which is a local ts file.

tsc works fine. And removing the following tsconfig.json will make everything work as expected:

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],

    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  }

}

vekexasia avatar Sep 30 '16 08:09 vekexasia

I am having the very same problem. Yet it does not seem to be related to anything that is mentioned here. This is the command that I execute:

> mocha --require ./test_setup.js --report lcovonly src/server/**/tests/*.ts --compilers ts:ts-node/register

/Users/tomi/Github/apps/Clara-Apollo/src/server/configs/tests/accounts_config_tests.ts:1
(function (exports, require, module, __filename, __dirname) { import { config } from 'apollo-module-authentication';

this is my tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es6",
    "module": "es6",
    "moduleResolution": "node"
  },
  "include": [
    "./src/**/*.ts",
    "./src/**/*.tsx"
  ],
  "exclude": [
    "node_modules",
    "src/**/tests/*.ts",
    "src/**/tests/*.tsx"
  ]
}

Does anything seem dodgy to you? I am stuck with this for several hours.

tomitrescak avatar Oct 04 '16 09:10 tomitrescak

Ah, ok. I need to actually change to es5 and commonjs if I want to use ts-node. Makes sense. This falls back to my problem that -P flag cannot be used with mocha.

tomitrescak avatar Oct 04 '16 09:10 tomitrescak

I'm able to compile using the es6 flag by setting module to "commonJS" in my tsconfig @tomitrescak. It appears as though the tsc is not properly compiling the imports otherwise. So I think it is actually a tsc bug.

jeffrey-l-turner avatar Jan 05 '17 20:01 jeffrey-l-turner

"commonjs" after all.

lygstate avatar Apr 01 '17 15:04 lygstate

Strangely for me, I fixed this with @SethDavenport's example though slightly differently.

I had the files to test glob in the test script like so

"test": "mocha --opts tests/mocha.opts tests/**/*.spec.ts"

Moving tests/**/*.spec.ts to mocha.opts magically fixed the issue. Strange!

--compilers ts:ts-node/register
tests/**/*.spec.ts

mattdell avatar Apr 18 '17 21:04 mattdell

If you quoted it in the test script, does that also fix your issue? I'm not sure what the problem would be there, but there's probably differences in the shell expansion used by Mocha vs your shell.

blakeembrey avatar Jul 22 '17 20:07 blakeembrey

@jeffrey-l-turner well, tsc "module": "commonjs" works but it doesn't allow using fs from node while I'm in tests, how else I'm supposed to load file-content?

import fs from 'fs';
import path from 'path'

akoidan avatar Jul 29 '17 22:07 akoidan

@Deathangel908 Both of those are incorrect. It should be import * as fs from 'fs' and import * as path from 'path'.

blakeembrey avatar Jul 30 '17 23:07 blakeembrey