faker
faker copied to clipboard
test: extract cjs require tests to .spec.cts
I just go a great idea over night! 💡
What if we just move all require(*)-cjs calls into .spec.cts files. That way we can more safely use the require-nodejs keyword and ensure that it really works.
But not only this, when we will remove cjs support in v10, we could ensure this way at least for newer versions of nodejs, that it still works with their new require(esm) https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require
Deploy Preview for fakerjs ready!
| Name | Link |
|---|---|
| Latest commit | 84292d02904c79e8431191335dbe5b6307ba1546 |
| Latest deploy log | https://app.netlify.com/projects/fakerjs/deploys/6861a1f82630b10008313ab2 |
| Deploy Preview | https://deploy-preview-3436.fakerjs.dev |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify project configuration.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 99.97%. Comparing base (
c7086b2) to head (84292d0). Report is 1 commits behind head on next.
Additional details and impacted files
@@ Coverage Diff @@
## next #3436 +/- ##
=======================================
Coverage 99.97% 99.97%
=======================================
Files 2880 2880
Lines 220510 220510
Branches 952 952
=======================================
Hits 220457 220457
Misses 53 53
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
What if we just move all require(*)-cjs calls into .spec.cts files. That way we can more safely use the require-nodejs keyword and ensure that it really works.
What do you mean by "ensure that it really works"? Why does it not work right now? 🤔
Before this PR, we used require in a normal ts file which gets processed by vitest/vite and therefore potentially gets optimized (not sure if this really happens!)
When we now use .cts instead, we ensure that the file is running in pure common-js context.
https://github.com/faker-js/faker/pull/3436/files#diff-f8cd4ea46aecbba86ee9cba1a851bca523aca5e63bf9fa1866e3604a9d2dec26L9
https://github.com/faker-js/faker/blob/1e551c5f47e292d3dc6bbefae9eaf75664a052a1/test/locale-imports.spec.ts#L9
But not only this, when we will remove cjs support in v10, we could ensure this way at least for newer versions of nodejs, that it still works with their new require(esm) nodejs.org/api/modules.html#loading-ecmascript-modules-using-require
I don't see how moving the test in a separate file ensures this behavior. As far as I can tell this would require our code base to use explicit
*.m(t|j)sfile extensions, which is not the case right now. Am I missing something? Could you elaborate on this?
No, we wont need to ship .mjs files, because everything is controlled be package.json's "type": "module" and therefore the hole package will get consumed as pure esm package.
In combination with NodeJS's new require(esm), this finally get solved. And our .spec.cts file can cover that.
@xDivisionByZerox in the future (v10) we might need to run this .spec.cts test with Node v20.17+ and the --experimental-require-module or Node v23+
(see https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require and open the History)
And then we need to switch the required calls to depend on v10's esm bundled code
- require('../dist/index.cjs')
+ require('../dist/index.js')
- require(`../dist/locale/${locale}.cjs`)
+ require(`../dist/locale/${locale}.js`)
@xDivisionByZerox just an info, but https://github.com/faker-js/faker/pull/3540#issuecomment-3016734241 fails with node 18 But this PR here would also still run with node 18, because it actively still tests it with native cjs support and does not fake a require inside an esm file
Do you still wont like this cts test file change?