Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window.require does not work; you must use window.parent.require #27

Open
suchipi opened this issue Mar 25, 2018 · 12 comments
Open

window.require does not work; you must use window.parent.require #27

suchipi opened this issue Mar 25, 2018 · 12 comments

Comments

@suchipi
Copy link

suchipi commented Mar 25, 2018

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:

console.log(window.require);

Expected behavior

Outputs function require(path) { ... }

Actual behavior

Outputs undefined

Workaround

console.log(window.parent.require);

Outputs function require(path) { ... }

@bokuweb
Copy link
Owner

bokuweb commented Mar 25, 2018

@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

@suchipi
Copy link
Author

suchipi commented Mar 25, 2018

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.

@bokuweb
Copy link
Owner

bokuweb commented Mar 25, 2018

@suchipi It is very helpful. Did you customize your context.html ?

@suchipi
Copy link
Author

suchipi commented Mar 25, 2018

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.

@suchipi
Copy link
Author

suchipi commented Mar 25, 2018

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');

@bokuweb
Copy link
Owner

bokuweb commented Mar 25, 2018

@suchipi I see. Thanks for your great investigation :) I'll fix it so.

@suchipi
Copy link
Author

suchipi commented Mar 25, 2018

Thank you!

@bokuweb
Copy link
Owner

bokuweb commented Mar 25, 2018

@suchipi 0.4.15 published 🎉 Could you please try it, if you get a chance?

@suchipi
Copy link
Author

suchipi commented Mar 25, 2018

I have tried it but it does not fix the issue.

To test:

  • Clone the repro repo
  • Run yarn upgrade karma-nightmare@latest
  • Test the issue as described in the repo's readme

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.

@bokuweb
Copy link
Owner

bokuweb commented Mar 25, 2018

Hmmm, sorry, I'll do the investigation.

@suchipi
Copy link
Author

suchipi commented Mar 26, 2018

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

@bokuweb
Copy link
Owner

bokuweb commented Apr 13, 2018

I see. understood. But I have not found a nice way to solve this problem yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants