amwmedia/infect.js

Allow for Creation of Child Contexts, with Inheritance

Opened this issue · 3 comments

One nice feature to have would be the ability to make contexts that can produce child contexts.

One existing implementation of this idea can be found here: https://github.com/Wolfy87/venturi

The use cases for this feature include:

  • setting up overrides for testing
  • setting up different contexts for different sections of an application

I'm not sure I understand the benefit here. If I want an override for testing, wouldn't I just run infect.set at the top of my test script and set my stubbed data loader or mock values there?

I was thinking of a scenario like this:

  1. You start with some existing infect wiring:
infect.set('foo', Foo);
infect.set('bar', Bar);
...
  1. For one test, you want to override one of the infectables.
describe('my test', function() {
  it ('should test foo with a stubbed bar', function() {
    var testContext = infect.createChild();
    var testContext.set('bar', StubbedBar);
    var foo = testContext.get('foo');
  })
 it ('should test bar with a stubbed foo', function() {
    var testContext = infect.createChild();
    var testContext.set('foo', StubbedFoo);
    var bar = testContext.get('bar');
  })
});

If we don't have a test context, then I think we would need to manually reset each stubbed component after each test.

Does that help show what I was thinking?

ok, I know it's a year later, but I found this email in my inbox again and it got me thinking about it. Would the ideal solution just be to have your test suite store a copy of the stubbed infectable objects and then write a small function to "restore" their state to the copies? As I see it, your main issue is that your test may mutate your objects, maybe I'm misunderstanding your issue though.