Зміст курсу
Introduction to QA Automation Testing
Introduction to QA Automation Testing
Setting Up & Using Jest
Commands Used in Video:
Change Directory (`cd`):
The syntax for changing the current directory is cd <path>
.
The path can be either relative or global. For-example you can move to a subdirectory simply by typing the name of the subdirectory instead of its full path: cd <subdirectory name>
.
Otherwise, you can method the full path of a directory as well: cd <full path>
.
If the target path is in a different local disk than the current location, then the \d
option is required: cd \d <full path>
.
Examples:
- Full Path:
cd C:/Users/Admin/Desktop/targetFolder
- Full Path with Different Local Disk:
cd \d F:/exampleFolder/targetFolder
- Relative Path:
targetFolder
Make Directory (`mkdir`)
To create a new folder, you can use the mkdir
command which uses the following syntax:
Examples:
- Full Path:
mkdir C:/Users/Admin/Desktop/newFolder
- Relative Path:
mkdir newFolder
Summary
The steps followed in the video are summarized below:
Installing Jest is fairly simple. We will learn how to install and use Jest on an empty Node.JS project, therefore, let’s first create a new Node.JS project:
- Open Command Prompt, or any other suitable Console Application;
- Navigate to the desirable location using the `cd`` command;
- Create a directory for storing the project using the
mkdir
command. (Syntax: mkdir); - Once the directory is successfully created,
cd
into the directory; - Use
npm init
command to start a new project; - Customize the basic configurations;
- Once configurations are complete, we can install Jest using the command
npm install --save-dev jest
;
After following the above steps, Jest should successfully be installed in the project. We can now move on to looking at how to execute some simple test cases.
Lets create a file called sort.js
which is going to contain a simple function for sorting integer arrays:
The last line makes the sort
function accessible from other JavaScript files in the same project.
To test if this sorting function works correctly, we can create a new Test Script containing some Unit Tests. Let’s create a new file inside the project folder, called sort.test.js
. It is important to note that the extension is .js
and not .test.js
. This is a naming convention used in Jest, for naming Test Scripts.
To automatically find Test Scripts, Jest looks for all the files that end with .test.js
in case of JavaScript and .test.ts
in case of TypeScript. It also looks for all the JavaScript or TypeScript files that are present in an optional folder named __tests__
. We will look at the test folder method in a bit.
Now we have successfully created a Test Script, however, it is empty. We can add the following code to the Test Script, which contains a few Unit Test cases for the sort
function:
The syntax of writing the test cases will be explored in the later chapters in detail, therefore it is not necessary to completely understand the code above.
To run the tests, we need to:
- Open Command Prompt, or any other suitable Console Application;
- Navigate to the project directory using the
cd
command; - Execute the following command:
npx jest
;
Upon running the command, Jest will automatically find and execute the Test Script - given that it is present inside the project folder. After the execution, it will display the results of the seven test cases.
The green checkmark indicates that the respective test has successfully passed.
Дякуємо за ваш відгук!