Skip to content

unitn-software-engineering/03-TestingAndCI

Repository files navigation

Testing and Continuous Integration

In today's class, we will see how to use Jest to test our APIs, how to set-up a continuous integration environment with Travis CI, and how to deploy our application on Heroku.

Jest

https://jestjs.io/docs/en/getting-started.html

An example:

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Use Jest in our npm project:

Install Jest in develompment environment:

> npm install --save-dev jest

Configure package.json test command:

"scripts": {
    "start": "node api.js",
    "test": "jest"
}

Also add this to activate coverage into your package.json:

"jest": {
    "verbose": true,
    "collectCoverage": true
}

Launch:

> npm run test

Additional reading on Jest:

Travis CI

Register and connect your GitHub account on:

https://travis-ci.org/

Configure your repository to use Travis CI:

https://docs.travis-ci.com/user/tutorial/#to-get-started-with-travis-ci-using-github

This includes the creation of file .travis.yml:

language: node_js
node_js:
  - "14"

Define Environment Variables:

https://docs.travis-ci.com/user/environment-variables

Chech build status of your application on Travis CI. Then include the "Build status image" into your repository, in the readme file. Click on the "Build status image" on Travis CI Dashboard to get the image embedding link. Example:

Build Status

Heroku

https://devcenter.heroku.com/articles/getting-started-with-nodejs?singlepage=true

  1. Register on https://www.heroku.com/

  2. Install Heroku CLI

    https://devcenter.heroku.com/articles/heroku-cli

  3. CLI log in

    heroku login

Prepare the application

https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#

  1. Create Procfile:

    https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#3-add-a-procfile

    web: node api.js
  2. Set listening port of Node server:

    https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#4-listen-on-the-correct-port

    const PORT = process.env.PORT || 8080

Create heroku app and connect with local repository.

https://devcenter.heroku.com/articles/git#creating-a-heroku-remote

  1. Create app and add remote to local repository. In your repository call:

    heroku create

  2. Alternatively, create a new app from the Heroku Dashboard and then manually add remote source to your local repository:

    With Heroku CLI: heroku git:remote -a our-heroku-app

  3. Define Environment Variables:

    https://devcenter.heroku.com/articles/config-vars

    https://devcenter.heroku.com/articles/heroku-local#copy-heroku-config-vars-to-your-local-env-file

  4. Push repository on heroku remote

    git push heroku main

  5. Start the Heroku app:

    heroku ps:scale web=1

  6. Run the Heroku app locally through the Procfile:

    heroku local web

  7. Open the deployed app:

    heroku open

  8. View logs:

    heroku logs --tail

Additional reading on Heroku:

About

Laboratorio di Ingegneria del Software 2 - Anno accademico 2020-2021 - Università degli studi di Trento

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published