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

Run each set of tests in separate iframe #412

Closed
hakubo opened this issue Mar 19, 2013 · 24 comments
Closed

Run each set of tests in separate iframe #412

hakubo opened this issue Mar 19, 2013 · 24 comments

Comments

@hakubo
Copy link

hakubo commented Mar 19, 2013

This would make karma also superb for integration (and such) tests.
As they can easily pollute global variables or fixtures.

This would be awesome to have files variable in config file to accept something like:

files = [
    ['file.js', 'file.spec.js'],
    ['lib.js', 'lib.spec.js']
]

or an object

files = {
    'fileTest': ['file.js', 'file.spec.js'],
    'libTest': ['lib.js', 'lib.spec.js']
}

so you could name each set

and each array would be set of files to be loaded in a separate iframe
so you would not have to worry about polluting environment
and just focus on integration tests itself

@bitwiseman
Copy link
Contributor

👍
I have to do this on my own to shim my integration tests, so this seems like a fine idea.

@huyz
Copy link

huyz commented Mar 20, 2013

I'm also interested in how to groups of tests in isolation without fear of interference.

@vojtajina
Copy link
Contributor

Do not do global state and you gonna be fine ;-)

This is related to sharding (for huge test suites - one could run parts on
different machines to make it faster). It's not a high priority right now,
but I like this idea.

On Tue, Mar 19, 2013 at 11:51 PM, Huy Z [email protected] wrote:

I'm also interested in how to groups of tests in isolation without fear of
interference.


Reply to this email directly or view it on GitHubhttps://github.com//issues/412#issuecomment-15160826
.

@hakubo
Copy link
Author

hakubo commented Mar 21, 2013

I like the idea of not doing global states! But life is not that beautiful :)

@huyz
Copy link

huyz commented Mar 21, 2013

It's not just a problem with global states.
If you're using RequireJS, then all the modules that have already been loaded could have state modified by prior tests.

@dignifiedquire
Copy link
Member

What about adding a reset/reload mechanism? That way there is a way to ensure you have a clean slate but we don't need to manage multiple iframes.

On 21.03.2013, at 10:12, Huy Z [email protected] wrote:

It's not just a problem with global states.
If you're using RequireJS, then all the modules that have already been loaded could have state modified by prior tests.


Reply to this email directly or view it on GitHub.

@huyz
Copy link

huyz commented Mar 21, 2013

Sure. Although I'm not sure how it would work, given the example for main-test.js at http://karma-runner.github.com/0.8/plus/RequireJS.html, which looks to me as if the requireJS state wraps the karma runner.

@hakubo
Copy link
Author

hakubo commented Mar 21, 2013

Also having that done for separate iframes is a step towards sharding that @vojtajina mentioned.

@vojtajina
Copy link
Contributor

@huyz What you described IS a global state. It might be not accessible/visible, but it's still a global state.

@huyz
Copy link

huyz commented Mar 22, 2013

@vojtajina point taken, thanks.

@hakubo
Copy link
Author

hakubo commented Mar 27, 2013

Any Idea when this could be done ?
If not, could someone show me some pointers on where to look ?

@jperl
Copy link

jperl commented Mar 27, 2013

You could also run your tests using uitest.js. It loads up your application in an iframe.

@hakubo
Copy link
Author

hakubo commented Mar 28, 2013

This looks good!
I'll definitely look into it.

@vojtajina
Copy link
Contributor

It's not high priority now, so not sure when this gonna happen...

On Thu, Mar 28, 2013 at 2:17 AM, Jakub Olek [email protected]:

This looks good!
I'll definitely look into it.


Reply to this email directly or view it on GitHubhttps://github.com//issues/412#issuecomment-15575219
.

@vojtajina
Copy link
Contributor

I'm closing this as there is already an issue for sharding - splitting a big suite into multiple chunks, so that it is possible to parallelize the run (run the same suite on multiple browsers). #439

I don't think we need a fresh iframe per spec. It would make the run much slower. I still believe fighting global state is better ;-) So far I saw a few apps that wanted this so badly (per spec iframe) and it was always possible to refactor the code to not need it.

@fredericoschardong
Copy link

@hakubo did you end up coding this feature? I need this for a plugging I am working on, it would be good to not start from scratch. Thanks!

@hakubo
Copy link
Author

hakubo commented Jan 28, 2016

@fredericoschardong Unfortunately no. I have changed my stack in the meantime.

@bonitao
Copy link

bonitao commented Apr 17, 2016

+1, the proposed grouping syntax looks very nice.

@timdp
Copy link

timdp commented Oct 15, 2017

While it's not an ideal solution, I've written a Karma plugin that injects an iframe with a postMessage bridge into Mocha tests. It's called karma-mocha-iframes and there's also a demo.

@sabberworm
Copy link

I’ve tried to generify @timdp’s solution. The result is karma-iframes. It’s the same idea but should work with any adapter, not just mocha.

@timdp
Copy link

timdp commented Nov 14, 2017

@sabberworm Are you sure it's the same idea? From the readme, it sounds like you're running each test file in a separate iframe. My version runs each individual (Mocha) test in its own iframe to prevent each test from tainting the context for subsequent ones.

@bashmish
Copy link

bashmish commented Dec 9, 2018

This might be really helpful when testing custom elements (web components). For now there is only one single registry and no way to unregister an element. Though there is this idea to have scoped registries which does not have a concrete proposal yet.

This old discussion about the possibility to unregister didn't go further than pointing out that there are test frameworks which already run tests in iframes, which is the web-component-tester. But web-component-tester is also very limited and only allows running individual suites in iframes, not individual tests. I created a repo to show this behavior. Looks like the above mentioned karma-iframes is similar to the WCT behavior.

I can understand that this use case is not trivial. But maybe it is time to reconsider and reopen the issue at least to explore possible use-cases further.

@lancedikson
Copy link

I'm currently having the same situation with custom elements (Web Components) testing and struggling with setting up integration and unit tests, where each component can be registered only once and there's no way to unregister it. It produces very specific workarounds after all. Let's try to call active maintainers to re-check this issue and maybe re-open it because the problem is getting more and more common due to the growing popularity of WebComponents. @johnjbarton 🙏

@lancedikson
Copy link

One more hint for those, who suffer from the issue and don't want to use karma-iframes because of their drawbacks: I came up with a wrapping decorator for @customElement():

import { customElement } from 'lit-element';

function customElementDecorator(tagName:string): any {
  const registeredElement = window.customElements.get(tagName);

  if (typeof registeredElement === 'undefined') {
    return customElement(tagName);
  }
  return () => {};
}

@customElementDecorator('my-component')
class MyComponent {
// ...
}

Then you can place this decorator to some optional requirement in webpack, for example, and use it only during testing, like this:

const legitCustomElement = (NODE_ENV === 'testing') ? customElementDecorator : customElement;
export { legitCustomElement as customElement };

This workaround seems working for now, let's see how it goes when we have more tests. I hope there will not be any race conditions with registering elements from different parts of the code.

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