N
N
Nikolai Stupak2016-01-28 18:37:59
JavaScript
Nikolai Stupak, 2016-01-28 18:37:59

How to run unit tests in Dojo?

Help with running tests in Dojo.
I have a project with the following structure:

project_root
|--src
    |-- main
         |-- webapp
              |-- index.html
              |-- js
                   |-- dojo
                   |-- dijit
                   |-- dojox
                   |-- util
                   |-- jla
                   |-- test
                        |-- dojo-test.sh
                        |-- dojo-test-config.js
                        |-- jla
                             |--test.js

The DOH documentation says that you can run tests from both the browser (using ${project_root}/src/main/webapp/js/util/doh/runner.html ) and the command line (using Rhino using $ {project_root}/src/main/webapp/js/util/shrinksafe/js.jar ). I am trying to run the test file ${project_root}/src/main/webapp/js/test/jla/test.js from the command line with this command:
> cd ${project_root}/src/main/webapp/js/test
> ./dojo-test.sh -m jla/app/test

The dojo-test.sh script code is as follows:
#!/bin/bash

log() {
    printf "    %25s:  %s\n" $1 $2
}

format() {
    printf "    %25s:  %s\n" $1 $2
}

PROJECT_DIR='../../../../../'
log 'PROJECT_DIR' $(readlink -f $PROJECT_DIR)

SOURCE_DIR=$PROJECT_DIR'src/main/webapp/js'
log 'SOURCE_DIR' $(readlink -f $SOURCE_DIR)

JLA_DIR=$SOURCE_DIR'/jla'
log 'JLA_DIR' $(readlink -f $JLA_DIR)

DOJO_DIR=$SOURCE_DIR'/dojo'
log 'DOJO_DIR' $(readlink -f $DOJO_DIR)

ABS_DOJO_DIR=$(readlink -f $DOJO_DIR)
log 'ABS_DOJO_DIR' $ABS_DOJO_DIR

BASE_URL='file://'$ABS_DOJO_DIR
log 'BASE_URL' $BASE_URL

DOJO_JS=$DOJO_DIR'/dojo.js'
log 'DOJO_JS' $(readlink -f $DOJO_JS)

TEST_EXECUTOR=$SOURCE_DIR'/util/shrinksafe/js.jar'
log 'TEST_EXECUTOR' $(readlink -f $TEST_EXECUTOR)

CONFIG_FILE='dojo-test-config.js'

CONFIG_PATH='file://'$(readlink -f $CONFIG_FILE)
log 'CONFIG_PATH' $CONFIG_PATH

TARGET_MODULE=

if [ "$1" = "-m" ] ; then
    shift
    TARGET_MODULE=$1
    shift
fi

CURRENT_DIR='.'

echo 'Test dojo modules:'
format 'CURRENT_DIR' $(readlink -f $CURRENT_DIR)
format 'PROJECT' $(readlink -f $PROJECT_DIR)
format 'EXECUTOR' $TEST_EXECUTOR
format 'DOJO.JS' $DOJO_JS
format 'BASE_URL' $BASE_URL
format 'CONFIG' $CONFIG_PATH
format 'TEST' $TARGET_MODULE
echo 'Testing...'

java -jar $TEST_EXECUTOR $DOJO_JS baseUrl=$BASE_URL load=file://$CONFIG load=doh test=$TARGET_MODULE

The contents of the ${project_root}/src/main/webapp/js/test/dojo-test-config.js} file
require({
    paths: {
        //путь относительно файла dojo.js
        "jla" : "../test/jla"
    }
});

but in response I get the following error:
org.mozilla.javascript.WrappedException: Wrapped java.io.FileNotFoundException:  (Нет такого файла или каталога)
        at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1654)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:175)
        at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:411)
        at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
        at org.mozilla.javascript.gen.c2._c11(Unknown Source)
        at org.mozilla.javascript.gen.c2.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.callN(OptRuntime.java:86)
        at org.mozilla.javascript.gen.c1._c82(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:66)
        at org.mozilla.javascript.gen.c1._c7(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
        at org.mozilla.javascript.gen.c1._c38(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.call0(OptRuntime.java:57)
        at org.mozilla.javascript.gen.c1._c74(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
        at org.mozilla.javascript.gen.c1._c37(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
        at org.mozilla.javascript.gen.c1._c42(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
        at org.mozilla.javascript.gen.c1._c11(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.ScriptRuntime.applyOrCall(ScriptRuntime.java:2347)
        at org.mozilla.javascript.BaseFunction.execIdCall(BaseFunction.java:272)
        at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:127)
        at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:76)
        at org.mozilla.javascript.gen.c1._c1(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:76)
        at org.mozilla.javascript.gen.c1._c0(Unknown Source)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3003)
        at org.mozilla.javascript.gen.c1.call(Unknown Source)
        at org.mozilla.javascript.gen.c1.exec(Unknown Source)
        at org.mozilla.javascript.tools.shell.Main.evaluateScript(Main.java:526)
        at org.mozilla.javascript.tools.shell.Main.processFileSecure(Main.java:448)
        at org.mozilla.javascript.tools.shell.Main.processFile(Main.java:414)
        at org.mozilla.javascript.tools.shell.Main.processSource(Main.java:405)
        at org.mozilla.javascript.tools.shell.Main.processFiles(Main.java:179)
        at org.mozilla.javascript.tools.shell.Main$IProxy.run(Main.java:100)
        at org.mozilla.javascript.Context.call(Context.java:499)
        at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
        at org.mozilla.javascript.tools.shell.Main.exec(Main.java:162)
        at org.mozilla.javascript.tools.shell.Main.main(Main.java:140)
Caused by: java.io.FileNotFoundException:  (Нет такого файла или каталога)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
        at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
        at org.mozilla.javascript.tools.shell.Global.readUrl(Global.java:875)
        at org.mozilla.javascript.tools.shell.Global.readUrl(Global.java:661)
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:160)
        ... 48 more

If you run the test from the browser using url
http://path/to/project_root/src/main/webapp/js/util/doh/runner.html?paths=jla,../../../js/test/jla&test=jla/settings
then everything works fine: the test starts and passes successfully.
Initially, the path in the config is relative to the dojo.js file . But if you look closely, you can see that when running the test from a browser, in the paths parameter , the jla identifier is mapped to the path relative to the ${project_root}/src/main/webapp/js/util/doh/runner.html file , i.e. relative to the file that triggers the test.
I tried changing the path to the config file to the path relative to the script file and to the path relative to the ${project_root}/src/main/webapp/js/util/shrinksafe/js.jar file and it still doesn't work.
I can't figure out which way he's swearing. Tell me how to start testing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Stupak, 2016-01-29
@nikolaas

All. Figured it out myself. If anyone is interested, here is the solution:
Brought the project to this structure (it was not necessary, but had to be done for other purposes):

project_root
|--src
    |-- main
         |-- webapp
              |-- index.html
              |-- js
                   |-- dtk
                        |-- dojo
                        |-- dijit
                        |-- dojox
                        |-- util
                   |-- jla
                        |-- some
                             |-- module.js
                   |-- test
                        |-- dojo-test.sh
                        |-- dojo-test-config.js
                        |-- jla
                             |-- root-test.js
                             |-- some
                                  |-- test.js

dojo-test.js script code :
#!/bin/bash

# Скрипт для запуска тестов проект. Имеет два способа запуска:
# 1) Запуск тестирования всего проекта:
#      ./dojo-test.sh
#    При этом в качестве основного теста выступает тест 
#    ${project_root}/jla-web-app/src/main/webapp/js/test/jla/root-test.js,
#    который запускает остальные тесты
# 2) Запуск конкретного теста:
#      ./dojo-test.sh -t test/some/test

format() {
    printf "    %25s:  %s\n" $1 $2
}

# как оказалось запускать тестирование нужно из этой директории (тут путь взят относительно директории скрипта):
cd ../dtk/util/doh

CURRENT_DIR='.'
SOURCE_DIR='../../../'
PROJECT_DIR=$SOURCE_DIR'../../../../'
DOJO_DIR=$SOURCE_DIR'dtk/dojo'
DOJO_JS=$DOJO_DIR'/dojo.js'
RHINO=$SOURCE_DIR'dtk/util/shrinksafe/js.jar'
CONFIG_FILE=$SOURCE_DIR'test/dojo-test-config.js'

TARGET_MODULE=
if [ "$1" = "-t" ] ; then
    shift
    TARGET_MODULE=$1
    shift
else
    TARGET_MODULE='test/root-test'
fi

echo''
echo 'Testing project '$(readlink -f $PROJECT_DIR)':'
echo''
format 'RHINO' $RHINO
format 'DOJO.JS' $DOJO_JS
format 'BASE_URL' $DOJO_DIR
format 'CONFIG' $CONFIG_FILE
format 'TESTS' $TARGET_MODULE
echo''
echo 'Run test '$TARGET_MODULE'...'

java -jar $RHINO $DOJO_JS baseUrl=$BASE_URL load=$CONFIG_FILE load=doh test=$TARGET_MODULE

The contents of the config file (which is dojo-test-config.js ):
require({
    //пути задаются относительно файла dojo.js
    paths: {
        //путь к исходникам jla
        "jla" : "../../jla",
        //путь к тестам jla
        "test" : "../../test/jla"
    }
});

All. Now we store the sources in the directory
All tests are stored in the directory
Test example ${project_root}/src/main/webapp/js/test/jla/some/test.js :
define(["doh/runner"], function(doh) {
    doh.register("Tests group name", {
        name: "Test name",
        setUp: function() {
              //тут инициализация чего
        },
        runTest: function() {
            require(["jla/some/module"], function(module) {
                //тут собственно тестируем наш модуль module
            });
        },
        tearDown: function() {
            //тут действия после тестирования    
        }
    });
});

You need to run the dojo-test.sh script from this directory
using the command:
Or if you have several tests and want to run them all at once, then you need to create a root-test.js
file in the directory with the following content:
define([
    "doh/runner",
    "test/some/test1",
    "test/some/test2"
    //собственно тут перечисляем все тесты, которые хотим запустить
], function(doh) {
    return 1;//почему-то нужно что-то обязательно вернуть, вероятно по другому Dojo Loader не понимает, что это модуль и не грузит его
});

And run the dojo-test.sh script using the command:
It is also possible to run testing from a browser. To do this, open the following address (the address is given for my example):
I have an Apache web server set up that resolves sin2
to the Pro Parameters Own directory . First parameter
does the same as the dojo-test-config.js config file - sets the paths where dojo will look for source files for modules and tests.
The second parameter
specifies the test to be run.
In our case, to run all tests, you could immediately set this parameter in this form:
test=test/root-test

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question