'Run Jasmine tests (coupled to DOM/jquery etc) via command line for CI

We have a set of Jasmine tests that run successfully in the local web server. http-server.

Successful test run image

We would like to run these tests from the command line during the TeamCity build process without having to start a webserver.

Opening the html file with chrome using --disable-web-security flag results in

Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

Error: Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

Probably because script references with type="module" in the SpecRunner.html file

<script src="../src/js/App/app.js" type="module"></script>

Jasmine tests are coupled to DOM/jQuery so that they need to be run in a browser. The SpecRunner html page includes script references with type="module".

How can we run these tests during a TeamCity build and fail the build if there are any test failures?

Thanks.



Solution 1:[1]

Here's my solution. We are already using jest for react tests and jest docs say

If you are using Jasmine, or a Jasmine like API (for example Mocha), Jest should be mostly compatible, which makes it less complicated to migrate to.

  1. npm install jest puppeteer jest-puppeteer http-server

    • jest: To run the tests using Jasmine API
    • puppeteer: To run tests in headless chrome browser
    • http-server: Local webserver to navigate to the jasmine spec runner page
    • jest-puppeteer: To configure starting the local server before running the tests and stopping it afterwards
  2. Add jest-puppeteer-config.json to start the server like this

Jest Puppeteer integrates a functionality to start a server when running your test suite. It automatically closes the server when tests are done.

  1. Add a jest-puppeteer test to navigate to Jasmine SpecRunner page and assert that there are no failures.

  2. Update your jest configuration docs

  3. Create npm script to a run jest test that in turn runs Jasmine tests in the browser.

At this point, one should be able to run browser Jasmine tests from the command line locally or on the build server.

Here is a screenshot that shows the files and test run results both locally and on TeamCity.

package.json jest-config.js jest-puppeteer-config.js run-tests.js

How can we run these tests during a TeamCity build and fail the build if there are any test failures?

This solution enabled us to restore around hundred legacy browser-coupled Jasmine tests as part of the build with minimum effort (did not have to update the tests).

Feel free to suggest alternatives.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alper