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
[testing] chai plugins #729
Comments
the status of chai plugins is the same as for many other libraries when it comes to es modules... adoption is slow as node is the primary target for many... and node by default still does not support es modules 😭 if there is a simple way to use the "normal" chai and not a bundled-es-modules version then we should totally do and recommend that... 🤗 so if you could do a PR for option B that would be awesome 💪 Just to be sure our primary goal is still to test code natively in the browser (e.g. no transformations* or adjustments to the code before it gets shipped to the browser)**.
According to our defined goal of using native es modules without any transformations in the browser mocha/chai was our best bet (back then I knew at least how to make es modules work in mocha; while I did not know it for jasmine) how is support for es modules in jasmine nowadays? * with the exception of bare module |
The jasmine framework doesn't need any transformation to the code if you use Karma-jasmine with karma-esm, but the CLI doesn't support ESM for now. See my comment on the related issue ☝️ It actually does even less transformation to your source code than Chai/Mocha. Here is an example: source file
Using karma-jasmine, I get exactly the same file in the browser, except the link to the source-map is added at the end. But with Mocha, I get: transformed by Mocha/Chai
|
Jasmine requires the In the Polymer projects we use mainline Mocha and Chai, and just load any other needed files via script tags. It's more important that library under test and tests are modules, than the test framework for the moment. |
About using Chai directly (answer to #729 (comment)): This is just a workaround permitting to use chai plugins without any modification. I would be happy to do a PR for that, but I think I would only instructions to the documentation, without modifying anything, because it looks kind of dirty ... except if you think we should provide some Chai plugins by default? And I get the following error when we add './node_modules/chai/chai.js' to the files array in karma config, so we do need the bundled-es-modules version of Chai.
|
And thx for the explanations you provided @justinfagnani & @daKmoR! This will be very useful for my next talk 😃 @daKmoR, are you talking about the "should" syntax? I would love to have a discussion about these topics with you both, but maybe it would be better to do that out of Github, in order to avoid to pollute the issues. |
the transformation you see is for code coverage... it has "nothing" to with mocha... I assume you would get the same transformations if you apply code coverage and you run it with jasmine
hehe I see this as another "holy" war - equal to tabs vs spaces or
something to test out 💪 we will probably need to change our existing plugins or at least load them differently as I assume that is why @noelmace sees the errors when not using the bundles es module version of chai... |
Using chai plugins with
@open-wc/testing
is tedious and undocumented.root cause & context
Chai and most chai plugins aren't exposed as es modules.
This led to the creation of @bundled-es-modules/chai and the following issues:
But these projects don't seem to be actively maintained anymore. So, there isn't any advancement on this right now.
current options
Developers using @open-wc/testing do not have many good options when using chai plugins.
A. esm fork
👉 nathanboktae/chai-dom#38 (comment)
B. define plugins globally
Most chai plugins (chai-dom, sinon-chai, chai-datetime) fall back to using window.chai.use when AMD, CommonJS, etc... aren't available:
So, we can:
install the "official" (non esm) packages
npm i -D chai-dom sinon-chai
import chai from
bundled-esm-modules/chai
, add it to window.chai, and then dynamically import the plugins packages:https://github.com/noelmace/devcards/blob/cf0fe84deb338c47699b2aa289100b9834a68e84/test/utils/chai-plugins.js#L1-L6
https://github.com/noelmace/devcards/blob/cf0fe84deb338c47699b2aa289100b9834a68e84/karma.owc.conf.js#L8-L9
proposals for resolution
The text was updated successfully, but these errors were encountered: