Get testing working with ChromeUtils.import no longer creating globals
Categories
(Firefox :: New Tab Page, defect, P1)
Tracking
()
Tracking | Status | |
---|---|---|
firefox67 | --- | fixed |
People
(Reporter: Mardak, Assigned: k88hudson)
References
Details
(Keywords: github-merged, Whiteboard: [export defect])
Bug 1514594 is changing the import API to not modify global and just takes the file with no second argument object with a return value of the object imported similar to esmodule import.
Pointing eslint-plugin-mozilla to the 1.1.0 version in mozilla-central and fixing up a var added to common/PerfService.jsm results in testmc:unit having failures like…
"message": "TypeError: XPCOMUtils is undefined\nat webpack:///lib/ASRouter.jsm:10
Reporter | ||
Comment 1•5 years ago
|
||
Oh. Probably something related to how we use const {Foo} = ChromeUtils.import("resource://activity-stream/…Foo.jsm", {})
but just plain ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm")
our basePath is only set to resource://activity-stream so jsm-to-commonjs doesn't know what to do about resource://gre/modules
Reporter | ||
Comment 2•5 years ago
|
||
So I guess the underlying issue is that our testing setup just ignored ChromeUtils.import("resource://gre…") and unit-entry.js would create various fake globals in the test environment to simulate those imports to be loaded. However, the change in bug 1514594 then means each jsm now is locally declaring those variables…
I'm not sure if this means we should make jsm-to-commonjs have some option to discard variable declarations to other resources ???
Reporter | ||
Comment 3•5 years ago
•
|
||
kmag (or Standard8 if you've run into this before?), do you have any suggestions on how to get the dynamic global changing behavior that we've been using for testing / stubbing? We had been running unit test by mocking various objects on the global object including global.ChromeUtils.import to do nothing. Some tests dynamically change the behavior of a global after a jsm has been loaded, but this no longer works with the local scoping and reference acquired eagerly at import.
So.. this seems to fix quite a few failing tests
+++ b/test/unit/unit-entry.js
@@ -36,17 +36,25 @@ const TEST_GLOBAL = {
ChromeUtils: {
import(str) {
- return {};
+ return new Proxy(global, {
+ get(o, p) {
+ return new Proxy(o[p] || {}, {
+ get(O, P) {
+ return global[p][P];
+ }
+ });
+ }
+ });
except for those that try to modify prototype (proxy must report the same value for the non-writable, non-configurable property '"prototype"'
)… Basically for jsm that try to access a global that is later set, e.g., in a test's beforeEach, the second nested proxy adds delayed lazy global object lookup of the property.
lib/ASRouter.jsm imports AttributionCode then does AttributionCode._clearCache on some action/event. The jsm gets loaded when global.AttributionCode is undefined so our outer Proxy returns an inner proxy with empty object target for the mean time. The unit test sets a new global AttributionCode, so when ASRouter tries to call _clearCache, it triggers the inner Proxy to get the current global created by the test.
Reporter | ||
Comment 4•5 years ago
|
||
We'll fix our automation by getting rid of the declaration statements that are for imports that we don't webpack. This should result in the jsm being tested to not have the local declaration and references will then just access the global instead.
Reporter | ||
Comment 5•5 years ago
|
||
Plugins updated:
https://github.com/k88hudson/babel-plugin-jsm-to-esmodules/commit/56565b2db4236c28e0036d475063fb0ef9060a4c
https://github.com/k88hudson/babel-plugin-jsm-to-commonjs/commit/bf2dcf01f47038454010c14ca9c1ff2c0c945604
Activity Stream testing fixed:
https://github.com/mozilla/activity-stream/commit/01a4d1659bfe76763250f6d6756b67ae3d4d66d2
Reporter | ||
Updated•5 years ago
|
Updated•5 years ago
|
Description
•