Sai Mallik Rameshwaram WD401-level4
WD401-level4
Configuration of Testing Framework:
I've used Jest for testing small parts of the code (unit testing) and Cypress for testing the entire application from start to finish (end-to-end testing), which also covers integration testing.
Jest configuration:
Jest is a testing framework commonly used for JavaScript applications. which helps to check if your JavaScript code works correctly. It's easy to use and helps you write tests to make sure your code does what it's supposed to do. It's popular because it's simple and does a good job of testing JavaScript programs.
steps I followed to configure jest in my application:
step-1: Installing jest as developer dependence by running the below code.
> npm install --save-dev jest
step-2 :Adding the scripts in package.json as shown below.
"scripts":{
"pretest": "NODE_ENV=test npx sequelize-cli db:drop && NODE_ENV=test npx sequelize-cli db:create && NODE_ENV=test npx sequelize-cli db:migrate ",
"test": "NODE_ENV=test jest --detectOpenHandles",
}
pretest: This script is executed before running the tests.
It sets the environment variable NODE_ENV to test, indicating that tests are being run in a testing environment.Then it uses sequelize-cli to perform database operations:
->sequelize-cli db:drop: Drops (deletes) the test database.
->sequelize-cli db:create: Creates a new test database.
->sequelize-cli db:migrate: Runs any pending migrations on the test database, ensuring that the database structure is up to date.
test: This script runs the actual tests.
->It also sets the environment variable NODE_ENV to test.
->It uses Jest to execute the tests.
->The --detectOpenHandles flag is used to detect asynchronous operations that haven't been properly closed in the tests, which can help identify potential memory leaks or resource issues.
Cypress Configuration:
Cypress is a user-friendly testing tool for websites, suitable for both integration testing and end-to-end testing. It allows developers to simulate user interactions and test how different parts of a website work together, ensuring functionality across the entire application. With Cypress, we can easily write and execute tests to validate the performance and behavior of their web applications.
=> steps I followed to configure jest in my application:
step-1: Installing cypress as developer dependence by running the below code.
npm install cypress --save-dev
step-2: Adding scripts in package.json :
"scripts":{
"pretest": "NODE_ENV=test npx sequelize-cli db:drop && NODE_ENV=test npx sequelize-cli db:create && NODE_ENV=test npx sequelize-cli db:migrate ",
"test": "NODE_ENV=test cypress --detectOpenHandles",
}
Test Suite Coverage:
Test suite coverage, also called test coverage, is a way to measure how much of a computer program's code gets tested by a bunch of test cases. It shows how well testing has been done by revealing the parts of the code that have been used during testing.
To achieve this total coverage of the application, we can use two types of testing:
1)Unit testing.
2)Integration testing.
Unit testing:
->Unit testing is a type of software testing where individual units or components of a software application are tested in isolation to ensure they work correctly.
->In unit testing, each unit of code, such as functions, methods, or classes, is tested independently to verify that it performs as expected.
->It helps identify bugs early in the development process, promotes code quality, and provides confidence that each unit of the software functions correctly before integrating them into larger parts of the application.
Integration testing:
->Integration testing is a type of software testing that focuses on verifying the interactions and interfaces between different components or modules of a software system.
->In integration testing, multiple units or modules are combined and tested together as a group to ensure they work correctly when integrated.
->Integration testing helps identify issues such as communication errors, data flow problems, and compatibility issues between modules.
This test suite will run whenever a developer pushes the latest commits to GitHub. This automation ensures that the tests are executed consistently and promptly, providing rapid feedback to developers about the code.
Steps I followed to execute automatic test suite execution on GitHub:
1) Environment Variables: This is denoted by env in the workflow.
Environment variables 1) PG_DATABASE, 2) PG_USER, and 3) PG_PASSWORD are defined. These variables are used to configure the PostgreSQL database.
2)Jobs:
There's one job defined labeled run-tests, which will execute the steps defined within it.
3)Service Containers:
A PostgreSQL service container is configured to run alongside the job. This container is based on the postgres:11.7 Docker image and is set up with environment variables for the PostgreSQL user, password, and database name.
4)Steps:
->Check out the repository code:
The actions/checkout action is used to download a copy of the repository's code before running the CI tests.
-> Install dependencies:
npm ci is used to install project dependencies. This command ensures dependencies are installed based on the package-lock.json file for consistent builds.
-> Run unit tests:
npm test is used to execute unit tests. This command runs the test scripts defined in the package.json file.
-> Run the app: This step performs several tasks:
Installs dependencies using npm install.
Drops the existing database using npx sequelize-cli db:drop.
Creates a new database using npx sequelize-cli db:create.
Runs database migrations using npx sequelize-cli db:migrate.
Starts the Node.js application on port 3000 using PORT=3000 using npm start.
Waits for 5 seconds using sleep 5 to allow the application to start before proceeding.
By observing this GitHub Actions workflow, I understood that it automates the process of setting up a PostgreSQL database, installing dependencies, running unit tests, and starting a Node.js application for the specified repository.
->The output of the above flow is observed in the specified project repository in the actions tab as below:








Comments
Post a Comment