Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Mocha with TypeScript paths

I'm having an issue with running mocha when using the paths + baseUrl configuration in TypeScript

My tsconfig.js is set like so:

"baseUrl": "./src/", /* Base directory to resolve non-absolute module names. */
"paths": {
  "interfaces/*": [
    "interfaces/*"
  ],
  "models/*": [
    "models/*"
  ],
  "schemas/*": [
    "schemas/*"
  ],
  "classes/*": [
    "classes/*"
  ],
  "libs/*": [
    "libs/*"
  ],
  "config/*": [
    "config/*"
  ]

and I'm runnings mocha as "mocha build/test"

The compiled TS code fails to find my references since it compiles to

var user_1 = require("interfaces/user");

and if I add "../" beforehand it will compile without problems

Any ideas what I'm doing wrong here?

like image 508
K41F4r Avatar asked Aug 31 '17 17:08

K41F4r


2 Answers

You can use tsconfig-paths, follow the instructions on tsconfig-paths:

mocha -r ts-node/register -r tsconfig-paths/register "test/**/*.ts"
like image 134
xstefi Avatar answered Sep 28 '22 08:09

xstefi


You should use a mocha-TypeScript integration package, such as ts-mocha

like image 41
Eliran Pe'er Avatar answered Sep 28 '22 10:09

Eliran Pe'er