Hacker News new | past | comments | ask | show | jobs | submit login

Agree. Especially in a language like Javascript. For testing -

  var myLib;
  if(process.env.NODE_ENV === "test") {
    myLib = require("../test/my_test_lib")
  } else {
    myLib = require("./my_lib")
  }
or similar (you can obviously tighten this code up, too; I'm just being very explicit). Tada, you're injecting your test stubs in place of the real library when running in test.

For the actual production use that a DI framework gives you, you can do similar, or manually set things up, or create a layer of indirection yourself, that only applies where you need it (say an IIFE that has the logic to determine which implementation to pull in, and return that function/module as appropriate).

But this is, as you say, largely tangential.




There's also stuff like rewire [0], which lets you change required values inside modules. Although every time I've tried it, I found it created more problems than it was solving.

[0] https://www.npmjs.com/package/rewire


Proxyquire - https://github.com/thlorenz/proxyquire - is a nicer way to solve many of the problems that people tend to use rewire for.

We use it to stub out imports in our unit tests and find it very easy.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: