test: add a simple test for import defer of a CJS module#64694
test: add a simple test for import defer of a CJS module#64694MayaLekova wants to merge 1 commit into
import defer of a CJS module#64694Conversation
|
@nicolo-ribaudo and @joyeecheung please take a look, thanks! |
This tests imports a CommonJS modules with the `defer` modifier. It ensures that the imported module is not evaluated before accessing properties form its exports. Signed-off-by: Maya Lekova <maya@igalia.com>
de9e5ac to
2d51c34
Compare
| assert.equal(imported.default.foo, 42); | ||
| assert.equal(imported.default.identifier, 'package-type-commonjs'); |
There was a problem hiding this comment.
| assert.equal(imported.default.foo, 42); | |
| assert.equal(imported.default.identifier, 'package-type-commonjs'); | |
| assert.equal(imported.foo, 42); | |
| assert.deepStrictEqual(['defer-1'], globalThis.eval_list); | |
| assert.equal(imported.identifier, 'package-type-commonjs'); |
The lexer should recognize these. Also we should test that it only gets evaluated once.
| assert.equal(imported.default.identifier, 'package-type-commonjs'); | ||
|
|
||
| // Check that the module has been evaluated at this point. | ||
| assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list); |
There was a problem hiding this comment.
| assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list); | |
| assert.deepStrictEqual(['defer-1'], globalThis.eval_list); |
It shouldn't be partial, if there are anything else, it's a bug.
|
|
||
| // Check that the module hasn't been evaluated yet (which is noted | ||
| // by adding a property to `globalThis.eval_list`). | ||
| assert.strictEqual(globalThis.eval_list.length, 0); |
There was a problem hiding this comment.
| assert.strictEqual(globalThis.eval_list.length, 0); | |
| assert.deepStrictEqual(globalThis.eval_list, []); |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #64694 +/- ##
=======================================
Coverage 90.13% 90.13%
=======================================
Files 741 741
Lines 242106 242112 +6
Branches 45551 45595 +44
=======================================
+ Hits 218211 218221 +10
+ Misses 15430 15365 -65
- Partials 8465 8526 +61 🚀 New features to boost your workflow:
|
| if (!globalThis.eval_list) { | ||
| globalThis.eval_list = []; | ||
| } |
There was a problem hiding this comment.
tiniest optional nit...
globalThis.eval_list ??= ['defer-1'];| globalThis.eval_list = []; | ||
|
|
||
| // Import the CJS module with the `defer` modifier. | ||
| // import defer * as imported from '../fixtures/es-modules/package-type-commonjs/index.js'; | ||
| import defer * as imported from '../fixtures/es-modules/module-cjs-deferred-eval.js'; |
There was a problem hiding this comment.
It would be better to swap these two statements, so that when reading somebody does not mistakenly assume that globalThis.eval_list = [] would be evaluated first (imports are hoisted and executed before code in the file).
This test right now would pass regardless of whether import defer is eager or deferred. If it's eager it passes because:
- after the eager
../fixtures/es-modules/module-cjs-deferred-eval.jsevaluation, this file setsglobalThis.eval_list = [];. Then it asserts that its length is 0, which is obviously true because we just set it to an empty array. - further down, in
assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list);, we are checking that['defer-1']contains all the elements ofglobalThis.eval_list. IfglobalThis.eval_listis empty, that's tautologically true.
I have not run this test, but I assume the reason in test/fixtures/es-modules/module-cjs-deferred-eval.js you had to add if (!globalThis.eval_list) { globalThis.eval_list = []; } is because the CJS module is indeed not being deferred, so it's running before that this file defines globalThis.eval_list = [].
Maybe better, either one of this:
- move the
globalThis.eval_list = []initialization to a separate file that is imported at the beginning of this one, before the other imports, so that we know it runs first - use a boolean instead of an array, and then we don't need to pre-initialize it.
test: add a simple test for
import deferof a CJS moduleThis tests imports a CommonJS modules with the
defermodifier.It ensures that the imported module is not evaluated before
accessing properties form its exports.