Important

You are browsing the documentation for version 3.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:

1 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:

1./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’.

The following extensions can be useful if you use PHPStorm:

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});

karma-jsmodule-exposure

This approach allows to test the public API of a module. But what about

Use the karma-jsmodule-exposure plugin on a fly injects exposing code inside the js-module and provides API to manipulate internal variables:

 1 import someModule from 'some/module';
 2 import jsmoduleExposure from 'jsmodule-exposure';
 3
 4 // get exposure instance for tested module
 5 var exposure = jsmoduleExposure.disclose('some/module');
 6
 7 describe('some/module', function () {
 8     var foo;
 9
10     beforeEach(function () {
11         // create mock object with stub method 'do'
12         foo = jasmine.createSpyObj('foo', ['do']);
13         // before each test, pass it off instead of original
14         exposure.substitute('foo').by(foo);
15     });
16
17     afterEach(function () {
18         // after each test restore original value of foo
19         exposure.recover('foo');
20     });
21
22     it('check doSomething() method', function() {
23         someModule.doSomething();
24
25         // stub method of mock object has been called
26         expect(foo.do).toHaveBeenCalled();
27     });
28 });

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   });