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?
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:
I would recommend the book "Test Driven JavaScript Development" for further study
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With