-
Notifications
You must be signed in to change notification settings - Fork 780
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
Qunit v2 has incorrectly configured exports #1724
Comments
The jsbin example uses https://esm.sh. I'm not familiar with that particular transformer, but based on how rollup and other transformers work, and from a quick glance at the I see that esm.sh documents a workaround in the form of https://esm.sh/#specify-cjs-exports. For example: import { test, module } from 'https://esm.sh/[email protected]?cjs-exports=test,module';
module(…
test(… Assuming there is a way esm.sh can understand CJS exports automatically, I'd love to make this work in an upcoming QUnit 2.x release. We can debug the above and figure out what it's doing in the I note that QUnit does not currently provide its own ESM release (yet). QUnit does currently contain the following: if (window) {
window.QUnit = QUnit;
// […]
if (typeof module !== 'undefined' && module && module.exports) {
module.exports = QUnit;
module.exports.QUnit = QUnit;
// […] The intent of this is to allow developers to choose their preferred style. You can import individual methods, or you can import the API as a whole. And the latter is possible both as default and as explicitly named The design of QUnit is generally such that individual test files do not need to be written with knowledge or hardcoded awareness of where or how QUnit was loaded. (Afaik most test runners will have already loaded and configured QUnit before the first test file test file loads, and made the API available as global variable. So it'd only be looking to regain access to the cached import at this point.) Thus, assuming the test runner has loaded QUnit for you already, you can (usually) do the following: const { module, test } = QUnit;
module(…
test(… Or, directly: QUnit.module(…
QUnit.test(… QUnit does support being imported in every test file, and that worked under CJS, so it'd be nice for that to work under ESM as well. I wonder if that is still needed today though? I'd love to understand whether that's by choice or whether there's a specific runtime requirement that forces this habbit. The most common case where this comes up is when you bootstrap your own standalone QUnit process. For example, using a generic framework-agnostic test runner like airtap/browserify. Or when you use Node.js/SpiderMonkey/Deno plainly (e.g. without QUnit CLI); then you do indeed need to load QUnit at least once on your index or entry point script. That entry point could use |
thanks for the deep investigation! however, I don't think we want to focus on the specifics of what Here is a stackblitz, similar to my jsbin, which uses https://stackblitz.com/edit/stackblitz-starters-j2kwo2? in // This is what folks using ESM expect
// But, are given:
// SyntaxError: Named export 'module' not found. The requested module 'qunit' is a CommonJS module, which may not support all module.exports as named exports.
// import { module, test } from 'qunit';
// Works, but is not ideal, and not what folks using ESM expect to do.
import Qunit from 'qunit';
const { module, test } = Qunit;
import { add } from '../src/add.js';
module('add', function () {
test('can add valid inputs', function (assert) {
assert.strictEqual(add(1, 3), 4);
});
});
I think that's fine as a goal, but that's not what the expectations of ESM are. Nearly all tooling in the ecosystem now operates under the assumption that you can easily go-to-definition from an identifier and eventually got to where the thing is defined. If QUnit is a global, or generally just not imported, you're not going to be able to have this ergonomics table stakes.
but that's not how folks (in my circle of the web anyway) want to use things in ESM.
I see two paths forward here to meet all goals stated in your reply (again, thanks for the super thorough investigation!)
thank you! |
I took a stab at a PR over here: #1728, but I don't yet have a way to test browser-ESM due to constraints of the test setup |
Note to self: understand this commit and validate what we do against it. |
Tell us about your runtime:
Repro: https://jsbin.com/fipayiy/edit?html,output
What are you trying to do?
import { module, test } from 'qunit'
but this is how you have to use
module
/test
:Repro: https://jsbin.com/fipayiy/edit?html,output
The text was updated successfully, but these errors were encountered: