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

Issue 2105 more detailed error #2264

Merged

Conversation

michael-lloyd-morris
Copy link
Contributor

@michael-lloyd-morris michael-lloyd-morris commented Mar 20, 2023

🤔 What's changed?

The error message viewed when importing a CommonJS module or requiring an ES module is now customized.

⚡️ What's your motivation?

Making life easier when simple mistakes are made. The error message Node gives when you try to import steps using CommonJS format or require an ES module are a bit cryptic. The new error message tells the user they are using the wrong directive and to either change the file's format or use the other directive to include the file.

🏷️ What kind of change is this?

  • ⚡ New feature (non-breaking change which adds new behaviour)

📋 Checklist:

  • I agree to respect and uphold the Cucumber Community Code of Conduct
  • I've changed the behaviour of the code
    • I have added/updated tests to cover my changes.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • Users should know about my change
    • I have added an entry to the "Unreleased" section of the CHANGELOG, linking to this pull request.

@coveralls
Copy link

coveralls commented Mar 20, 2023

Coverage Status

Coverage: 98.489% (-0.04%) from 98.525% when pulling c0fa54d on michael-lloyd-morris:issue-2105-more-detailed-error into aae711c on cucumber:main.

Copy link
Contributor

@davidjgoss davidjgoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing we need to account for here is that, unlike the other way around, it's perfectly valid to import() a CommonJS module.

The example in the feature file yields an error from Node.js because the file is evaluated as an ES module (due to its .mjs suffix) but contains require() calls so is effectively invalid JavaScript from the engine's perspective. If that file was .cjs or even .js (assuming no `type="module" on the package) it would work fine with the import option.

I think the issue can also show up where you use an ESM loader but it outputs code with require() in - again because the file purports to be a module but contains require() calls. This could be easily done if you misconfigure ts-node for example.

It's probably worth catching that error still in the importer, but I think the messaging will need to be more along the lines of "it looks like some of your support code is using require syntax where only imports are expected" and a maybe a link relevant Node.js docs.

@michael-lloyd-morris
Copy link
Contributor Author

michael-lloyd-morris commented Mar 20, 2023

I did not know that it's valid to import a common JS module. That does explain why the error messages are so different. Of the two native nodejs errors that one is the more easy to understand, so I'll remove the catch on the importer for now to satisfy this requirement along with the scenario testing for that code change. Shall I keep the remaining feature alone in it's own file or move it into the esm.feature file? (on second thought, just going ahead and doing that as it makes the most sense, but I can undo it).

@michael-lloyd-morris
Copy link
Contributor Author

Requested Changes applied.

@michael-lloyd-morris michael-lloyd-morris self-assigned this Mar 21, 2023
@michael-lloyd-morris michael-lloyd-morris added this to the 9.0.0 milestone Mar 21, 2023
@michael-lloyd-morris
Copy link
Contributor Author

Is there something else I need to do or where the changes I did insufficient?

src/api/support.ts Show resolved Hide resolved
src/api/support.ts Outdated Show resolved Hide resolved
@davidjgoss
Copy link
Contributor

Just a couple of things to address but this is looking like a good change. Also see a conflict on the changelog file.

@michael-lloyd-morris
Copy link
Contributor Author

I'll get to these items in the morning and will try to be done by end of day, though I've got to take a chunk of the day to take my Dad to the hospital.

(Until I have a job, volunteer work here is my job - if only to keep my skills sharp and my sanity intact - so I'm treating it as such).

@michael-lloyd-morris
Copy link
Contributor Author

Ok, all requested changes applied, though see the note about Typescript and using the cause option on Error throws.

@davidjgoss davidjgoss removed this from the 9.0.0 milestone Mar 28, 2023
@davidjgoss davidjgoss merged commit d599dfe into cucumber:main Mar 28, 2023
10 checks passed
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

Successfully merging this pull request may close these issues.

None yet

3 participants