How To Start Writing Unit Tests #programming #javascript #webdevelopment

preview_player
Показать описание
Unit tests should run quickly and test some in-between logic. In the context of developing a social media application, it's unnecessary to manually navigate a browser, log in, and create a new post just to verify the functionality of a hashtag detection feature. Put that logic in it's own function and unit test it. Then the e2e tests exist, but are slow and run less often, maybe only on a test server when you push your feature branch.

🔗
Рекомендации по теме
Комментарии
Автор

I was caught off guard when recording, because I thought my code worked. It wasn't until I ran the tests that I realized there was an issue. So I did the first thing that came to mind to get the tests passing. I might then go back and refactor my code to make it better, and my tests would tell me if I messed anything up.

SamMeechWard
Автор

This is excellent way to getting to tests which most people usually skip

SpongeBOBat
Автор

Preach!
Do this whenever you use the word "should" in your internal dialog. This regexp should match valid filenames => *does* it though? Extract and write a test.

stevecarter
Автор

Great tip! That's the reason why I love you man.

ashuzon
Автор

I’m a junior and at work we don’t write tests so I’m trying to figure it out outside of work and this video is quite helpful 👍

szmr
Автор

I strongly urge anyone considering this to think about it before doing what was shown here, you can extract that function for readability but I recommend keeping the test at a higher level e.g entry point of the API or what ever is your main function.
This gives you flexibility to refactor between boundaries without having to update loads of tests. Only write tests like this in isolation if that function is being reused elsewhere or when that entry point is growing legs you could then choose cleaner abstractions not test per function. A repo with a test for every function in isolation would be a massive pain in the behind.

JT-vgwk
Автор

Is this from a stream, a video, or somewhere else?

mohamadybr
Автор

That's actually a useful tip. Btw, could you please tell me how you opened that command window for copilot? I know how to add comments and make it work but giving it instructions is really useful. I don't know how to open that window to give instructions. 😅

anirudhdhsinhjadeja
Автор

You forgot to mention that you need to also implement type-checks at the beginning of each function and also test that unless you are running on bun because typescript has no type-safety at all, those types are just nice hints, nothing to depend on.

JanBebendorf
Автор

Nice.

One question, I get surprise for the speed of bun: is your video edited not showing the real speed or is that fast?

tresvecesno
Автор

Great tip!!! Thanks for sharing!
What's your theme??

MaxBertta
Автор

could you share the github? I have struggle when try to create integration unit test

IkromAuliaFahdi
Автор

Stop writing tests the conventional way! Start using ATDD and Cucumber, especially for repetivite stuff that fits in a table, like string processing.

der.Schtefan
Автор

Testing? But that's what my users are for 😂

LeCigareVolant
Автор

I am so happy I don't need to touch TS anymore. Python RULES!

James-lsk
Автор

Is it really necessary to do unit tests where DB and API calls are mocked before doing integration tests where these are not mocked?

Anto-mipn
Автор

You still need to test the rest. The whole idea is max coverage and not selective coverahe

pantherdddjvdgx
Автор

How do we know which section of our codes to unit test ?

devonchia
Автор

Nice, but i think when you start, begin with a test, and let copilot write your code. You define what it could do, and that is the test, code is less important than the test.
Your coworker can refactor or write the code if you have the test.
If you have only the code, what was expected that it should do?
And then you also do TDD.

PieterWigboldus
Автор

Why do we put up with js/ts its disgusting

billyhagood