Closed GProst closed 7 years ago
Packages versions: electron: 1.6.2 karma: 1.5.0 karma-electron: 5.1.1 karma-jasmine: 1.1.0 karma-ng-html2js-preprocessor: 1.0.0 angular: 1.6.2 angular-mocks: 1.6.2
Thanks for the detailed bug report. I'm not sure what would cause this so we'll definitely need to triage. I'll perform a triage by the end of the weekend
We have successfully reproduced the error. We were able to reproduce with something as simple as:
var electron = require('electron');
console.log(electron);
My hunch is this is an issue in Karma's serializer due to Electron using some native modules but I'll keep on digging
Okay, we've traced down the source of this issue. electron
is using a Proxy
constructor for its remote functions:
https://github.com/electron/electron/blob/v1.6.3/lib/renderer/api/remote.js#L170-L190
Proxies cannot be serialized without causing a TypeError
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString
new Proxy(function x () {}, {}).toString()
// VM594:1 Uncaught TypeError: Function.prototype.toString is not generic
// at Proxy.toString (<anonymous>)
// at <anonymous>:1:33
Unfortunately, proxies can't be detected either (due to them being proxies -- intentionally transparent). However, we can detect them having serialization issues and provide a fallback.
case 'function':
try {
return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
} catch (err) {
if (err instanceof TypeError) {
return 'Proxy(function ' + (obj.name || '') + '(...) { ... })';
} else {
throw err;
}
}
which yields:
Object{clipboard: Object{availableFormats: Proxy(function remoteMemberFunction(...) { ... }), has: Proxy(function remoteMemberFunction(...) { ... }),
Our plan of action is 2 part:
try/catch
to Karma so it falls back when encountering proxiestoString
logic to Electron's Proxy
wrappernew Proxy(function x () {}, {get: function () { return function () { return x.toString() }; }}).toString()
// "function x() {}"
As a workaround for you, you should be able to use a fork of Karma with the patched serializer (which it seems you're already doing). This can be formalized by a git+ssh
URL but whatever works for you :+1:
https://docs.npmjs.com/files/package.json#git-urls-as-dependencies
Okay, 1 PR down -- Karma https://github.com/karma-runner/karma/issues/2595. 1 PR to go
Finally got the repo locally bootstrapped. Took a lot longer than I expected because I missed the quick bootstrap notes again:
Alright, second PR is open: https://github.com/electron/electron/pull/8890
Since this issue is not specific to our repo at all, we are going to close it. Thanks for the bug report and finding that edge case =)
Great! Thanks for being quick to react, I will look forward for your PR's to be merged and released.
Full stacktrace:
In context.js file I
console.info()
that function name and it showselectron
's internalremoteMemberFunction
function.In my app I use
electron
'sremote.require('moduleName')
several times. I think it may be important causeremoteMemberFunction
is defined inelectron
'srenderer/api/remote.js
file.I start karma via
karma start
. After replacingreturn obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
line in content.js withreturn;
karma seems to work fine (this is my temporal decision of solving problem).Here is my karma.conf:
Thank you in advance!