piotrwitek / ts-mocha

Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
MIT License
190 stars 25 forks source link

ts-mocha does not resolve tsconfig path aliases when using ESM #74

Open kran6a opened 2 years ago

kran6a commented 2 years ago

When trying to execute a test importing a path alias ($lib) I get Cannot find package '$lib' imported from .... Tests that do not import path aliases are running fine and I don't know what's the problem since I am already using the --paths option and I have tsconfig-paths installed.

This is my current config: package.json

{
  "name": "test",
  "version": "0.0.1",
  "scripts": {
    "test": "ts-mocha --recursive -n loader=ts-node/esm -n experimental-specifier-resolution=node -p tsconfig.json --paths 'tests/**/*.spec.ts'",
  },
  "devDependencies": {
    "@sveltejs/adapter-node": "next",
    "@sveltejs/kit": "next",
    "@types/chai": "^4.3.0",
    "@types/expect": "^24.3.0",
    "@types/mocha": "^9.1.0",
    "@types/node": "^17.0.23",
    "@types/object-hash": "^2.2.1",
    "chai": "latest",
    "mocha": "latest",
    "svelte": "^3.46.6",
    "svelte-check": "^2.2.6",
    "svelte-preprocess": "^4.10.1",
    "ts-node": "^10.7.0",
    "ts-mocha": "^9.0.2",
    "tsconfig-paths": "^3.14.1",
    "tslib": "latest",
    "typescript": "next",
  },
  "type": "module",
  "dependencies": {
    "async-mutex": "^0.3.2",
    "level-rocksdb": "^5.0.0",
    "node-fetch": "^3.2.3",
    "object-hash": "^3.0.0",
    "secp256k1": "^4.0.3"
  }
}

tsconfig.json

{
    "extends": "./.svelte-kit/tsconfig.json",
    "compilerOptions": {
        "baseUrl": ".",
        "rootDir": ".",
        "module": "ESNext",
        "target": "ESNext",
        "lib": ["ESNext"],
        "typeRoots" : ["./node_modules/@types", "./src"],
        "sourceMap": true,
        "resolveJsonModule": true,
        "alwaysStrict": true
    },
    "typeAcquisition": {
        "enable": true
    },
    "paths": {
        "$src/*": ["src/*"],
        "$lib/*": ["src/lib/*"],
        "$api/*": ["src/routes/api/*"],
        "$app/*": [".svelte-kit/runtime/app/*"]
    }
}

Environment

TS_NODE_TRANSPILE_ONLY=1;
TS_NODE_PREFER_TS_EXTS=1;
piotrwitek commented 2 years ago

Hey, thanks for your report. We would need a minimal reproduction repo and will fix that easily. Thanks!

sorousht commented 2 years ago

Hey, I face the same issue. Did you find a way to fix it?

cmdruid commented 2 years ago

I face the same issue as well.

dcaillibaud commented 1 year ago

I have the same alias pb, here is a small repo to reproduce it : https://github.com/dcaillibaud/ts-mocha-boilerplate

Thanks for any clue to make it working.

jiouiuw commented 1 year ago

Try add TS_CONFIG_PATHS=true before ts-mocha, like this:

"test": "cross-env TS_CONFIG_PATHS=true ts-mocha --recursive -n loader=ts-node/esm -n experimental-specifier-resolution=node -p tsconfig.json --paths 'tests/**/*.spec.ts'",

cross-env is for windows environment.

furstenheim commented 1 year ago

Same issue. @jiouiuw doesn't seem to work

jiouiuw commented 1 year ago

@furstenheim Hi, Sorry to hear that.

Maybe you check this one? I try, It's worked

https://github.com/ts-monorepo/basic/blob/main/apps/hello/package.json

➜ pnpm --filter=hello-app test

> hello-app@1.0.0 test /Users/jiouiuw/Projects/basic/apps/hello
> cross-env NODE_ENV=test TS_CONFIG_PATHS=true ts-mocha -p tsconfig.json src/**/*.test.ts --parallel --timeout 30000

before test
after test

  tests
    ✔ test sum

  1 passing (536ms)