Closed Bug 1523488 Opened 5 years ago Closed 5 years ago

Get testing working with ChromeUtils.import no longer creating globals

Categories

(Firefox :: New Tab Page, defect, P1)

defect

Tracking

()

RESOLVED FIXED
Firefox 67
Iteration:
67.1 - Jan 28 - Feb 10
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

Flags: needinfo?(khudson)

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

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 ???

Summary: Update babel plugins for ChromeUtils.import API changes → Get testing working with ChromeUtils.import no longer creating globals

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.

Flags: needinfo?(kmaglione+bmo)

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.

Assignee: nobody → khudson
Flags: needinfo?(kmaglione+bmo)
Flags: needinfo?(khudson)
Iteration: --- → 67.1 - Jan 28 - Feb 10
Keywords: github-merged
Target Milestone: --- → Firefox 67
Component: Activity Streams: Newtab → New Tab Page
You need to log in before you can comment on or make changes to this bug.