Isolating tests for ES6?

15 views
Skip to first unread message

mat...@gmail.com

unread,
Nov 1, 2017, 8:34:22 AM11/1/17
to karma-users
[ I originally posted this before joining; sorry... it doesn't seem to have made it through moderation, so re-posting now I've joined—apologies for any duplicate. ]

This issue has come up in the past, but not, apparently, for a few years—and now seems more relevant with ES6, so I thought I would ask on a new thread.

I'm writing code in ES6, using `const` to declare some singleton functions, and `class` for the syntactic sugar classes in ES6.  Karma loads all of my source files (each class is in a separate file) and spec files into the browser at the same time.  This presents some problems:

 * Almost all of my objects use dependency injection, but the root of my API cannot, because it has to do all of the instantiation and wiring of the other objects in the system.  In order to test this root object in isolation I would have to make fakes/mocks/stubs for the objects that it refers to in hard-wired fashion in its code.

 * That would be easy in ES5, as I could override the declarations via the `window` object (although it would require some setup/teardown to ensure things are restored after the tests are run), but `const`/`let` variables, and class definitions, are not stored on `window`, so cannot be intercepted.

 * Ideally Karma would run each pair of (code, test code) in isolation—or, if performance is a serious concern, allow me to specify when I want to run /some/ of these in isolation.

A great solution to this to run some pairs of (code, test code) in separate iframes [Issue 412] was suggested back in 2013 but was since discounted in favour of an issue about sharding chunks of test suites across browser instances [Issue 439]. However the problem with the sharding approach is that it doesn't solve the isolation issue: sometimes you need each test (or at least, if performance is a concern, /some/ tests) to be run in an isolated environment.  Neither the original, nor sharding, issues were implemented within Karma, though there is a sharding plugin.

The iframes solution seems a lot lighter than the sharding one, and more suitable for tests that need isolation so that ES6 globals can be mocked.  Just wondering if anyone thinks it may be worth revisiting this now?

Karma has been very helpful to my project (thanks!) and being able to address this would make it absolutely rock.

best regards,


Matthew

Reply all
Reply to author
Forward
0 new messages