2
votes

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
Does it appear when you use PhantomJS 1.9.7-15? If yes, this might be related since PhantomJS 1.9.12 is an npm package that contains PhantomJS 1.9.8.Artjom B.
I tried with npm package PhantomJS 1.9.7-15, same issue. When using the latest version from NPM 1.9.12 the issue remains but the status of the test changes to pending instead of failing. Although still throwing the "script error (:0) error. So this means that i should a version of phantom prior to 1.9.8?Raymond de Wit
In my case serving all files from the same origin helped. Try not to use CDNs or file: scheme. Instead serve all scripts from a single server. Also see here: github.com/mochajs/mocha/issues/165Tad Lispy

1 Answers

4
votes

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.