ts-mocha: TypeError: Unknown file extension ".ts" - esmodules support
Original post in Anchor repo: [(https://github.com/project-serum/anchor/issues/1286)]
Anchor.toml [script] command: test = “yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts”
The problem is the m1.ts file cannot import functions from another ts file!!!??? in my utils.ts
export const log1 = console.log;
in my m1.ts file:
import { log1 } from './utils';
describe('scenario1', async () => {
it('initialize', async () => {
log1('\n---------== init');
});
});
the imported log1 function or any other function will cause the Unknown file extension “.ts” error!!??
my local package dependencies: “mocha”: “^9.1.3”, “ts-mocha”: “^9.0.0-alpha1”, “ts-node”: “^10.4.0”, “typescript”: “^4.5.4”
Please advise. Thank you
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 16
- Comments: 39 (7 by maintainers)
Hello everyone,I have the same problem and I think the problem was caused by tsconfig.json:
Hope this trick can help.
I’ve tried a variety of solutions in this thread. These did not work:
tsconfig.json
’s module type tocommonjs
was not a fix. (This was already the case.)"type": "module"
in package.json was not a fix. (Wasn’t the case.)loader=ts-node/esm
did not fix things.So, I’m kind of at a loss.
Try
ts-mocha -n loader=ts-node/esm -p tsconfig.json tests/**/*.test.ts
via npm scripts, it works for me! (Optional) Checknpx ts-mocha --help
for more details of this option.In my case, on of my dependencies (nanoid) added
"type": "module"
to their package.json. I had to downgrade nanoid to fix mocha.create .mocharc.json and
Hello Everyone, thanks for multiple useful contributions here in this issue, I’ll try to look into this issue today and propose some solutions, also I will try to include a special test case scenario on a branch in the repo to help with debugging. Cheers!
Pretty sure this is due to
type: module
in package.json (possibly in a dep). See the ts-node issue for this.Not sure how to mangle
ts-mocha
into doing the right thing here? Something likenode --loader=ts-node/esm ts-mocha ...
should be enough, but doesn’t work for me, perhapsts-mocha
needs to support--loader
itself?I have created a branch with a reproduction test case for experimentation here: https://github.com/piotrwitek/ts-mocha/tree/support-for-node-es-modules
After some research, the issue was indeed introduced by ts-node dependency not handling
esmodules
and I found some solutions we could try to implement:More details on ts-node
esm
support:I won’t have time to work on it anytime soon, but will try to help and accept PRs for this. Cheers!
Thank you for your post… but “ts-mocha”: “9.0.0” results in the same error as above.
I tried adding this but then get:
node 18.17.0
i switched to vitest, almost there
@webJose So, since I commented on this, I’ve switched test runners to Vitest in every scenario. As far as I can tell, Jest / Mocha just don’t work, or don’t work consistently, and Vitest works with very little configuration.
came across this today
Google found this: https://github.com/TypeStrong/ts-node/discussions/1268
Hi @piotrwitek , thank you for your quick reply. Please take the following code: https://github.com/AuroraLantean/SolanaAnchorDemo2 Thank you!