Skip to content

Commit 646acd0

Browse files
Merge pull request #3 from monikakonieczna/Task-3-Create-API-Tests-For-GET-All-Characters-Endpoint
Task 3: Create api tests for get all characters endpoint
2 parents fdaa652 + f383b9a commit 646acd0

File tree

10 files changed

+1464
-6
lines changed

10 files changed

+1464
-6
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,30 @@ The following scripts are configured to run tests based on different groups:
7979
- test:filter_characters: Runs only the tests tagged as "filter_characters".
8080
- test:all: Runs all tests in the suite.
8181

82-
### Run All Tests
82+
### Parametrize Tests with Jest
83+
When writing api tests, it is common to have multiple test cases that run the same logic with different inputs and expected outputs. Jest provides a convenient way to handle this by allowing parameterized tests. This technique allows you to define a single test structure and run it multiple times with different values, reducing redundancy and improving maintainability.
84+
#### How to Parametrize Tests in Jest
85+
In Jest, you can use the test.each or describe.each functions to define parameterized tests. These functions allow you to specify a table of inputs and expected results that the test framework will loop over, executing the test for each case.
86+
87+
Example of describe.each
88+
89+
- describe.each groups related test cases that share common input data
90+
- Each test case within the describe block runs with the provided parameters
8391

84-
```shell
85-
npm test
8692
```
93+
describe.each([
94+
[99999, 404],
95+
[0, 404],
96+
[-1, 404],
97+
[1.5, 404],
98+
])(
99+
"Rick and Morty API Tests - Get One Character - Not Existing Character ID",
100+
(id, statusCode) => {
101+
it("Verifies that the API handle invalid character ID correctly", async () => {
102+
const response = await makeRequest("get", `${BASE_URL}/character/${id}`);
103+
assertStatusCode(response, statusCode);
104+
validateNotFoundErrorMessage(response.body);
105+
});
106+
}
107+
);
108+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
4+
// Function to load JSON data from the file
5+
const loadJsonData = (fileName: string): any => {
6+
const filePath = path.join(__dirname, "../test_data/get__all_characters", fileName);
7+
const data = fs.readFileSync(filePath, "utf-8");
8+
return JSON.parse(data);
9+
};
10+
11+
// Load test data
12+
const validCharactersPage1 = loadJsonData("valid_characters_page_1.json");
13+
const validCharactersPage2 = loadJsonData("valid_characters_page_2.json");
14+
const validCharactersPage42 = loadJsonData("valid_characters_page_42.json");
15+
const notFoundPage = loadJsonData("not_found_page.json");
16+
17+
/**
18+
* A helper function to validate the structure of the response data for existing character - Page 1
19+
* @param character
20+
*/
21+
export function validatePage1Structure(characters: Characters) {
22+
expect(characters).toEqual(validCharactersPage1);
23+
}
24+
25+
/**
26+
* A helper function to validate the structure of the response data for existing character - Page 2
27+
* @param character
28+
*/
29+
export function validatePage2Structure(characters: Characters) {
30+
expect(characters).toEqual(validCharactersPage2);
31+
}
32+
33+
/**
34+
* A helper function to validate the structure of the response data for existing character - Page 42
35+
* @param character
36+
*/
37+
export function validatePage42Structure(characters: Characters) {
38+
expect(characters).toEqual(validCharactersPage42);
39+
}
40+
41+
/**
42+
* A helper function to validate that the response body contains the expected character data
43+
* @param character
44+
*/
45+
46+
export function validatePaginationInfo(characters: Characters, count: number, pages: number, next: string | null, prev: string | null) {
47+
expect(characters.info.count).toEqual(count);
48+
expect(characters.info.pages).toEqual(pages);
49+
expect(characters.info.next).toEqual(next);
50+
expect(characters.info.prev).toEqual(prev);
51+
52+
}
53+
54+
/**
55+
* A helper function to verify the error response message for not existing page
56+
* @param character
57+
*/
58+
export function validateNotFoundErrorMessage(error: Error) {
59+
expect(error.message).toEqual(notFoundPage.message);
60+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
interface Characters {
2+
info: {
3+
count: number;
4+
pages: number;
5+
next: string;
6+
prev: number;
7+
};
8+
results: [
9+
{
10+
id: number;
11+
name: string;
12+
status: string;
13+
species: string;
14+
type: string;
15+
gender: string;
16+
origin: {
17+
name: string;
18+
url: string;
19+
};
20+
location: {
21+
name: string;
22+
url: string;
23+
};
24+
image: string;
25+
episode: Array<string>;
26+
url: string;
27+
created: string;
28+
}
29+
];
30+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error": "There is nothing here"
3+
}

0 commit comments

Comments
 (0)