Skip to content

ts-node using with mocha and tsconfig.json #521

Closed
@courteous

Description

@courteous

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,

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionSupport requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions