question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts-node using with mocha and tsconfig.json

See original GitHub issue

Hello and first thank you for creating this project. I bumped is a problem that seems to be discussed before but it is not clear to me what the solution is:

Here is the problem:

I was following yesterday this unit testing manual that describe one of the ways how to do unit testing with mocha and chai. It is all working as presented there but i found out that if i add my tsconfig.json to the project then the test stop working and fail with the error

It all is working if the file tsconfig.json is not in the main directory

npm run test


> external@1.0.0 test /home/tito/Projects/test
> ./node_modules/mocha/bin/mocha -r ts-node/register  ./tests/example.ts

SyntaxError: Unexpected token ] in JSON at position 475
    at JSON.parse (<anonymous>)
    at parse (/home/tito/Projects/test/node_modules/tsconfig/src/tsconfig.ts:195:15)
    at readFileSync (/home/tito/Projects/test/node_modules/tsconfig/src/tsconfig.ts:181:10)
    at Object.loadSync (/home/tito/Projects/test/node_modules/tsconfig/src/tsconfig.ts:151:18)
    at readConfig (/home/tito/Projects/test/node_modules/ts-node/src/index.ts:455:18)
    at Object.register (/home/tito/Projects/test/node_modules/ts-node/src/index.ts:205:18)
    at Object.<anonymous> (/home/tito/Projects/test/node_modules/ts-node/register/index.js:1:16)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at requires.forEach.mod (/home/tito/Projects/test/node_modules/mocha/bin/_mocha:467:3)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/tito/Projects/test/node_modules/mocha/bin/_mocha:466:10)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:617:3

if I rename or remove the tsconfig.json file my test a running with mocha ok and if i execute npm run test

then i get:


[  Hello function
    ✓ should return hello world
  1 passing (5ms)

and here is how the test script is being executed in package.json:

  "scripts": {
    "echo": "echo \"Error not test specified \" && exit 1",
    ......
    "server": "webpack-dev-server --watch",
    "test": "./node_modules/mocha/bin/mocha -r ts-node/register  ./tests/example.ts"
    .....
  },

I have tired adding exclude parameter to my tsconfig.json as mentioned in the issue number 4 here, but that does not seems to fix the problem i.e. here is how my tsconfig.json looks like:


{
    "compilerOptions": {
      "outDir": "./dist/",
      "sourceMap": true,
      "noImplicitAny": true,
      "module": "es6",
      "target": "es5",
      "jsx": "react",
      "allowJs": true,
      "removeComments": false

    }
    "exclude": [
        "node_modules",
        "tests",

    ]

  }

I have tired adding the dist folder to the exclude statements but that only changes the exception thrown i…e

> ./node_modules/mocha/bin/mocha -r ts-node/register  ./tests/example.ts

/home/tito/test/tests/example.ts:1
(function (exports, require, module, __filename, __dirname) { import { expect } from '../node_modules/chai';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at new Script (vm.js:51:7)
    at createScript (vm.js:138:10)
    at Object.runInThisContext (vm.js:199:10)
    at Module._compile (module.js:624:28)
    at Module.m._compile (/home/tito/test/node_modules/ts-node/src/index.ts:422:23)
    at Module._extensions..js (module.js:671:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/tito/test/node_modules/ts-node/src/index.ts:425:12)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at /home/tito/test/node_modules/mocha/lib/mocha.js:231:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/tito/test/node_modules/mocha/lib/mocha.js:228:14)
    at Mocha.run (/home/tito/test/node_modules/mocha/lib/mocha.js:536:10)
    at Object.<anonymous> (/home/tito/test/node_modules/mocha/bin/_mocha:582:18)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:617:3


but that does not seems to solve the problem as well.

In issue number 4 it was mentioned that i should use the “–noProject” directive but how to execute that with mocha? i.e what is the command line to test with i.e. ts-node --no-project node_modules/mocha/bin/mocha -r ts-node/register ./tests/example.ts`` or ts-node --no-project node_modules/mocha/bin/_mocha -r ts-node/register ./tests/example.ts or

i do not quite understand to what should i apply that “–no-project” directive.

basically the question is how to use the ts-node correctly with mocha.

and here are some stats

node_modules/ts-node/dist$ ./bin.js --version
ts-node v4.1.0
node v9.4.0
typescript v2.6.2

any help is really appreciated.

regards,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
courteouscommented, Jan 26, 2018

got it changing node-ts does not support es6 yet, described here:

https://github.com/TypeStrong/ts-node/issues/212

changing the module parameters from

// “module”: “es6”,
“module”: “commonjs”,

worked for me, although es6 support will be nice since that is where the road is going anyway.

5reactions
KillyMXIcommented, Nov 1, 2021

This gist did the job for me: https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d

I can’t see “node-option” documented anywhere but it actually works and seems prettier than using node_modules internals or falling back to CommonJS.

Mocha section in package.json:

  "mocha": {
    "extension": [
      "ts"
    ],
    "node-option": [
      "experimental-specifier-resolution=node",
      "loader=ts-node/esm"
    ],
    "spec": "test/**/*.ts"
  }

ts-node section in tsconfig.json:

  "ts-node": {
    "compilerOptions": {
      "module": "ESNext",
      "esModuleInterop": true,
      "moduleResolution": "Node"
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding `tsconfig.json` for ts-node in mocha - Stack Overflow
You need to set the configuration through the TS_NODE_COMPILER_OPTIONS environment variable. Example code on an unix machine:
Read more >
Setting up Mocha.js to run TypeScript - Dave Schinkel - Medium
I've created an additional compiler.js file which tells ts-node to process my additional tsconfig.json that lives in my src/test folder:
Read more >
Node + TypeScript + Mocha + ESLint - Level Up Coding
This article is a guide to set up a programming project with Node, TypeScript, Mocha, and ESLint.
Read more >
ts-mocha - npm
Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity.
Read more >
Mocha | ts-node - TypeStrong · GitHub
mocha --require ts-node/register --extensions ts,tsx --watch --watch-files src 'tests/**/*. ... Or specify options via your mocha config file. .mocharc.json.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found