0

I've been testing JavaScript code using unit testing frameworks like jasmine and Qunit. But all these testing framework works only at load time, but I want to initiate the test cases at run time, for instance I want to test an object's value on a button click like below test case in Jasmine,

 function btnClick() {
        var temp++;                  
        describe("Test Suite Inside Button Click", function () {
            it("To test true value", function () {
                expect(temp).not.toEqual(-1);
            });

        });
    };

How to run the test cases dynamically ?

Bharath
  • 91
  • 1
  • 3
  • 11

1 Answers1

0

Here is how you do it.

  • Invoke the jasmineEnv at run time and run the test
  • Note that I'm clearing out the reporter div just to clean up the output- which may not to necessary in your case.
  • My setTimeout is only to load the div onto the page
  • See it in action here

    var testFunc = function() {
      //this is optional- I'm just clearing the reporter out to run it on fiddle.
        $('.jasmine_html-reporter').remove();
        var jasmineEnv = jasmine.getEnv();
        describe('test', function() {
          it('sample test', function() {
            console.log('test ran');
            expect(true).toBe(true);
          });
        });
        jasmineEnv.execute()
    }
    
    setTimeout(function() {
        $('#myElement').click(testFunc);
    }, 0);
    
Winter Soldier
  • 2,607
  • 3
  • 14
  • 18