These are chat archives for canjs/canjs

22nd
Nov 2017
Viktor Busko
@Lighttree
Nov 22 2017 22:26

Hi guys,
I'm doing some investigation about how to test can.js (components) and trying to run Mocha test in Node environment.
Expected result that I'll be able to test view model that uses can-fixtures (or not it depends on requirements :))

But I have lots of issues when I'm trying to go this way. For example If i try to use can-fixture code of the test fails with:

can-fixture\xhr.js:15
var XHR = XMLHttpRequest,
          ^

ReferenceError: XMLHttpRequest is not defined

without can-fixture code of component that I'm trying to test fails with:

can-ajax\dist\cjs\can-ajax.js:22
var originUrl = parseURI(Global().location.href);
                                          ^

TypeError: Cannot read property 'href' of undefined

As far as I got the source of the issues is Node, because all the examples use testee as test runner which works in the browser itself and there is XMLHttpRequest object and Global() - window so we will have location.href.

So the question is.. is it possible to run tests in Node env. or this is bad idea in general and if I'm going to test can.js its better to use testee or analog with browser as env ?

Chasen Le Hara
@chasenlehara
Nov 22 2017 23:05
@Lighttree Awesome question. I’m curious, do you also SSR your app/components?
You could probably get your tests working with jsdom or other Node/browser polyfills.
I personally think that it’s better to test your code in as close of an environment as it will run, in this case running tests in the browser instead of Node.
Brad Momberger
@bmomberger-bitovi
Nov 22 2017 23:11
can-fixture doesn't work by itself in node environments because of the reasons you cite. You may be able to get it work with the XMLHttpRequest2 library we use in done-ssr, though: https://github.com/donejs/xmlhttprequest2
Most of the canjs libraries can work fine without polyfills. There are exceptions to this, though, and if you have other issues with HTML element class references, you may need to also include can-vdom which adds partially functional mockups of the global DOM objects to your global Node namespace
That said, I strongly recommend using testee and having it automatically spawn browser instances if you are going to deploy this code to browsers anyway. It will save you a lot of headache