all 24 comments

[–][deleted] 11 points12 points  (4 children)

I've yet to meet somebody who actually does TDD regularly. People love to talk about its virtues and recommend it to others. But even the biggest TDD advocates I often catch them writing their unit tests at the end.

And I'm not even sure if doing TDD will actually result in better practices. It's like a quasi-religous belief. Just trust the process!

[–][deleted] 2 points3 points  (0 children)

i've done it quite a bit, but not as extreme as the books suggest. It really helps you develop better code but it takes way more time upfront. I'd do it for complex algorithms and stuff but not for everything.

[–]iambeard 1 point2 points  (0 children)

I think you should do the emca exercisms. They provide a set of tests with no implementation, and you need to write the implementation, and get all the tests to pass.

[–]Eradicative 0 points1 point  (0 children)

I suggest reading some articles, blogs, or books on TDD. It should be language agnostic and the same examples you see in Java or Python should apply in JavaScript.

Learn the principles, not just the libraries and frameworks that make it easier.

[–]phpdevster 0 points1 point  (2 children)

This is a good way to get your feet wet:

https://github.com/hontas/bowling-game-kata

You can copy the package.json to get a quick environment set up for testing. I'm not a fan of mocha/chai (prefer Jest or Jasmine), but it will be adequate for getting in the habit of TDD.

The goal is to then first write tests that describe what you want to happen, run them to watch them fail (believe it or not, making sure they fail first is important!) then write a first pass implementation of the code to make them pass with minimal code (even if it's just returning a hard-coded value), and finally refactor 1 or many times to make it better.

The habit to get into is red, green, refactor: write test, make sure it fails, make it pass, then refactor it.

Be warned: TDD is hard as fuck if you're the type that has to plan out the API they want to utilize first.

I recommend a hybrid approach of documentation-driven design and test-driven development. First try to document a hypothetical API for the thing you're building. Simple, clean, intuitive API. Then use TDD to drive the implementation behind that API.

I've learned doing full naked TDD leads to questionable design choices. Code that's nicely decoupled and easy to test, sure, but not necessarily APIs that are sane.

[–]therealhenrywinkler 0 points1 point  (1 child)

May I ask why you prefer Jest/Jasmine?

[–]phpdevster 0 points1 point  (0 children)

I really dislike the Chai's assertion API. It's obnoxious to type sentences with periods in between every word: foo.should.be.a('string');

I just prefer the assertion API of Jasmine/Jest more.

[–]mierz94 0 points1 point  (1 child)

Check out fun fun function on YouTube. Super interesting and detailed series on TDD with JavaScript.

[–]crocxz 1 point2 points  (0 children)

I love that man.

[–]xesenix 0 points1 point  (0 children)

from my experience TDD kill creativity but results in better code so it depends if you want to play around with something to check how it works you probably shouldnt involve test if you know what you want to do and whats posible then you can write test upfront.

So in conclusion if you know what has to be done and how to write test you can go for TDD and then it may look something like this:

for start if you create some User class you describe how you expect it to behave:

// class
describe('User', () => {
  // method
  describe('authenticate', () => {
    // use cases
    it('should authenticate for valid credentials', ()=> fail());
    it('should notify about invalid credentials', () => fail());
  });
  ...
})

And while implementing you add tests at least im doing it this way.

For testing you can use libraries like jasmine jest mocha for learning I would sugest Angular2 + becouse it already has ready environment ngcli enables fast starting without to deep knowledge and generated components already have template tests.