Tests JavaScript for Joomla 4

From Joomla! Documentation

This page is a translated version of the page JavaScript Tests for Joomla4 and the translation is 21% complete.

Other languages:
Deutsch • ‎English • ‎français


For Running JavaScript Tests for the Joomla 3.x please see Running JavaScript Tests for the Joomla CMS. Joomla! 4.x core currently has some custom written JavaScript libraries used in performing various tasks. This documentation is about the environment setup used in Joomla! 4.x for testing those JavaScript libraries and how to write new tests.

You can find the current version of the tests on Github.com in the repo https://github.com/joomla/test-javascript.

When you checkout the 4.0-dev branch of Joomla from Github.com, you will see a folder named "tests" which contains a folder named "javascript". This "javascript" folder contains the JavaScript tests written to cover the aforementioned JavaScript libraries. The tests are written using the Jasmine framework, Karma is used as the test runner and the tests are run on a Firefox web browser.

$ ls
acceptance.suite.yml  Gemfile              package.json
administrator         htaccess.txt         phpunit.xml.dist
appveyor-phpunit.xml  images               plugins
build                 includes             README.md
build.js              index.php            README.txt
build.xml             installation         RoboFile.dist.ini
cache                 Jenkinsfile          RoboFile.php
cli                   jenkins-phpunit.xml  robots.txt.dist
codeception.yml       karma.conf.js        scss-lint.yml
components            language             templates
composer.json         layouts           -> test
composer.lock         libraries            tmp
configuration.php     LICENSE.txt          travisci-phpunit.xml
dev                   media                web.config.txt
drone-package.json    modules

Before you can run the tests, you need to install some software on your local workstation (see Prerequisites section below). This document explains how to run and create your own tests from your local workstation.


The testing environment requires your local machine to have Node.js installed in it. To install Node.js, please go to the Node.js official web site, download the respective setup for your operating system and install it by following the installation wizard. NPM (Node Package Manager) is used to manage and setup the JavaScript testing environment.

Install dependencies (packages)

Open a command line and navigate to the root directory of Joomla 4.x.

Exécutez la commande npm install

$ npm install
added 1354 packages in 193.687s

This will install all the dependencies to the "/node_modules" directory. If a /node_modules folder does not exist, a folder will be automatically created by npm. It is OK, if you see some warnings, but you should not see an error message. This will give you a full setup that might be different from the automated testing setup. For automated testing we are using the drone-package.json. If you are rename package.json to package.json.save and drone-package.json to package.json before you run npm install you will get a lighter setup and you will using the versions we are using in our automated testing setup.

Starting the Karma server and running the tests

Exécutez la commandenpm run test
Ceci démarre le serveur web et ouvre automatiquement le navigateur Firefox. Ensuite, les tests seront exécutés et les résultats détaillés seront affichés dans la ligne de commande elle-même.

[33m05 02 2018 13:10:01.666:WARN [watcher]: [39mAll files matched by "/var/www/html/JOOMLA/joomla4/joomla-cms/media/system/js/core.js" were excluded or matched by prior matchers.
[32m05 02 2018 13:10:02.009:INFO [karma]: [39mKarma v2.0.0 server started at
[32m05 02 2018 13:10:02.010:INFO [launcher]: [39mLaunching browser Firefox with unlimited concurrency
[32m05 02 2018 13:10:02.015:INFO [launcher]: [39mStarting browser Firefox
[32m05 02 2018 13:10:04.159:INFO [Firefox 58.0.0 (Ubuntu 0.0.0)]: [39mConnected on socket 0l2b3VhoFFUVyWitAAAA with id 91983742
[33m05 02 2018 13:10:04.493:WARN [web-server]: [39m404: /uri
[32m05 02 2018 13:10:04.556:INFO [Firefox 58.0.0 (Ubuntu 0.0.0)]: [39mStarting tests 91983742
Firefox 58.0.0 (Ubuntu 0.0.0): Executed 0 of 125 SUCCESS (0 secs / 0 secs)
Browser results:

 - Firefox 58.0.0 (Ubuntu 0.0.0): 125 tests
   - 125 ok

What the above command does is execute node node_modules/karma/bin/karma start karma.conf.js --single-run in the background. You can check this specification in the script section of the file package.json

The magic inside

The karma.conf.js file mentioned in the command for starting the Karma server and that you can find in the root of Joomlaǃ 4.x, is the configuration file for the Karma server. Below you can see the content of this file.

// Karma configuration

module.exports = function (config) {

		// base path that will be used to resolve all patterns (eg. files, exclude)
		basePath: '',

		// frameworks to use
		// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
		frameworks: ['jasmine-ajax', 'jasmine', 'requirejs'],

		// list of files / patterns to load in the browser
		files: [
			{pattern: 'media/system/js/polyfills/webcomponents/webcomponents-ce.min.js', included: true, served: true, watched: true},
			{pattern: 'node_modules/jquery/dist/jquery.min.js', included: false},
			{pattern: 'node_modules/jasmine-jquery/lib/jasmine-jquery.js', included: false},
			{pattern: 'node_modules/text/text.js', included: false},
			{pattern: 'media/vendor/bootstrap/js/bootstrap.min.js', included: false},
			{pattern: 'media/vendor/jquery-ui/js/jquery.ui.core.min.js', included: false},
			{pattern: 'media/vendor/jquery-ui/js/jquery.ui.sortable.min.js', included: false},
			{pattern: 'media/system/js/*.js', included: false},
			{pattern: 'media/system/js/core.js', included: false,served: true, watched: true},
			{pattern: 'media/system/js/legacy/*.js', included: false},
			{pattern: 'media/system/js/fields/*.js', included: false},
			{pattern: 'media/vendor/joomla-custom-elements/js/joomla-alert.min.js', included: false, served: true, watched: true},
			{pattern: 'media/system/js/fields/calendar-locales/*.js', included: false},
			{pattern: 'media/system/js/fields/calendar-locales/date/gregorian/*.js', included: false},
			{pattern: 'tests/javascript/**/fixture.html', included: false},
			{pattern: 'tests/javascript/**/spec.js', included: false},
			{pattern: 'tests/javascript/**/spec-setup.js', included: false},
			{pattern: 'media/system/webcomponents/js/*.js', included: false},
			{pattern: 'images/*.png', included: false},


		exclude: [

		// preprocess matching files before serving them to the browser
		// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
		preprocessors: {
			'**/system/js/*.js': ['coverage']

		// coverage reporter configuration
		coverageReporter: {
			type : 'html',
			dir : 'build/coverage-js/'

		// test results reporter to use
		// possible values: 'dots', 'progress'
		// available reporters: https://npmjs.org/browse/keyword/karma-reporter
		reporters: ['verbose', 'progress', 'coverage'],

		// web server port
		port: 9876,

		// enable / disable colors in the output (reporters and logs)
		colors: true,

		// level of logging
		// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
		logLevel: config.LOG_INFO,

		// enable / disable watching file and executing tests whenever any file changes
		autoWatch: true,

		// start these browsers
		// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
		browsers: ['Firefox'],

		// Continuous Integration mode
		// if true, Karma captures browsers, runs the tests and exits
		singleRun: false,

		// list of plugins
		plugins: [

		// Concurrency level
		// how many browser should be started simultaneous
		concurrency: Infinity

Let's have a look at the important configurations that makes everything run together smoothly.

The files attribute contains a list of files that the Karma server is going to serve. Unless specified in here, no tests can access any file. The set of files currently specified here are the

  • third party JavaScript libraries like jQuery and jasmine-jquery,
  • the set of Joomla! custom JavaScript libraries,
  • the set of spec files that contain test cases along with the files that set up the spec executions and
  • the files that contains the HTML fixtures the libraries are tested on and finally some sample images.

We have these files specified with the included: false option because we are using require.js for that purpose. The only file that will be both LOADED and INCLUDED from karma itself will be the test-main.js file generated from require.js. From there on, require.js takes care of including the other loaded files.

The exclude attribute does what the name suggests and is used since the "media/system/js" directory contains both the minified as well as the uncompressed version of the same library. We need to exclude the uncompressed versions from the loaded files list and that's what this exactly does. Preprocessors attribute has the externally added coverage reporter specified in it. The output in the command line is also made verbose by the verbose reporter.

Autowatch is set to true so that whenever a change to any loaded file is made, the Karma server would detect that and rerun the tests. This makes the life of the tester easy.

The browsers attribute accepts an array of browsers that we need the tests to be run on and currently has only Firefox set. In order to use any other browser, the relevant browser launcher needs to be installed and of course you need to have that browser installed on your local machine.

If singleRun is set to true, Karma will start and capture all configured browsers, run tests and then exit with an exit code of 0 or 1 depending on whether all tests passed or any tests failed.singleRun a
That obviously would not be what we expect when we have autowatch also set to true. Therefore for the ease of development the option is set to false. Still it is possible to run the server in the 'single run' fashion by passing --single-run option in the karma start command - as we do. node node_modules/karma/bin/karma start karma.conf.js --single-run

The next important file we need to look at is the test-main.js file which is an auto generated file from require.js. We use require.js in this setup to make dynamic loading of dependencies in test specs possible. You can find this file in the folder /test/javascript/.

Below you can see the content of this file.

const allTestFiles = [];
const TEST_REGEXP = /(spec|test)\.js$/i;

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach((file) => {
	if (TEST_REGEXP.test(file)) {
		// Normalize paths to RequireJS module names.
		// If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
		// then do not normalize the paths
		const normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');

	// Karma serves files under /base, which is the basePath from your config file
	baseUrl: '/base',

	paths: {
		'core': 'media/system/js/core.min',
		'jquery': 'node_modules/jquery/dist/jquery.min',
		'jui': 'media/vendor/jquery-ui/js/jquery.ui.core.min',
		'jui-sortable': 'media/vendor/jquery-ui/js/jquery.ui.sortable.min',
		'bootstrap': 'media/vendor/bootstrap/js/bootstrap.min',
		'jasmineJquery': 'node_modules/jasmine-jquery/lib/jasmine-jquery',
		'libs': 'media/system/js',
		'legacy_libs': 'media/system/js/legacy',
		'testsRoot': 'tests/javascript',
		'text': 'node_modules/text/text',
		'fields': 'media/system/js/fields',
		'calLang': 'media/system/js/fields/calendar-locales/en',
		'calDate': 'media/system/js/fields/calendar-locales/date/gregorian/date-helper',
		'JCE': 'media/system/webcomponents/js'

	shim: {
		jasmineJquery: ['jquery'],
		bootstrap: ['jquery'],
		'jui-sortable': ['jquery'],
		'libs/validate': {
			deps: []
		'libs/subform-repeatable': {
			deps: ['jquery', 'jui', 'jui-sortable']
		'JCE/joomla-field-send-test-mail': {
			deps: ['jquery']
		'libs/fields/calendar': {
			deps: ['calLang', 'calDate']

	// dynamically load all test files
	deps: allTestFiles,

	// we have to kickoff jasmine, as it is asynchronous
	callback: window.__karma__.start

Two important changes are done to the auto generated file.

  • The first is the paths attribute set in in the require configuration. These paths allow us to have aliases assigned to specific JavaScript files. This way for example, whenever we need to add "node_modules/jquery/dist/jquery.min" as a dependency, all we need to do is to specify the alias jquery in the dependency list.
  • The next change made to the auto generated file is the 'shim' attribute. This is a very simple option where we get to specify the dependencies among the paths set above. For example: shim: {jasmineJquery: ['jquery']} tells require.js to load jquery before loading jasmineJquery.

L'écriture de nouveaux tests

Test cases are stored in a modular fashion inside the "tests/javascript/" directory where we have a specific directory allocated for a specific JavaScript library. Inside of that directory lies two JavaScript files, one containing the test cases (spec.js) and one containing any setup code necessary to run the tests (spec-setup.js). The HTML content that needs to be loaded to the DOM for the tests to run are kept inside a directory named "fixtures" and inside it lies the fixture.html file. fixture.html file simply contains pure HTML.


The spec-setup.js mostly does appending of the required HTML fixtures to the DOM. The test setup uses a plugin called RequireJSText that allows dynamic loading of HTML files also possible. Only difference is that the dependency is prefixed with text!. For example, to load the fixture with an alias fixtureHtml, the dependency specified should be text!fixtureHtml. You can find this plugin in the folder /node-modules For more information see http://requirejs.org/.

Exemple de code

On the end of the article Running JavaScript Tests for the Joomla CMS you can find some Code templates. Here you see a code example.


define(['jquery', 'testsRoot/calendar/spec-setup', 'jasmineJquery'], function ($) {
	beforeAll(function () {
		var element = document.querySelector(".field-calendar"),
		    input = document.getElementById('jform_created'),
		    currentDate = new Date();

		input.value = currentDate.getFullYear() + '-09-01 05:17:00';
		input.setAttribute('data-alt-value', currentDate.getFullYear() + '-09-01 05:17:00');


	describe('Calendar set for the input element', function () {
		it('Should have calendar element under the input element', function () {

		it('Calendar should be hidden', function () {

		it('Should appear on button click', function (done) {

            setTimeout(function() {
            }, 200)



define(['jquery', 'text!testsRoot/calendar/fixtures/fixture.html', 'libs/fields/calendar'], function ($, fixture) {




<div id="calendarjs">
	<form name="formTest" onsubmit="return false">
		<div class="field-calendar">
			<div class="input-append">
				<input type="text"
						placeholder="Created date."
				<button type="button"
						class="btn btn-secondary"
						data-dayformat="%Y-%m-%d %H:%M:%S"
					<span class="icon-calendar"></span>
		<input type="text" name="none-spec" id="cal-close-btn" value="" title="nonenne"/>