(As per the Jest website) Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It has a great API that covers features (describe
), scenarios (it
) and assertions (expect
) which means you don't need any additional tools like CHAI assertion library to write your tests.
A sample feature and test in Jest:
// Feature
describe("Math Operations", () => {
// test
test("1 + 2 equals 3", () => {
expect(math.sum(1, 2)).toBe(3);
});
// test
test("1 - 2 equals -1", () => {
expect(math.difference(1, 2)).toBe(-1);
});
});
I have created this project as a starting point for those who want to use Jest for functional test automation. If you are planning to use Jest in your testing work, this project will surely give you a good head-start.
It's a Test Automation Framework with a lot of important features already built-in. I have created this project as a starting point for those who want to use Jest for functional test automation. If you are planning to use Jest in your testing work, this project will surely give you a good head-start.
It comes with a jest config file that has a lot of settings already configured. This means, you can use it as-is or customize it further as per your needs. see the central jest config file here.
Under the testconfig folder, there are global_setup
and global_teardown
scripts. They run only once for entire set of tests that get executed. You may add global-level setup and teardown code here. To begin with, these scripts only print the colorful messages on the console.
Jest Config file has a section where global variables can be defined. See a sample Global variables section below.
globals: {
__APP_NAME__: 'MATH',
}
The variable __APP_NAME__
can be directly used in the test script as shown below.
// __APP_NAME__ value is set in the jest.config.js under the Globals section
let appName = __APP_NAME__;
// parent test suite
describe(`${appName} - Main Suite`, () => {
...
});
One use of global variables is to store application and environment details like application URLs, port numbers, API end points.
I have configured 3 different HTML reports in the framework. The configuraton is available in the Jest Config file under the reporters
key. Based on your liking you may choose to use any one of them (or all, if you are inclined to do so 😊).
Test coverage is by default enabled in the config file. The framework generates coverage reports in multiple formats. (It also shows coverage report and summary on the console output.). All coverage reports are available under the coverage
sub-folder.
Bonus feature - 1 - A detailed coverage report is available in a colorful format as part of the JEST-STARE HTML reports. See the Coverage
link at the top in the screenshot above.
Bonus feature - 2 - the coverage report is also available in a colorful format in the Jenkins UI, when the tests are run through Jenkins CI builds. More on this in the following section.
The framework comes with a sample Jenkinsfile that can be use to setup a basic Pipeline with test step. The Jenkinsfile executes below steps during the build operation.
- Runs
npm install
to download and install the dependencies - Runs
npm test
to execute the jest tests - Runs post steps to
- read the junit.xml and generate report in the Jenkins project
- read the coverage xml to generate the Cobertura coverage report See the Jenkins Coverage report below.
- Clone or download the repo.
- Switch to the framework folder
- Run
npm install
. This will install all framework dependencies (local install) - Run
npm test
to trigger the jest execution run. After the run is complete, some extra folders get created (reports
,coverage
etc). You can see various html reports under thereports
folder and coverage reports under thecoverage
folder.