test framework for arcgis javascript

2324
7
01-24-2018 10:32 AM
mingleidi
New Contributor II

We use arcgis 4.5 and webpack, now I need to choose a test framework for the project, could anyone please advise which one is good, like Mocha or Jamine, or anything else?

we had trouble to use intern with webpack so it's not an option.

Any advises are much appreciated!

Thanks,

Benjamin

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor

Take a look at the blog of Rene Rubalcava. He's written a couple of articles about using Intern

Using Intern with ArcGIS API for JavaScript - odoenet 

Hello Intern 4 - odoenet 

mingleidi
New Contributor II

Thanks Ken, the Hello Intern 4 - odoenet is helpful, unfortunately its code is not published, I spent some time to follow the video and modified my project, but got an error when run intern:

Cannot find module 'node_modules/@dojo/loader/loader.js'

Do you know where to get the code from Hello Intern 4 - odoenet?

Updated:

After added dojo modules to package.json, the above error is resolved, but when intern is running, the following error shows up:

> intern

Listening on localhost:9000 (ws 9001)
Tunnel started

‣ Created remote session chrome 63.0.3239.132 on Windows NT (27c194eca1d54879f40f9e1c609ccb3f)
Suite chrome 63.0.3239.132 on Windows NT FAILED
Error: scriptError
at makeError <node_modules\dojo\dojo.js:125:15>
at HTMLScriptElement.<anonymous> <node_modules\dojo\dojo.js:1752:21>

Thanks again,

Benjamin

0 Kudos
ReneRubalcava
Frequent Contributor

We recently released a new example app, Maps App JavaScript.

Source here.

This app is using Intern 4 for unit testing.

It uses a webpack config specifically for running tests.

Hope that helps a bit.

MatthewLewis5
New Contributor II

Im trying to use intern 4 with dojo 1 and jsapi 3.23. However i receive the same issue as highlighted above

Suite chrome 64.0.3282.186 on Windows NT FAILED
Error: scriptError  at makeError <node_modules\dojo\dojo.js:125:15>
at HTMLScriptElement.<anonymous> <node_modules\dojo\dojo.js:1752:21>
TOTAL: tested 1 platforms, 0 passed, 0 failed; suite error occurred

The WAB still utilises 3.23 for 2D apps so it would be good to use intern with 3.x until the framework catches up.

I expect its something to do with the dojo loader but i'm not sure how to resolve it.

the following is my intern.json

{
     "loader": {
          "script": "dojo",
          "options": {
               "packages": [{
                    "name": "dgrid",
                    "location": "https://js.arcgis.com/3.23/dgrid"
               }, {
                    "name": "dijit",
                    "location": "https://js.arcgis.com/3.23/dijit"
               }, {
                    "name": "dojo",
                    "location": "https://js.arcgis.com/3.23/dojo"
               }, {
                    "name": "dojox",
                    "location": "https://js.arcgis.com/3.23/dojox"
               }, {
                    "name": "put-selector",
                    "location": "https://js.arcgis.com/3.23/put-selector"
               }, {
                    "name": "util",
                    "location": "https://js.arcgis.com/3.23/util"
               }, {
                    "name": "xstyle",
                    "location": "https://js.arcgis.com/3.23/xstyle"
               }, {
                    "name": "moment",
                    "location": "https://js.arcgis.com/3.23/moment"
               }, {
                    "name": "esri",
                    "location": "https://js.arcgis.com/3.23/esri"
               }]
          }
     },
     "environments": [{
          "browserName": "chrome",
          "chromeOptions": {
               "args": ["headless", "disable-gpu"]
          },
          "fixSessionCapabilities": false
     }],
     "browser": {
          "suites": "./widgets/OperationManagement/tests/testSpec.js"
     }
}

the following is the test

import timeUtils = require("../../modules/timeUtils");

const { assert } = intern.getPlugin("chai");
const { registerSuite } = intern.getInterface("object");

registerSuite("timeUtils", {
 "create new"(): any {
 assert.doesNotThrow(() => new timeUtils());
 }
});

any help would be appreciated

0 Kudos
RobSantos
New Contributor

Did you ever find a solution to this?  I am also trying to get Intern to work with a WAB project and am running into the exact same problem.

0 Kudos
MatthewLewis5
New Contributor II

My solution was to use the Yo builder here - Which scaffolds out the widget this had intern in it last time i attempted it.

 GitHub - Esri/generator-esri-appbuilder-js: Yeoman generator to help customize Esri's WebAppBuilder 

0 Kudos
RobSantos
New Contributor

Right after I posted this I was able to get this working.  Just to post in case someone else runs into this problem, the error I got was:

Suite chrome 67.0.3396.87 on Windows NT FAILED
Error: scriptError
at makeError <node_modules\dojo\dojo.js:125:15>
at HTMLScriptElement.<anonymous> <node_modules\dojo\dojo.js:1752:21>

In short, the problem is caused because Intern fails to resolve a path it was pointed to.  I ran into this when I gave the "suites" property a specific file with an extension (should be "myTest", not "myTest.js") and when Intern could find a copy of dojo for use during testing, which is separate from the dojo library used for the Intern loader (further explained below).

This problem may appear to be caused by the dojo Intern loader, but actually is not.  You can test this by creating and running a blank dojo loaded test, one with no AMD dependencies: 

define([
 
], 
function (
 
) {
 var registerSuite = intern.getInterface("object").registerSuite;
 var assert = intern.getPlugin("chai").assert;


 registerSuite('Component', {
 tests: {
 'Is intern dojo working?'() {
 assert.ok(true);
 }
 }
 });
});

As is described in this article, the dojo library used by the Intern dojo loader in your intern.json file is distinct from the dojo library used by your injected packages in tests (Dojo FAQ: How can I run Dojo tests locally with Intern? - Blog | SitePen ).  So, you must be sure that the path you provide in the "packages" intern.json object for dojo is accurate from the directory structure where intern is executing, not from the directory structure that might be used to resolve dojo in a deployed WAB instance.  For most people, this would be in node_modules but things can get tricky in Web App Builder.  In my case, that meant manually copying the arcgis API libraries (including dojo) into a directory where they would be available to intern:

myproject/
-app/
--arcgis-js-api-3.24/
---dojo/
---esri/
---and others/
-tests/
--tests go here
-src
intern.json

And my intern.json (my problem was caused by the "dojo" entry not actually pointing at this local copy of the dojo library, which caused tests with an injected Class with dojo as a dependency not to to be able to retrieve dojo):

{
 "suites": ["tests/myTest"],
 "tunnelOptions": {
 "drivers": ["chrome"]
 },
 "loader": {
 "script": "dojo",
 "options": {
 "async": true,
 "tlmSiblingOfDojo": false,
 "has": {
 "extend-esri": 1
 },
 "packages": [{
 "name": "dojo",
 "location": "apps/arcgis-js-api-3.24/dojo"
 }, {
 "name": "dijit",
 "location": "apps/arcgis-js-api-3.24/dijit"
 }, {
 "name": "dojox",
 "location": "apps/arcgis-js-api-3.24/dojox"
 }, {
 "name": "esri",
 "location": "apps/arcgis-js-api-3.24/esri"
 }, {
 "name": "src",
 "location": "src"
 }, {
 "name": "tests",
 "location": "tests"
 }, {
 "name": "apps",
 "location": "apps"
 }]
 }
 },
 "environments": [{
 "browserName": "chrome",
 "fixSessionCapabilities": "no-detect",
 "chromeOptions": {
 "args": ["headless", "disable-gpu"]
 }
 }],
 "coverage": false,
 "debug": false
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I hope this helps someone who runs into a similar issue getting unit tests running in WAB.

0 Kudos