[FEATURE] Introduce UnitTests for JavaScript
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Documentation / Changelog / master / Feature-76590-IntroduceUnitTestsForJavaScript.rst
1 ====================================================
2 Feature: #76590 - Introduce UnitTests for JavaScript
3 ====================================================
4
5 Description
6 ===========
7
8 The core use karma as test runner and jasmine as testing framework for JavaScript UnitTests.
9 The tests will be run on travis with PhantomJS.
10 Locally other browsers like Chrome, Firefox, Safari or IE can be used as well.
11
12 To run the UnitTests on a local system the following steps are necessary:
13
14 1. Install dependencies
15
16 .. code-block:: bash
17
18 cd Build
19 npm install
20 cd ..
21
22 2. Run the tests from your terminal
23
24 .. code-block:: bash
25
26 # Execute the tests only once
27 ./Build/node_modules/karma/bin/karma start typo3/sysext/core/Build/Configuration/JSUnit/karma.conf.js --single-run
28
29 # Execute the tests for every change (file watcher mode)
30 ./Build/node_modules/karma/bin/karma start typo3/sysext/core/Build/Configuration/JSUnit/karma.conf.js
31
32 # Execute the tests for different browser
33 ./Build/node_modules/karma/bin/karma start typo3/sysext/core/Build/Configuration/JSUnit/karma.conf.js --single-run --browsers Chrome,Safari,Firefox
34
35 Test-Files
36 ==========
37
38 Any test file must be located in extension folder ``typo3/sysext/<EXTKEY>/Tests/JavaScript/``
39 The filename must end with Test.js, e.g. ``GridEditorTest.js``
40 Each testfile must be implemented as AMD module, must use strict mode and have to use :js:`describe` with module name as outer wrap for each test.
41 The following code block shows a good example:
42
43 .. code-block:: javascript
44
45 define(['jquery', 'TYPO3/CMS/Backend/AnyModule'], function($, AnyModule) {
46 'use strict';
47 // first and outer wrap describe the test class name
48 describe('TYPO3/CMS/Backend/AnyModuleTest:', function() {
49 // second wrap describe the method to test
50 describe('tests for fooAction', function() {
51 // the first parameter of each 'it' method describe the test-case.
52 it('works for parameter a and b', function() {});
53 });
54 describe('tests for barAction', function() {
55 it('works for parameter a and b', function() {});
56 });
57 }
58 }
59
60 Please take a look at the existing test-files and read the jasmine documentation for further information.
61
62 DataProvider for tests
63 ----------------------
64
65 For testing a set of values, the core implement a kind of DataProvider. To use the DataProvider you have to use the function :js:`using`.
66 Please take a look at ``FormEngineValidationTest.js`` for an example.
67