1

I am trying to set up the jasmine Unittests for Magento 2. I've already edited the settings.json inside the dev/tests/js/jasmine/spec_runner directory, to only test my own unit tests because I don't want to care about if unit tests of Magento or some other 3rd party modules would work or not. So far so good, everything works fine using grunt spec:luma:

(...)
GET /pub/static/frontend/Magento/luma/en_US/XXX_Osc/js/action/create-shipping-address-mock.js 200 0.611 ms - 442
GET /pub/static/frontend/Magento/luma/en_US/FormData.js 200 0.386 ms - 4236
GET /pub/static/frontend/Magento/luma/en_US/MutationObserver.js 200 0.431 ms - 17621
    ✓ should create
    - should create shipping address mock...
log: JQMIGRATE: Migrate is installed with logging active, version 1.4.1
    ✓ should create shipping address mock

2 specs in 0.106s.
>> 0 failures
(node:14824) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.

Done.


Execution Time (2019-09-02 14:47:58 UTC+2)
loading tasks                  56ms  ▇▇▇▇▇ 4%
spec:luma                      21ms  ▇▇ 1%
connect:luma                   33ms  ▇▇▇ 2%
loading grunt-contrib-jasmine  18ms  ▇▇ 1%
jasmine:luma                   1.4s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 91%
Total 1.5s

but to keep care of unit tests, now I want to get code coverage output for the jasmine unit tests. I've added the grunt-template-jasmine-Istanbul (https://github.com/maenu/grunt-template-jasmine-istanbul) grunt mixin, and am trying now to change the jasmine.js in the spec_runner directory in any way to get the code coverage for the unit tests to work, but nothing of it is working so far:

Example1:

tasks[themeName] = {
    src: configs,
    options: {
        host: host,
        template: require('grunt-template-jasmine-istanbul'),
        templateOptions: {
            baseUrl: baseUrl,
            coverage: 'bin/coverage/coverage.json',
            report: 'bin/coverage',
            thresholds: {
                lines: 75,
                statements: 75,
                branches: 75,
                functions: 90
            }
        },
        vendor: requireJs,
        junit: {
            path: "var/log/js-unit/",
            consolidate: true
        },

        /**
         * @todo rename "helpers" to "specs" (implies overriding grunt-contrib-jasmine code)
         */

        helpers: specs
    }
}

results in following console output... you may realize here that my custom js file which I want to test, is not found (404). Having a look on the var/coverage/ output it seems that it tested all the require.config.js files defined in the settings.json:

GET /_SpecRunner.html 200 3.716 ms - 1209
GET /.grunt/grunt-contrib-jasmine/jasmine.css 200 1.297 ms - 20297
GET /.grunt/grunt-contrib-jasmine/jasmine-html.js 200 1.857 ms - 16347
GET /.grunt/grunt-contrib-jasmine/jasmine.js 200 1.871 ms - 92077
GET /.grunt/grunt-contrib-jasmine/boot.js 200 2.171 ms - 6401
GET /.grunt/grunt-contrib-jasmine/json2.js 200 1.763 ms - 17569
GET /dev/tests/js/jasmine/tests/app/code/XXX/Osc/view/frontend/js/action/create-shipping-address-mock.test 404 3.192 ms - 246
GET /pub/static/frontend/Magento/luma/en_US/requirejs/require.js 200 3.471 ms - 82717
GET /.grunt/grunt-contrib-jasmine/dev/tests/js/jasmine/require.conf.js 200 2.190 ms - 1451
GET /.grunt/grunt-contrib-jasmine/pub/static/frontend/Magento/luma/en_US/requirejs-config.js 200 2.754 ms - 40188
GET /.grunt/grunt-contrib-jasmine/grunt-template-jasmine-istanbul/reporter.js 200 1.239 ms - 554
GET /.grunt/grunt-contrib-jasmine/reporter.js 200 1.251 ms - 3768
Warning: expected branches coverage to be at least 75% but was 40% Use --force to continue.

Aborted due to warnings.

or Example2:

tasks[themeName] = {
    src: configs,
    options: {
        host: host,
        template: require('grunt-template-jasmine-istanbul'),
        templateOptions: {
            baseUrl: baseUrl,
            coverage: 'bin/coverage/coverage.json',
            report: 'bin/coverage',
            thresholds: {
                lines: 75,
                statements: 75,
                branches: 75,
                functions: 90
            },
            template: require('grunt-template-jasmine-requirejs'),
            templateOptions: {
                requireConfig: {
                    baseUrl: '.grunt/grunt-contrib-jasmine/src/main/js/'
                }
            },
            replace: false,
        },
        vendor: requireJs,
        junit: {
            path: "var/log/js-unit/",
            consolidate: true
        },

        /**
         * @todo rename "helpers" to "specs" (implies overriding grunt-contrib-jasmine code)
         */

        helpers: specs
    }
}

results in:

GET /_SpecRunner.html 200 3.572 ms - 3961
GET /'%20+((__t%20=%20(%20style%20))%20==%20null%20?%20''%20:%20__t)%20+' 404 2.096 ms - 189
GET /'%20+((__t%20=%20(%20script%20))%20==%20null%20?%20''%20:%20__t)%20+' 404 1.515 ms - 190
GET /'%20+((__t%20=%20(%20temp%20?%20temp%20+ 404 1.588 ms - 171

Warning: PhantomJS timed out, possibly due to an unfinished async spec. Use --force to continue.

Aborted due to warnings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.