Search code examples
qunitcasperjs

Run QUnit tests within Casper.js


My current unit tests are using QUnit and they are executed as described on the QUnit website. Basically all my tests are compiled to a tests.js and this is included in the index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>QUnit Example</title>
  <link rel="stylesheet" href="/resources/qunit.css">
</head>
<body>
  <div id="qunit"></div>
  <script src="qunit.js"></script>
  <script src="tests.js"></script>
</body>
</html>

So my question is, how can I use my existing unit tests inside Casper.js?


Solution

  • First inject QUnit, and presumably your tests into your website, just like you would jQuery (do this in the casper.start function). These files have to be absolutely located on your local machine to work.

    casper.page.injectJs('/path/to/qunit.js');
    casper.page.injectJs('/path/to/tests.js');
    

    You need to use the evaluate function

    casper.evaluate(function() {
        // this code is run in the context of your website
        doTests();
    });
    

    to execute your tests.

    You could write a function to return your test results and write them to the command line. Or, for the lazier among us, you could grab a screenshot with

    casper.capture('testResults.png');
    

    after the evaluate.

    I haven't tested this particular method, but I've been working with CasperJS recently and believe it to be a sound path towards what you're looking to accomplish.