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.
https://jestjs.io/docs/en/getting-started.html
An example:
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});Install Jest in develompment environment:
> npm install --save-dev jestConfigure 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-
Mock Functions:
Register and connect your GitHub account on:
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:
https://devcenter.heroku.com/articles/getting-started-with-nodejs?singlepage=true
-
Register on https://www.heroku.com/
-
Install Heroku CLI
-
CLI log in
heroku login
https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#
-
Create
Procfile:https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#3-add-a-procfile
web: node api.js
-
Set listening port of Node server:
const PORT = process.env.PORT || 8080
https://devcenter.heroku.com/articles/git#creating-a-heroku-remote
-
Create app and add remote to local repository. In your repository call:
heroku create -
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 -
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
-
Push repository on
herokuremotegit push heroku main -
Start the Heroku app:
heroku ps:scale web=1 -
Run the Heroku app locally through the Procfile:
heroku local web -
Open the deployed app:
heroku open -
View logs:
heroku logs --tail
-
Deploy automatically on Heroku after a successful build by Travis CI:
-
Alternatively, auto-deployment from GitHub can be configured on Heroku Dashboard: