Results 1 to 3 of 3

Thread: JsUnit -- test driven javascript? you bet!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366

    JsUnit -- test driven javascript? you bet!

    Ok, so to preface this post: I HATE JAVASCRIPT. If I never have to code javascript again it'll be too soon. Long story short, one of our front end guys built a modal dialog nightmare...er..i mean solution. Bottom line, if anything goes wrong with that code it's a frickin mess, even though it's javascript, it's so messy it's as good as a black box component. I'd rather not have to open it up.

    Well, today we had a problem in that code and it was buried deep. I went nuts tryin to find the error and still haven't found it; it's not a show stopper so it can wait until tomorrow. I got to thinkin though. If every method of the way was thoroughly tested, we wouldn't have to do much guess work as to where the problem was...cuz there wouldn't be a problem to begin with. So, before rolling my own testing javascript testing framework(i really didn't want to, cuz, have i mentioned that, I HATE JAVASCRIPT? anyway...), I set out on a google journey to find an existing one. Found one in about a second. JsUnit. I just loaded it up to take it for a test drive. Way easy. I downloaded the code from JsUnit. Wrote a stupid hello world type test in about a second, and had 'tested javascript code' - right out of the box. Anywho, no installation necessary, just download and extract the files from the zip,here's my stupid employee example(if it's not obvious, all of the tests are prefixed with 'test'):
    PHP Code:
    <html>
        <
    head>
            <
    script language="JavaScript" type="text/javascript" src="../app/jsUnitCore.js"></script>
            <
    script language="JavaScript">
            function 
    EmployeefirstNamelastName )
            {
                
    this.FirstName firstName;
                
    this.LastName lastName;
                
    this.FullName getFullName;
            }
            function 
    getFullName()
            {
                if( ( 
    this.LastName == undefined || this.LastName.length == ) || 
                    ( 
    this.FirstName == undefined || this.FirstName.length == ) )
                {
                    return 
    undefined
                
    }
                else
                {
                    return 
    this.LastName ", " this.FirstName;
                }
            }
            function 
    testEmployeeFirstName()
            {
                
    emp = new Employee"Chris" );
                
    assertNotNull"emp should not be null"emp );
                
    assertEquals"FirstName should match the constructor""Chris"emp.FirstName );
            }
            function 
    testEmployeeLastName()
            {
                
    emp = new Employee "Chris""Carter" );
                
    assertNotNull"emp should not be null"emp );
                
    assertEquals"LastName should match constructor""Carter"emp.LastName );
            }
            function 
    testEmployeeFullName()
            {
                
    emp = new Employee "Chris""Carter" );
                
    assertNotNull"emp should not be null"emp );
                
    assertEquals"FullName() should be 'LastName, FirstName'""Carter, Chris"emp.FullName() );
            }
            function 
    testEmployeeExpectUndefined_getFullName()
            {
                
    assertUndefined"getFullName() should return undefined if called directly"getFullName() );
            }
            </
    script>
        <
    body>
            <
    form>
                <
    input type="button" onclick="init()" value="Employee">
            </
    form>
        </
    body>
    </
    html

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I'll agree with you that Javascript is a major pain. I have grown acustomed to it thanks to dealing with it every hour of every day.

    But before my recent wisdom, for about a week, we would type in the JS and just hope it worked.

    We then realized you could debug step-thru your js code IF internet explorer debugging is enabled.

    You can set breakpoints, return parameters, and inspect variable values using the command window of VS.NET or mouse hover. Setting the next execution instruction doesn't seem to work as well, but it sure has been a god-send since that first week.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Ya, that is a pretty cool feature. One thing I've noticed though with writing the xUnit tests, is that after awhile I notice that I never have to step through code...ok, maybe once in awhile, but probably 99% of the time there's I find no need to step through using the debugger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width