module-aliases in NPM doesn't work when running unit tests

142 views Asked by At

I defined an alias in package.json:

  "_moduleAliases": {
    "@microservices": "./microservices",
  }

When I use imports such as:

import ClassName from "@microservices/service_name/class_name";

it works when I launch the program.

However, this same imoprt fails to work when I run unit tests. I use Mocha test runner and when I run:

node_modules/mocha/bin/_mocha --inspect --opts=test/mocha.opts test/microservices/service_test.ts

I get the error:

Error: Cannot find module '@microservices/south_state_detector/app/base_task' Require stack: C:\MyProject\test\microservices\service_name\class_name.ts

It means that when running it from a unit test, mocha takes "service_name\class_name.ts" with relation to C:\MyProject\test and not from C:\MyProject.

Do you know how to resolve it so I can safely use module aliases also when running tests?

0

There are 0 answers