Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript testing

I'm currently working on my first little Javascript project, which in general is a little script that drives a Thunderbird extension. As I'm a Java programmer, I like to test my code in order to keep it maintainable and to check if it's working as expected.

The thing I want to test is a simple script, which basically fetches an URI, connects to the server, downloads a zip file and finally extracts to the filesystem it. I don't use Javascript - "classes", I just have these 2-3 functions, assembled in a main function.

I would like to mock up the server in order to test the script's behaviour with different server responses, corrupted files and so on.

Now my problem is that i don't really know how to test in javascript. I tried to get an overview of the available testing frameworks and finally decided that Jasmine is the one i want to use, because it seems to be the most mature one out there.

I'm perfectly fine with unit tests in Java and consider myself to be an average test engineer, but it's hard for me to gain access to the whole script testing thing.

Has anybody good advice how to do this? Maybe an useful tutorial or a good example?

like image 497
Patrick Avatar asked Nov 15 '22 00:11

Patrick


1 Answers

The approach is probably the same you would take in Java: Mock out the class/function which fetches the data.

I'm assuming you're using XMLHttpRequest, so the simplest way to do this would be something like this:

  • Have a function which performs the request and calls back with the data
  • Provide a way to inject this into your code or for it to be replaced with the mock
  • Replace the data fetching function with a mock function which just calls back with your dummy data

I would recommend the book "Test Driven JavaScript Development" for further study

like image 119
Jani Hartikainen Avatar answered Jan 04 '23 21:01

Jani Hartikainen