Important
You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.
See our Release Process documentation for more information on the currently supported and upcoming releases.
JavaScript UnitTests¶
Installation¶
The following software is required to run JS tests:
Node.js (JavaScript Engine)
Karma (Test Runner for JavaScript)
Jasmine 3.5 (Behavior-Driven Development Testing Framework)
Hint
For instructions on how to install Node.js, navigate to the official website.
Once the node is installed, install several modules using Node Packaged Modules manager by executing the following command from the root folder of your application:
npm install --prefix=vendor/oro/platform/build
Where the –prefix parameter specifies the relative path to the platform/build directory.
Configuration¶
Configuration for the test-run is placed in build/karma.config.js.dist.
Hint
See more information in the official Karma documentation.
It can be useful to create a separate configuration file by copying the ./vendor/oro/platform/build/karma.config.js.dist file to ./vendor/oro/platform/build/karma.config.js and modifying it.
Running¶
To run tests, call the following command:
./vendor/oro/platform/build/node_modules/.bin/karma start ./vendor/oro/platform/build/karma.conf.js.dist --single-run
Remember to change the path to platform/build directory, if it is different in your application.
To run testsuite with a custom configuration, you can use the command line parameters which overwrite the parameters in the configuration file.
There are few custom options added for preparing karma config:
–mask _string_ file mask for Spec files. By default it is ‘vendor/oro/**/Tests/JS/**/*Spec.js’ that matches all Spec files in the project within oro vendor directory.
–spec _string_ path for a certain Spec file, if it passed then the search by mask is skipped and the test is run single Spec file.
–skip-indexing _boolean_ allows to skip phase of collection Spec files and reuse the collection from previews run (if it exists).
–theme _string_ theme name is used to generate webpack config for certain theme. By default it is ‘admin.oro’.
To keep tests continuously running and re-executing when any watched file is modified, use the following command:
./vendor/oro/platform/build/node_modules/.bin/karma start ./vendor/oro/platform/build/karma.conf.js.dist
To debug unit test:
run test in watch mode
open the
http://localhost:9876/debug.html
page in you browser (check the port in the address, it has to be the same as in terminal’s output)open the inspector panel and use it for the debug purpose
Any modification of the source or test file will lead to reassembly, after which you can reload the page in the browser and debug the updated code.
To run specific test, use the –spec “<path/to/someSpec.js>” parameter:
./vendor/oro/platform/build/node_modules/.bin/karma start ./vendor/oro/platform/build/karma.conf.js.dist --spec vendor/oro/platform/src/Oro/Bundle/UIBundle/Tests/JS/mediatorSpec.js
The following extensions can be useful if you use PHPStorm:
Karma plugin helps to run testsuite from the IDE and view results there;
ddescriber for jasmine helps to quickly turn off or skip some tests from testsuite .
Writing¶
Hint
See Jasmine 3.5 documentation for extensive information on writing tests with Jasmine 3.5.
The example below illustrates the spec for the oroui/js/mediator module:
1import mediator from 'oroui/js/mediator';
2import Backbone from 'backbone';
3
4describe('oroui/js/mediator', function () {
5 it("compare mediator to Backbone.Events", function() {
6 expect(mediator).toEqual(Backbone.Events);
7 expect(mediator).not.toBe(Backbone.Events);
8 });
9});
Hint
Use the inject-loader webpack loader for stubbing dependencies of a tested module.
Jasmine-jQuery¶
Jasmine-jQuery extends the base Jasmine functionality, specifically it:
adds a number of useful matchers, and allows to check the state of a jQuery instance easily
applies HTML-fixtures before each test and rolls back the document after tests
provides a way to load HTML and JSON fixtures required for a test
However, because Jasmine-jQuery requires the full path to a fixture resource, it is better to use import to load the fixtures by a related path.
1 import 'jasmine-jquery';
2 import $ from 'jquery';
3 import html from 'text-loader!./Fixture/markup.html';
4
5 describe('some/module', function () {
6 beforeEach(function () {
7 // appends loaded html to document's body,
8 // after test rolls back it automatically
9 window.setFixtures(html);
10 });
11
12 it('checks the markup of a page', function () {
13 expect($('li')).toHaveLength(5);
14 });
15 });