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

Add Support for dependency injection #1074

Closed
jsdevel opened this issue Dec 17, 2013 · 11 comments
Closed

Add Support for dependency injection #1074

jsdevel opened this issue Dec 17, 2013 · 11 comments

Comments

@jsdevel
Copy link
Contributor

jsdevel commented Dec 17, 2013

It would be really nice to support dependency injection. On the command line, you could add a flag for a provider module path. In a browser you could load a provider before your test scripts. It would probably look like angular's factory pattern.

mocha.factory('someDep', function(){return a;});

//later in my test
describe(function(someDep){
//tests here
});
@jsdevel
Copy link
Contributor Author

jsdevel commented Dec 24, 2013

Would this be a welcomed addition if I were to submit a pull request for review?

@rlidwka
Copy link

rlidwka commented Dec 24, 2013

what's wrong with require and a bunch of other module loaders?

@jsdevel
Copy link
Contributor Author

jsdevel commented Dec 24, 2013

Redundancy. Plus if I ever want to change the location of one of my modules I have to change it all over my entire project.

@tj
Copy link
Contributor

tj commented Dec 24, 2013

you could just use NODE_PATH and give the test modules precedence, or hack require(), definitely not something that should be in mocha IMO

@travisjeffery
Copy link
Contributor

i'm -1 too. enough to be a whole separate lib or something.

@rlidwka
Copy link

rlidwka commented Dec 24, 2013

you could just use NODE_PATH and give the test modules precedence

That's something discouraged afair.

@jsdevel, but yeah, you can use other dependency injection libraries with mocha or write your own. Not suggesting to copy angular decisions (it's turned out to be really bad library after all), but some amd.js loaders can help you with that task.

@jsdevel
Copy link
Contributor Author

jsdevel commented Dec 24, 2013

Thanks for the discussion guys! Nothing popped up on NPM. I can see the wisdom in keeping it in a separate lib.

@rlidwka Why do you think angular is bad?

@rlidwka
Copy link

rlidwka commented Dec 24, 2013

Why do you think angular is bad?

I have no love for client-side only frameworks, because they are slow and don't work in browsers where javascript is turned off.

But AngularJS deserves a special attention because it's what is usually called "insecure by design" (here's nice write-up). Template engine where code and content aren't strictly separated is definitely not a good one.

@ORESoftware
Copy link

DI would be a good replacement for the current --delay functionality which is hacky, no?

@jsdevel
Copy link
Contributor Author

jsdevel commented Feb 8, 2016

I could see DI saving a lot of reuiqre('../../../../bla') in tests though, considering that you could wire up mocks globally with DI.

it('should do something asynchronously', function(done, myDbMock) {
  sut(myDbMock).should.eventually.equal(/* user object */).and.notify(done);
});`

@ORESoftware
Copy link

yeah...saving require verbiage is not so useful, but I think if you could replace the --delay option (see mocha homepage) with DI/IoC that would very cool. The difference being the DI would inject dependencies that were sourced/initialized asynchronously. Basically, you could "require" dependencies that can be loaded synchronously, but any dependency that needs some asynchronous setup, could be loaded with DI. The only reason to do this, is for dynamic test cases. Meaning, a dynamic number of it()s, which currently is only possible using --delay. There is an open issue for this as of early Feb 2016.

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

No branches or pull requests

5 participants