w3resource

Jest Architecture: Understanding Framework Components


Remember the last time you ran jest <my-test.js>, you type those commands in your terminal and waited till the test completed. All you had up your sleeves was what command does what. Today however we are going to take a deep dive into how jest accomplishes that instruction for you, this tutorial focuses on the architecture of the jest framework.

The jest framework comprises of packages and in most cases these are the packages and classes that are required for a test to run

  • jest-config
  • jest-cli
  • jest-haste-map
  • jest-worker
  • searchsource
  • testsequencer
  • testscheduler
  • jest-runner
  • jest-jasmine
  • jest-runntime
  • jest-environment

jest-cli

This package holds all other packages together, when you run the jest my-test.js, it will make a call to a package called jest-config.

jest-config

This package takes the config that you specify in say your package.json and determines what you actually want to do. There is a function called normalize in this package which normalizes the data that is obtained from the package.json and then create two separate config objects:

  • Global config
  • Project config

The global config is defined by jest, it config contains everything from which timers to use, which test framework to use and how many workers to use, test-reporters to run e.t.c. while the project config contains configuration that is contained in your project folder. Once the configuration has been established, jest (jest-cli) will make a call to another package called jest-haste-map (explained below)

jest-haste-map

This package loads the files in your project and the dependencies that those files require, and then relationships between these modules. The jest-haste-map will invoke watchman if you have it installed in your project, which will help you to monitor changes made to your files. This is because jest-haste-map has a cache. If watchman is not installed jest will use the node-crawler.

jest-workers

The jest worker helps us to manage our CPU?s efficiently. It loads the dependencies and return it back to a HasteContext class that is contained in jest-haste-map. This HasteContext will be returned back to jest(jest-cli). The jest-cli will then make a call to a SearchSource class contained in the jest-cli.

SearchSource

This is a class in jest that searches the HasteContext and figures out which tests we are to run. The SearchSource will an array of tests

TestSequencer

The TestSequencer determines the order in which a test is run, first it will check the list of tests that is found by searchSource and if it finds failed tests, it will run those tests first. It also runs slower tests first so as to maximize system CPUs. This class was previously known as TestScheduler. The downside of TestSequencer is when it comes to determining the order in which test will run based on the sizes, this is because a test that contains small lines of code, may require very heavy modules. However, TestSequencer can be improved by using a test priority system. Once this is done, it calls the TestScheduler.

TestScheduler

This class is the actual system that figures out which test we want to run, based on the sequencing that we have defined. It checks whether we want to run the test with the default jest process or we want to run the tests with workers. It is also responsible for creating reporters in jest.

jest-runner

This package helps to determine the number of processes that will be used to run the list of tests.

jest-jasmine /jest-circus

By default, jest runs your test with jest-jasmine but you can set it up to use jest-circus.

jest-environment

This is created by the test-runner at the same time that the jest-runtime is created.

jest-runtime

This module enables your tests to be run in a different context even if they are running in the same process. It is created by the test-runner.

Now when these processes are complete, jest will return a type called TestResult, which is updated as soon as your results come from jest-jasmine or jest-circus. So that is the journey that your files have to go through when you run jest <my-test.js>

Previous: Jest CLI Options.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/jest/jest-architecture.php