Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Setting Up & Using Jest | Using a Test Automation Framework
Introduction to QA Automation Testing
course content

Conteúdo do Curso

Introduction to QA Automation Testing

Introduction to QA Automation Testing

1. Introduction to Automation Testing
2. Using a Test Automation Framework
3. Browser Automation with Selenium
4. Intro to Intermediate Automation Testing

bookSetting 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:

  1. Open Command Prompt, or any other suitable Console Application;
  2. Navigate to the desirable location using the `cd`` command;
  3. Create a directory for storing the project using the mkdir command. (Syntax: mkdir );
  4. Once the directory is successfully created, cd into the directory;
  5. Use npm init command to start a new project;
  6. Customize the basic configurations;
  7. 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:

  1. Open Command Prompt, or any other suitable Console Application;
  2. Navigate to the project directory using the cd command;
  3. 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.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
some-alt