Skip to content

fix: errorCodes import using ESM#5390

Merged
Uzlopak merged 2 commits intofastify:mainfrom
melroy89:patch-1
Apr 7, 2024
Merged

fix: errorCodes import using ESM#5390
Uzlopak merged 2 commits intofastify:mainfrom
melroy89:patch-1

Conversation

@melroy89
Copy link
Copy Markdown
Contributor

@melroy89 melroy89 commented Apr 6, 2024

  • Fixing errorCodes import using ESM.
  • Adding an unit-test
  • Extend .taprc configuration to run test.mjs files as well
  • Rename other mjs files to .test.mjs filename?

Checklist

In favor of: #5388

Moving fastify errorCodes below module.exports = fastifty and also export it

Signed-off-by: Melroy van den Berg <melroy@melroy.org>
Copy link
Copy Markdown
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

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

What is this fixing? Please provide context when opening pull requests. Also, this is missing a test.

@melroy89 melroy89 changed the title fix: errorCodes for ESM fix: errorCodes import using ESM Apr 6, 2024
@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 6, 2024

It's indeed missing a test. I first wanted to know if this is what we want according to climba03003.

@climba03003
Copy link
Copy Markdown
Member

climba03003 commented Apr 6, 2024

It's indeed missing a test. I first wanted to know if this is what we want according to climba03003.

Yes, it is what I said. If you need to test it, use .mjs extension for the file.

What is this fixing? Please provide context when opening pull requests. Also, this is missing a test.

It is actually fixing how people can be import errorCodes in ESM.

// Before
import Fastify from 'fastify'
const { errorCodes } = Fastify

// After
import { errorCodes } from 'fastify'

The reason is behind how Node.js transform CJS to ESM named export.
Not going to explain the detail, but it requires the syntax of module.export.<named export> = <value>

For anyone how interested in the details, see https://github.com/nodejs/cjs-module-lexer?tab=readme-ov-file#grammar

Uzlopak
Uzlopak previously approved these changes Apr 6, 2024
Copy link
Copy Markdown
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

LGTM

@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 6, 2024

It is actually fixing how people can be import errorCodes in ESM.

Important to mention this "after" example is already documented on your fastify website (it's not yet working like this): https://fastify.dev/docs/latest/Reference/Errors/#fastify-error-codes

This pr try to solve that, so it's inline with the documentation.

@Uzlopak
Copy link
Copy Markdown
Contributor

Uzlopak commented Apr 6, 2024

What we need is a test. So basically create a esm-error-codes.test.mjs and import the errorCodes as mentioned in the documentation and check if it is not undefined.

@melroy89

This comment was marked as outdated.

@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 6, 2024

There are TS Type tests in tests/types folder (incl. errors.tst-d.ts), how to execute those tests? Since tap doesn't seem to run them when executing npm run test

Or are they ran using fastify@4.26.2 test:typescript, without any output, no success message, nothing?

EDIT: Yes the errors.tst-d.ts didn't catch this issue.

@Uzlopak
Copy link
Copy Markdown
Contributor

Uzlopak commented Apr 6, 2024

The typescript typings tests are Independent from this issue. Just because you test it with tsd, you wont test it in node.

@Uzlopak Uzlopak dismissed their stale review April 6, 2024 17:57

Missing test

@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 6, 2024

It seems that the tests in esm directory are not all executed (only test/esm/index.test.js ). Which is a problem!

I believe tap is only executing .js files.

@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 6, 2024

Should I extend the .taprc file? with the following line:

  - 'test/**/*.test.mjs'

EDIT: Done..! Now my test is running.

PASS ​ test/esm/errorCodes.test.mjs 3 OK 23.488ms

Suites:   ​168 passed​, ​168 of 168 completed

@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 6, 2024

Should I rename the other tests in the esm directory to comply with the same filename convention for tests (aka .test.mjs in this case).? So those tests are getting executed by tap as well? Since now only my errorCodes.test.mjs test is getting executed. The other tests were never executed before via tap.. Basically dead tests.

(This is basically unrelated fixes outside of this PR, however your ESM tests were never part of your npm run test)

@melroy89 melroy89 requested review from Uzlopak and jsumners April 7, 2024 08:52
Copy link
Copy Markdown
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

LGTM

@Uzlopak Uzlopak merged commit b73b812 into fastify:main Apr 7, 2024
@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 7, 2024

Thanks for merging. I still would like to know what you want to do with those other ESM tests that were never part of tap?

@Uzlopak
Copy link
Copy Markdown
Contributor

Uzlopak commented Apr 7, 2024

It can not harm to rename them to mjs.

@melroy89
Copy link
Copy Markdown
Contributor Author

melroy89 commented Apr 7, 2024

It can not harm to rename them to mjs.

You mean .test.mjs as suffix (they already have mjs as "extension"), but I use the same file syntax as for your other tests: .test.js and .test.mjs. Currently those ESM files aren't executed (they were never executed actually), only my .test.mjs test :).

But OK, I think I will create a follow-up PR to fix those other ESM tests.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2025

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants