nativescript-cli: [socket.io.js] document.createElement is not a function

From @warren-bank on September 21, 2016 23:48

(edit: you can jump ahead to my final comment… to see the very minor code patch I would suggest be applied, which would fix this issue)

I was super pleased to discover that the karma test runner could be used in conjunction with the interactive debugger: tns test android --debug-brk

However, I did want to raise one small issue…

The application crashed because of an uncaught exception. You can look at "stackTrace" or "nativeException" for more detailed information about the exception.
com.tns.NativeScriptException: 
Calling js method run failed

TypeError: document.createElement is not a function
File: "/data/data/org.nativescript.ExampleProject/files/app/tns_modules/zone.js/dist/zone-node.js, line: 201, column: 25

StackTrace: 
    Frame: function:'JSONPPolling.doPoll', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 1085, column: 25
    Frame: function:'Polling.poll', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 1740, column: 8
    Frame: function:'Polling.doOpen', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 1684, column: 8
    Frame: function:'Transport.open', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 827, column: 10
    Frame: function:'Socket.open', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 248, column: 13
    Frame: function:'Socket', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 129, column: 8
    Frame: function:'Socket', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 55, column: 41
    Frame: function:'Manager.open.Manager.connect', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 4549, column: 17
    Frame: function:'', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/nativescript-unit-test-runner/socket.io.js', line: 4859, column: 12
    Frame: function:'ZoneDelegate.invokeTask', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/zone.js/dist/zone-node.js', line: 323, column: 38
    Frame: function:'Zone.runTask', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/zone.js/dist/zone-node.js', line: 223, column: 48
    Frame: function:'ZoneTask.invoke', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/zone.js/dist/zone-node.js', line: 391, column: 34
    Frame: function:'ZoneDelegate.invoke', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/zone.js/dist/zone-node.js', line: 290, column: 29
    Frame: function:'Zone.runGuarded', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/zone.js/dist/zone-node.js', line: 197, column: 48
    Frame: function:'', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/zone.js/dist/zone-node.js', line: 173, column: 30
    Frame: function:'java.lang.Runnable.run', file:'/data/data/org.nativescript.ExampleProject/files/app/tns_modules/timer/timer.js', line: 17, column: 13

the file nativescript-unit-test-runner/socket.io.js is pretty much littered with references to the DOM, and attempts to perform DOM updates.

in particular:

  • JSONPPolling.prototype.doPoll
  • JSONPPolling.prototype.doWrite
  • useColors
  • localstorage
  • various references to: navigator.userAgent

I haven’t done any digging into this. I’m not aware of the low-level details of precisely how socket.io is used by the test runner, or why the debugger would trigger additional polling, or why this error didn’t occur while either:

  • debugging “app/main.js”
  • unit testing “app/tests/*.js”

and only occurs while debugging these unit tests.

Maybe this is nothing more than pilot error (on my part)… I wouldn’t rule it out, though I don’t think that I’ve done anything wrong.

In any case, I just wanted to share my observations… in case somebody who knows the code and how things are glued together… might read this and mutter: “oh shoot, yep… easy fix”

Copied from original issue: NativeScript/nativescript-unit-test-runner#17

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 15 (11 by maintainers)

Most upvoted comments

Hi again,

I just took a quick peek at the repo. Here is the current version of the (above mentioned) file (before being transpiled to js). Looks like that line of code hasn’t been changed.

Hi,

Wish I could offer additional insight beyond the original thread of code/observations, but I haven’t used NativeScript in quite a while… so I’m not sure if this has been addressed/solved in the time since then.

As for a workaround, please refer to the 2 comments that precede yours. That’s what I ended up doing at the time, and it worked just fine. Specifically:

  • in file: nativescript-unit-test-runner/main-view-model.js
  • changed code to:
var socket = this.socket = io.connect(this.baseUrl, {
  forceBase64: true,
  jsonp: false,
  reconnection: false
});

I’m a big fan of what you guys are building. When I swing back around to doing mobile UI, I’m sure I’ll start using it again.

Sorry I couldn’t be more helpful.