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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 Feature: Support algorithmically setting loader #5016

Closed
FFdhorkin opened this issue Oct 13, 2023 · 2 comments
Closed

馃殌 Feature: Support algorithmically setting loader #5016

FFdhorkin opened this issue Oct 13, 2023 · 2 comments
Labels
type: question support question

Comments

@FFdhorkin
Copy link

FFdhorkin commented Oct 13, 2023

Is your feature request related to a problem or a nice-to-have?? Please describe.

I can successfully run my tests on CLI by running mocha --loader=ts-node/esm. However, I need to be able to do this algorithmically, not via CLI, and there doesn't appear to be any way to do so.

Because of this, when I run await mocha.loadFilesAsync();, I get an exception: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

Describe the solution you'd like

const mocha = new Mocha({ loader: "ts-node/esm" });

Describe alternatives you've considered

I've tried adding loader to mocha.opts and package.json and .mocharc.json and it doesn't seem to pick any of these up when run programmatically.

@FFdhorkin FFdhorkin added the type: feature enhancement proposal label Oct 13, 2023
@nwalters512
Copy link

The loader option is a feature of Node itself, not Mocha, and is passed directly through to the Node binary via this file: https://github.com/mochajs/mocha/blob/master/bin/mocha.js. There's no way to provide --loader from within an existing Node process.

Note that if you're running Node 20.6+, you can use the module.register API to do something essentially equivalent at runtime: https://nodejs.org/api/module.html#moduleregisterspecifier-parenturl-options. I don't think ts-node uses this yet, though I think tsx might.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Support algorithmically setting loader 馃殌 Feature: Support algorithmically setting loader Dec 27, 2023
@JoshuaKGoldberg
Copy link
Member

Yup, the module.register API is the way to do this!

mochajs/mocha-examples#75 -> mochajs/mocha-examples#76 has examples showing very straightforward on-the-fly transpilation: one with ts-node, the other with tsx. If you're looking for a more algorithmic/fancy example than that then I'd suggest posting an issue over on that repo.

Thanks all! 馃

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2024
@JoshuaKGoldberg JoshuaKGoldberg added type: question support question and removed type: feature enhancement proposal labels Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question support question
Projects
None yet
Development

No branches or pull requests

3 participants