5

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?

3
  • 4
    Please check out jsfiddle.net. It's a nice tool to play around with javascripting and even contains most javascrtip frameworks as well as you can enter html and some css too. You can also load your custom css/javascript libraries if you. Commented Apr 20, 2011 at 19:38
  • Not just the Java people likes testing ;) Commented Apr 21, 2011 at 1:31
  • jsfiddle seems to be really nice for exploring some code. Unfortunately i haven't found the time to play around with it in more detail yet. The thing is that it doesn't seem to help me with testing my code... Or did i miss something? Commented Apr 23, 2011 at 13:59

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hey Jani! Thanks for your answer. Which testing framework do you use?
Ah, and yes, i do use a XMLHttpRequest including an if-modified-since header.
I tend to use JsTestDriver in my projects, since it's quite easy to set up multi-browser testing with it, and tests can be ran from the command line which for me is a must have. It also integrates nicely with continuous integration stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.