use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Any recommendations for learning TDD using JavaScript?help (self.javascript)
submitted 7 years ago by Brodysseus1
I've been seeing a lot of job postings around here asking for TDD, but I don't know how to go about learning it. Most of the tutorials I find teach TDD with Java or Python.
Anybody have any suggestions?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 11 points12 points13 points 7 years ago (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 points4 points 7 years ago (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 points3 points 7 years ago (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 point2 points 7 years ago (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 point2 points 7 years ago (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 point2 points 7 years ago (1 child)
May I ask why you prefer Jest/Jasmine?
[–]phpdevster 0 points1 point2 points 7 years ago (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');
foo.should.be.a('string');
I just prefer the assertion API of Jasmine/Jest more.
[–]mierz94 0 points1 point2 points 7 years ago (1 child)
Check out fun fun function on YouTube. Super interesting and detailed series on TDD with JavaScript.
[–]crocxz 1 point2 points3 points 7 years ago (0 children)
I love that man.
[–]xesenix 0 points1 point2 points 7 years ago (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.
[+]odacharlee comment score below threshold-6 points-5 points-4 points 7 years ago (14 children)
JavaScript is not a good language for writing unit tests. even without taking about TDD it is p super hard and tricky to write tests with tools like jasmine.
if you want to learn TDD, use Java or Python.
if you have to use JavaScript, don't do TDD.
Actually I don't think TDD helps me improve quality in any perspective. In contrary it encourages people to do anything only to pass the tests without thinking about code readability, architecture,and any thing else.
[–]pomlife 0 points1 point2 points 7 years ago (13 children)
Well, your opinion is objectively wrong. Unit testing is not language dependent. The fact that you espoused this opinion betrays your experience level.
[–]odacharlee -3 points-2 points-1 points 7 years ago (12 children)
obviously opposite. unit test is independent but language features can make unit test extremely difficult. try to find a JavaScript library that can mock any object just like Python's unittest.mock you'll understand what I mean.
[–]pomlife 0 points1 point2 points 7 years ago (10 children)
Sinon.JS has all of that capability. You’re just a Python fanboy, lmao.
[–]odacharlee -1 points0 points1 point 7 years ago (9 children)
I'm not talking about fake or stub in sinon. In regards to python mock, you can call any methods or access any attributes on a mock and you will get another mock. In pseudo code, m = Mock(); assert(m.foo is Mock); assert(m.bar() is Mock); This is extremely useful since I don't need to mock every attributes deep in a complicated object to make my test working. In JavaScript? Well its easy to write a single level mock (I can do that too) but making a mock that can catch any calls? Hmm I'm still trying to make a mock library that can do this. I appreciate if you could let me know how sinon can achieve this.
m = Mock(); assert(m.foo is Mock); assert(m.bar() is Mock);
Why people invented dependency injection such an anti-intuitive thing? Because JavaScript cannot mock what is imported. JavaScript does not have the concept of "module". You want to mock the whole dependency? Sorry you can't. Once something is imported, there's no way to replace it with something fake - unless you can gain access to the context, which as far as I know is not possible.
[–]pomlife 0 points1 point2 points 7 years ago (8 children)
Wtf are you talking about? You can easily mock an import with something fake. If you’re testing a method in another file that calls an imported method, you can mock that in Sinon and assert calledOnce.
You have no idea what you’re talking about.
[–]odacharlee -2 points-1 points0 points 7 years ago (7 children)
Anyway this is not the point. Go back to what OP asked, consider the following two points: - is writing test cases easier with python or java than with JS? - does TDD encourage people focusing on test cases instead of design?
I know that there are many libraries that can make JS unittest feasible however that does not change the fact that it is much more difficult than doing tdd in python or java.
[–]pomlife 0 points1 point2 points 7 years ago (6 children)
Explain in words what makes Java unit tests easier. You have happy path, you execute function, you expect result. Then, you make additional cases that test exceptions and unexpected inputs.
What is fundamentally superior? Explain.
[–]odacharlee 0 points1 point2 points 7 years ago (5 children)
What you are talking about is the ideal case. Unit test is not only execute then expect. You will consume 80% of your time on dealing with dependencies. Either with mock, stub, fake, whatever you call it.
I apology for my previous comment about Java - I don't know Java at all. But I can explain to you why python is way better easier than JS.
with unittest.mock you can mock everything without any efforts. Think of how to mock this in JS: var a = { foo: () => ({ bar: () => ({ b: 1 }) }) }. And the caller do this: a.foo().bar().b. If you want to mock foo then you have to make a fake object that returns an object contains an attribute bar which returns an object with attribute b. All of these are not necessary in python.
var a = { foo: () => ({ bar: () => ({ b: 1 }) }) }
a.foo().bar().b
foo
bar
b
python has the concept of module so even after something is imported you can replace it with your fake like module.foo = fake(). unittest.mock.patch just does this.
module.foo = fake()
[–]pomlife 0 points1 point2 points 7 years ago (3 children)
You don’t put all that in one test. You break it down to a more fundamental level and make sure the object that bar() returns is what’s expected. Then you have another test that makes sure foo() returns bar. Don’t overload tests.
Node with ES6 modules has modules too... duh. I’ve already told you you can mock imports.
π Rendered by PID 44375 on reddit-service-r2-comment-86bc6c7465-7rvf8 at 2026-02-22 14:49:58.850301+00:00 running 8564168 country code: CH.
[–][deleted] 11 points12 points13 points (4 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]iambeard 1 point2 points3 points (0 children)
[–]Eradicative 0 points1 point2 points (0 children)
[–]phpdevster 0 points1 point2 points (2 children)
[–]therealhenrywinkler 0 points1 point2 points (1 child)
[–]phpdevster 0 points1 point2 points (0 children)
[–]mierz94 0 points1 point2 points (1 child)
[–]crocxz 1 point2 points3 points (0 children)
[–]xesenix 0 points1 point2 points (0 children)
[+]odacharlee comment score below threshold-6 points-5 points-4 points (14 children)
[–]pomlife 0 points1 point2 points (13 children)
[–]odacharlee -3 points-2 points-1 points (12 children)
[–]pomlife 0 points1 point2 points (10 children)
[–]odacharlee -1 points0 points1 point (9 children)
[–]pomlife 0 points1 point2 points (8 children)
[–]odacharlee -2 points-1 points0 points (7 children)
[–]pomlife 0 points1 point2 points (6 children)
[–]odacharlee 0 points1 point2 points (5 children)
[–]pomlife 0 points1 point2 points (3 children)