Open r002 opened 3 years ago
Interesting. I sparked a TDD discussion on Twitter and someone mentioned that they found writing tests for JS difficult. I hadn't looked into it, so I didn't know what tools were out there to conduct JS testing.
@anitabe404 Yeah-- up until this week, I was most recently predominantly writing tests in Go
and didn't have much formal JavaScript
testing experience either. So I've been trying to figure out the JS
landscape. It's weird because in Go
all testing is just built into the standard library. So there isn't any need to seek out third-party testing frameworks like the way it works in JS
. I poked around the internet and found Jest
, Jasmine
, Chai
, and Mocha
. They all have various affiliations (like "good for React" or "good for Angular") but from what I read, at least in the time I spent, Jest
seemed like a well-liked framework that also worked well with just vanilla JS
. (Also, other devs I follow seem to like it so that was encouraging!)
TDD
and different testing paradigms is a total world unto itself. 🙂 In my own experience, a lot of what specific paradigm you ultimately use is actually driven by team culture-- like if it's a TDD
shop you eventually land at, there's just a straight-up process for how all developers develop. Ie. You write the failing tests first, then the code, then pass all the tests. Or: Every time there's a PROD fix, you write a test that verifies that exact breakage can't ever reproduce in the same way again. Etc. Like-- the team has a process; that's the process all peer-code reviews are checked against; and so it's very clear and everyone just follows it. Which is good! 🥳
(Or-- to share a horror story from one of my past lives in my more junior days: The team you land on doesn't write tests at all! In that case, I ended up also not writing tests either. Testing was always a series of manual GUI checks we did every time we did a new release. As you might surmise-- eventually this grew incredibly onerous... oh, how we suffered... 🤦♂️😤)
When I've been on my own though, I've been more lax with my tests. I've had the luxury of developing solo so there's that. But I definitely also still do write tests because the idea is to be able to get one's code to a state --especially once it starts getting larger-- where you can always make changes confidently. So you can move fast and not break things. ☺
@r002 Wow, no tests sounds like an absolute nightmare. And yes, a lot of it is dictated by the team/group you're in. There were some pretty strong opinions against TDD. I'm not a TDD purist; I can see some scenarios where writing tests after the code is written could be okay. I think going out without automated tests is too extreme. I've been taking a TDD approach to my project and I enjoy it. It forces me to think. Although there have been moments where I coded first to get a better understanding of what I was trying to do before going back and writing/updating tests.
But yea, I got a few people going on Twitter. 😂
But yea, I got a few people going on Twitter. 😂
LOL 😂
It forces me to think.
Totally this!! One of the absolutely best parts about writing tests is that it forces you to properly think about encapsulation and all the different/proper/useful ways to break apart your code into what abstractions. Totally 100% agree! Writing tests is the way! 😄
Friday
Tasks
Starter Repo
template inGitHub
Frontend Starter
template(no-auth)
OAuth
andAPI Key
Jest
testing templatesBackend Starter
templateGCF CORS proxy
fails
succeeds
succeeds
Write basic code that consumes from public APIVanilla (No Auth)WithOAuth
andAPI Key
WriteJest
testing templatesGoogle App Scripts
starter template projectCase #1: This fails when we try to directly query the resource because its header is missing
‘Access-Control-Allow-Origin’
:Case #2: This succeeds because we use a
CORS proxy
to add‘Access-Control-Allow-Origin: *’
to the response headers:Step 1:
frontend JS
'Access-Control-Allow-Origin: *'
), resort to CORS proxybackend JS
Google App Scripts
Step 2:
Step 3:
/Chai
Jest
/? https://www.theregister.com/2021/01/14/javascript_survey_jest_webpack_on/Mocha
References