DEV Community

Alex Romanova
Alex Romanova

Posted on

Testing is fun :o

Testing was always something I kinda looked upon. Didn't exactly know what it involved, but I imagined it being repetitive and in no way fun or creative. Well, here comes along this lab...

I first tried using some cool and different tool, however I got lost pretty fast in a new framework with a completely new concept. So I went back to jest that was shown in the tutorials. It was extremely helpful to see actual implementation and a walkthrough. I usually get lost in specific syntax and connecting everything together, I mean, even with a tutorial I managed to miss some things that would result in it not working. For example, naming your mocks folder specifically __mocks__ and in no other way. Had my mocks be undefined for a while until I noticed that error...

Anyways, testing. It's actually kinda cool. I'm pretty lost with using new tech, like I couldn't fully figure out creating mock files, but I managed to make some things work. And there could be so much more testing I could do... there should be! It really makes you improve your code in ways that you usually don't think about. I like that a testing process has a structure where you can at least concentrate on a specific thing not working, as opposed to creating an idea from scratch without any direction to follow. It is definitely something I now consider doing.

Using Jest

That part's pretty simple. Follow either a guide on their website, or follow my instructions here. Run

npm install jest
Enter fullscreen mode Exit fullscreen mode

in your project's directory.

Add this to package.json:

{
  "scripts": {
    "test": "jest"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create a file, usually with a name like "theFileYouWannaTest.test.js". Make a test of your choice, you can follow one in the guide, but I'd recommend you test your actual function, pick something easy.

Add a destination file of the function of your choice:

const myFunction = require('./functionsFile');
Enter fullscreen mode Exit fullscreen mode

Make a test with following syntax:

test('Describe what is supposed to happen', () => {
  expect(myFunction(passArgument)).toBe(correctReturnValue);
});
Enter fullscreen mode Exit fullscreen mode

To finally run the test, do

npm test
Enter fullscreen mode Exit fullscreen mode

EZ

Using the newly acquired knowledge

When I was searching for things to contribute to during hacktoberfest, I was very interested in finding something to do with Go. However, all I've found was too difficult for my knowledge. I have found a person with an app he made himself, but his response really made me nope out.

Tests? Yea, idk what that is.

After this lab I actually know what I'm doing. Well, I have an idea at least. I have a roadmap, a path to learn it. I went back to contact the guy, it seems it's still available to contribute to!

At least now I have my second contribution for release 0.3 planned out. I'm very unsure if I will actually be able to do it, since it is in GO and he did say tests have to be good... yea, idk about that... But I will definitely try!

Top comments (0)