ts-node-test icon indicating copy to clipboard operation
ts-node-test copied to clipboard

Trouble with absolute imports

Open jasonbarry opened this issue 2 years ago • 3 comments

I have paths set in my tsconfig like so

"compilerOptions": {
  "paths": {
    "~app/*": ["src/*"]
  }
},

But when I try to run ts-node-test, I get

Error: Cannot find package '~app' imported from <path>

Does ts-node-test look at compilerOptions.paths ?

jasonbarry avatar Jun 16 '23 19:06 jasonbarry

It doesn't, and I think it doesn't need to since it doesn't handle imports at all. This is the responsibility of Node.js + ts-node. ts-node-test only recursively resolves the list of test files and calls node --test --loader=ts-node/esm with that list. What happens when you run your test file with ts-node --esm test/my-test-file.test.ts?

meyfa avatar Jun 16 '23 19:06 meyfa

$ TEST_EXTENSIONS=.spec.ts ts-node --esm ./utils.spec.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for <path>

jasonbarry avatar Jun 16 '23 21:06 jasonbarry

Unfortunately I don't know what causes this, but I'm pretty sure it has nothing to do with ts-node-test. It's likely some CommonJS/ESM weirdness and/or missing or invalid configuration of ts-node. It's also entirely possible that Node.js's --test flag messes with the ts-node loader, since --test spawns child processes.

meyfa avatar Jun 16 '23 21:06 meyfa