I didn't find any article on the internet that explains how to get Ext4's dynamic class loading system (Ext.Loader) working with JsTestDriver.
Since the solution is not that obvious, I decided to share it with the community.
Note: JsTestDriver is a test runner for Javascript *unit* tests. It ships with its own framework for writing test-cases but supports other test frameworks such as Jasmine and QUnit with 3rd party plugins.
Disclaimer: this is not a beginner's tutorial how to write Javascript unit tests, nor how to use JsTestDriver. There are plenty of tutorials and blog posts that can help getting you started.
This post rather summarizes my findings (and a solution) on the specific problem of how to get Ext's dynamic class loader (Ext.Loader) working with JsTestDriver.
I came up with the following solution:- use the 'serve' config to have the embedded Jetty server service the Ext source files (and possibly your application source files as well).
Note that files specified under the 'server' section will *not* be loaded into the page by the jstd test-runner automatically/by default (as opposed to files under the 'load' section). This allows Ext.Loader to load these files dynamically.
- Configure Ext.Loader to know where to find the source files. JsTestDriver will serve your files from an absolute URL on the test server, that is '/test/...'.
- Tell Ext.Loader to use synchronous requests to load class files (!)
- Patch JsTestDriver to allow ** globbing (to recursively include directories), since this is not supported out of the box. JsTestDriver only supports globs with file extension that don't recurse into sub-directories (e.g. ext/src/*.js).
Please note that the ** syntax used below is not supported in JsTestDriver yet. See http://code.google.com/p/js-test-dri...es/detail?id=6. Alternatives:- Include ALL Ext directories following the pattern ext/src/*.js, ext/src/util/*.js, etc. Exclude the ext/src/core directory sub-tree. Update: added config file snippet that lists all required mappings, see below.
- Include ext-all.js in the load section and use dynamic loading only for application source files
- include file by file (oh well...)
- When listed in the 'serve' section the order of file definitions is *not* relevant, since Ext.Loader will evaluate dependencies to load files in the correct order.
jstestdriver.conf (example):
Code:
load:
- ext/ext-debug.js
- test/loader.js
test:
- test/MyTest.js
serve:
- app/**
- ext/src/**
loader.js:
Code:
Ext.Loader.setPath('myAppName', '/test/app');
Ext.Loader.setPath('test', '/test/test');
Ext.Loader.setPath('Ext', '/test/ext/src');
Ext.Loader.setConfig({ enabled: true, syncModeEnabled: true });
Notes:- make sure to restart the jstd server when modifying the 'serve' section. Even though it is the client (!) that uploads the config file and file infos, the server somehow doesn't properly update the served files without a restart. At least this is my experience with the JsTestDriver TRUNK (not sure about v1.3.2)
- Update: fixed in 1.3.3c. Both 1.3.2 and trunk don't work well on Windows due to bugs in the handling of file paths (/ vs \)
- Update: fixed in 1.3.3a. There is a critical bug in 1.3.2 with Chrome 14+ and FF 6 that will completely break the reporting of assert results from test cases. This is fixed on the trunk. See http://code.google.com/p/js-test-dri.../detail?id=263
- If one experiences file loading issues remember that it is possible to monitor the resources a captured browser loads in the browser's network tab (Firebug, DevTools, etc) - however, don't be fooled by the http status code. In case the test server cannot resolve the file it will return an *empty response* with status OK 200.
Add this to your JsTestDriver config file to allow Ext.Loader to access all ExtJs source files on the server.
Code:
serve:
- ext/src/*.js
- ext/src/app/*.js
- ext/src/button/*.js
- ext/src/chart/*.js
- ext/src/container/*.js
- ext/src/data/*.js
- ext/src/dd/*.js
- ext/src/direct/*.js
- ext/src/draw/*.js
- ext/src/flash/*.js
- ext/src/form/*.js
- ext/src/fx/*.js
- ext/src/grid/*.js
- ext/src/layout/*.js
- ext/src/menu/*.js
- ext/src/panel/*.js
- ext/src/picker/*.js
- ext/src/resizer/*.js
- ext/src/selection/*.js
- ext/src/slider/*.js
- ext/src/state/*.js
- ext/src/tab/*.js
- ext/src/tip/*.js
- ext/src/toolbar/*.js
- ext/src/tree/*.js
- ext/src/util/*.js
- ext/src/view/*.js
- ext/src/window/*.js
- ext/src/chart/axis/*.js
- ext/src/chart/series/*.js
- ext/src/chart/theme/*.js
- ext/src/data/proxy/*.js
- ext/src/data/reader/*.js
- ext/src/data/writer/*.js
- ext/src/draw/engine/*.js
- ext/src/form/action/*.js
- ext/src/form/field/*.js
- ext/src/fx/target/*.js
- ext/src/grid/column/*.js
- ext/src/grid/feature/*.js
- ext/src/grid/header/*.js
- ext/src/grid/plugin/*.js
- ext/src/grid/property/*.js
- ext/src/layout/component/*.js
- ext/src/layout/container/*.js
- ext/src/layout/component/field/*.js
- ext/src/layout/container/boxOverflow/*.js
- ext/src/tree/plugin/*.js
Update 2011-Oct-18:
- Updated to reflect changes (and bug fixes) in JsTestDriver 1.3.3c
- added 'serve' config example to allow loading of all ExtJs source files