Script error (:0) when trying to run async test in mocha-phantomjs

2.4k views Asked by At

i'm trying to test a piece of asynchronous code but sadly enough i'm getting an obscure error code and i cant seem to figure out what the issue is. The test runs fine in the browser but running it in phantomjs results in:

Uncaught Script error. (:0)

The test is written as an requirejs module and has a dependency on another module. Like i said this works fine in the browser, and when doing none async tests everything works fine in phantomjs as well. I'm using phantomjs 1.9.12 and mocha-phantomjs 3.4.1.

define([ "csl" ], function( csl ) 
{  
   describe( "CSL", function()
   {
      it( "isLoggedIn", function( testCompleted )
      {
          csl.isLoggedIn().then( function( partner )
          {
              chai.expect( partner ).to.be.a( "object" );
              testCompleted();
          } )
          .fail( function( error )
          {
               testCompleted( error );
          } );
      } );
  } );
} );
1

There are 1 answers

0
Tom Clarkson On

That is the error that mocha produces when there is an exception in an async function, and chai.expect can throw an AssertionError.

You can reproduce the same error in a browser with a simple timeout:

    it("should fail async", function(done) {
        window.setTimeout(function() {
            "".should.not.equal("");
            done();
        })
    })

To fix it, you need to report the error via the callback rather than exceptions:

    it("should fail async with good error", function(done) {
        window.setTimeout(function() {
            if ("" == "") return done(new Error("Async error message"));
            done();
        })
    })

    it("should fail async with exception", function(done) {
        window.setTimeout(function() {
            try {
                "".should.not.equal("");
            }
            catch (e) {
                return done(e);
            }
            done();
        })
    })

The problem isn't with phantom itself (other than whatever is making the test fail) but the connection between the test runner and phantom makes everything asynchronous, triggering the mocha bug.