Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kran6a opened this issue Apr 16, 2022 · 7 comments
Open

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

kran6a opened this issue Apr 16, 2022 · 7 comments

Comments

@kran6a
Copy link

kran6a commented Apr 16, 2022

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
Copy link
Owner

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

@sorousht
Copy link

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

@cmdruid
Copy link

cmdruid commented Sep 6, 2022

I face the same issue as well.

@dcaillibaud
Copy link

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
Copy link

jiouiuw commented Oct 15, 2022

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
Copy link

Same issue. @jiouiuw doesn't seem to work

@jiouiuw
Copy link

jiouiuw commented Aug 24, 2023

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants