Open suchipi opened 6 years ago
@suchipi Hi :) Thanks for your report!
But I can not reproduce it, in my env...
window.require
is used in karma-nightmare
test itself.
https://github.com/bokuweb/karma-nightmare/blob/master/test/index.spec.js#L10
If test fixture has iframe#context
, karma-nightramare
inject require
to window
.
https://github.com/bokuweb/karma-nightmare/blob/master/lib/browser.js#L38
I will try to put together a repro repo you can clone. I'm using it in a pretty involved setup so it might be related to my karma config.
@suchipi It is very helpful. Did you customize your context.html
?
No, but I have tracked down the issue and it is related to that. window.require
is only undefined when Karma's context wrapper is present.
Here is the repro repo: https://github.com/suchipi/karma-nightmare-window-require-repro
The test passes the first time, but when you refresh the page, the karma context wrapper appears, and the test fails.
It looks like the code evaluated by nightmare can run in either the top-level context or the iframe context, so I think this line: https://github.com/bokuweb/karma-nightmare/blob/master/lib/browser.js#L35 Needs to be changed to something like:
const iframe = (window.parent ? window.parent.document : document).querySelector('iframe#context');
@suchipi I see. Thanks for your great investigation :) I'll fix it so.
Thank you!
@suchipi 0.4.15 published 🎉 Could you please try it, if you get a chance?
I have tried it but it does not fix the issue.
To test:
yarn upgrade karma-nightmare@latest
I misunderstood when browser.js
runs; I thought it would run on every refresh, but it does not, so the iframe fix I proposed does not do anything. Sorry about that.
I'm not sure how to fix it.
Hmmm, sorry, I'll do the investigation.
It looks like the preload script only runs the first time you load the window, not on subsequent refreshes; I think that's the issue here.
I am using this workaround in my code: window.require || window.parent.require
I see. understood. But I have not found a nice way to solve this problem yet.
Overview of the problem
I'm using karma-nightmare version [0.4.14] I am sure this issue is not a duplicate.
Description
The readme indicates that
window.require
is available, but because the code runs in an iframe,window.parent.require
must be used.Steps to Reproduce
Run the following in a test with karma and karma-nightmare:
Expected behavior
Outputs
function require(path) { ... }
Actual behavior
Outputs
undefined
Workaround
Outputs
function require(path) { ... }