cucumber / cucumber-js

Cucumber for JavaScript
https://cucumber.io
MIT License
5.04k stars 1.09k forks source link

Documented way to include ts-node with esm prints warning, alternative swallows errors #2359

Closed timvahlbrock closed 9 months ago

timvahlbrock commented 9 months ago

👓 What did you see?

The documented way of registering ts-node in esm context by setting the nom script to cross-env NODE_OPTIONS='--loader ts-node/esm' cucumber-js creates the following warning.

(node:77515) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)

I simplified that to adding this to my cucumber.mjs:

import { register } from "node:module";
import { pathToFileURL } from "node:url";
register("ts-node/esm", pathToFileURL("./"));

This works, but typescript errors are swallowed (by VError in cucumber), because they are not actual error instances:

./node_modules/verror/lib/verror.js:298
        mod_assertplus.ok(mod_isError(err), 'err must be an Error');
                ^
AssertionError [ERR_ASSERTION]: err must be an Error
    at Function.VError.fullStack (./node_modules/verror/lib/verror.js:298:17)
    at Object.run [as default] (./node_modules/@cucumber/cucumber/src/cli/run.ts:35:35) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '=='
}

✅ What did you expect to see?

No warnings, TS Error is printed.

📦 Which tool/library version are you using?

@cucumber/cucumber: 10.0.1 ts-node: 10.9.2 Node: 20

🔬 How could we reproduce it?

Clone https://github.com/timvahlbrock/cucumber-tsnode, install dependencies, run scripts test and test-old. The earlier shows the V-Error problem, the later the Node Warning.

📚 Any additional context?


This text was originally generated from a template, then edited by hand. You can modify the template here.

davidjgoss commented 9 months ago

Thanks for raising @timvahlbrock.

I've got a PR up to remove verror since we can now use platform-native stuff to achieve the same thing. This would make it tolerant of errors that aren't Error instances, so should solve that problem for you.

For the point about loaders, I've raised https://github.com/cucumber/cucumber-js/issues/2364. Both --loader and register() are still experimental, we may let this shake out a bit before committing to putting something in our own API, just to manage expectations.

davidjgoss commented 9 months ago

Released in https://github.com/cucumber/cucumber-js/releases/tag/v10.1.0