bokuweb / karma-nightmare

⚡ A Karma plugin. Launcher for Nightmare
52 stars 5 forks source link

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

Open suchipi opened 6 years ago

suchipi commented 6 years ago

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

suchipi commented 6 years ago

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 commented 6 years ago

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

suchipi commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

suchipi commented 6 years ago

Thank you!

bokuweb commented 6 years ago

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

suchipi commented 6 years ago

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

To test:

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 commented 6 years ago

Hmmm, sorry, I'll do the investigation.

suchipi commented 6 years ago

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 commented 6 years ago

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