Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 4 testing #4

Open
wants to merge 8 commits into
base: task_4_source_code
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
.vscode
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
# node-mentoring-introduction
Task 1
Task 4

https://d17btkcdsmqrmh.cloudfront.net/node-gmp/docs/testing/Homework

[Task API](https://date.nager.at/swagger/index.html)

## Subtask 1
Write unit tests for public-holidays.service.ts and helpers.ts files.
Keep in mind that any external calls are mocked in unit tests.

Tests Result

![2023-09-22_23h36_37](https://github.com/kandalova/node-mentoring-introduction/assets/26093763/81bd7f24-1837-42af-a42a-397972766bb4)

## Subtask 2
Write integration tests for public-holidays.service.ts. Do not forget that in this case you make real calls to the API.

Tests Result

![2023-09-22_23h37_02](https://github.com/kandalova/node-mentoring-introduction/assets/26093763/04812cbb-187a-4b4c-81b3-e27a29ed3387)


## Subtask 3
Get code coverage with tests for your application.
Strive to have no less than 90% of code coverage.

Tests results

![2023-09-22_23h36_01](https://github.com/kandalova/node-mentoring-introduction/assets/26093763/a9cae208-6d77-44bc-ac5a-ae25717c148f)

## Subtask 4
Select any 2 endpoints from <code>Nager.Date API</code> and write e2e tests for them.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

все пункты выполнены! молодец )

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Спасибо!


Tests Result

![image](https://github.com/kandalova/node-mentoring-introduction/assets/26093763/dd65bdaa-98a5-460f-a0a5-370711232df9)

14 changes: 14 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
testEnvironment: 'node',
roots: ['./src'],
preset: 'ts-jest',
collectCoverageFrom: ['src/**'],
coverageReporters: ['text'],
coverageThreshold: {
global: {
lines: 98
}
},
silent: false,
verbose: true,
};
8 changes: 8 additions & 0 deletions jest.e2e.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var config = require('./jest.config')
config.testMatch= [
"**/tests/**/e2e/**/*.[jt]s?(x)",
"**.e2e.[jt]s?(x)",
],
config.displayName= "e2e",
console.log('RUNNING E2E TESTS');
module.exports = config
8 changes: 8 additions & 0 deletions jest.integration.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var config = require('./jest.config')
config.testMatch= [
"**/tests/**/integration/**/*.[jt]s?(x)",
"**.integration.[jt]s?(x)",
],
config.displayName= "integration",
console.log('RUNNING INTEGRATION TESTS');
module.exports = config
8 changes: 8 additions & 0 deletions jest.unit.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var config = require('./jest.config')
config.testMatch= [
"**/tests/**/unit/**/*.[jt]s?(x)",
"**.unit.[jt]s?(x)",
],
config.displayName= "unit",
console.log('RUNNING UNIT TESTS');
module.exports = config
87 changes: 86 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
"main": "",
"scripts": {
"test": "jest",
"test:coverage": "jest --coverage"
"test:coverage": "jest --coverage",
"test:unit": "jest -c jest.unit.config.js",
"test:int": "jest -c jest.integration.config.js",
"test:e2e": "jest -c jest.e2e.config.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^29.0.0",
"@types/node": "^18.7.16",
"@types/supertest": "^2.0.12",
"expect-more-jest": "^5.5.0",
"jest": "^29.0.3",
"supertest": "^6.2.4",
"supertest": "^6.3.3",
"ts-jest": "^29.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.3"
Expand Down
60 changes: 60 additions & 0 deletions src/tests/e2e/nager-date.api.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import request from 'supertest';
import 'expect-more-jest';

const PUBLIC_HOLIDAYS_API_URL = 'https://date.nager.at/api/v3';

describe("Date Nager API", ()=>{
describe("/PublicHolidays", ()=>{
test("Should return 200 and array of holidays", async ()=>{
const url = `/PublicHolidays/${2023}/${"GB"}`;
const {status, body} = await request(PUBLIC_HOLIDAYS_API_URL).get(url);
expect(status).toEqual(200);
body.forEach((holiday:any)=>{
expect(holiday).toEqual({
date: expect.any(String),
localName: expect.toBeNullableOf(expect.any(String)),
name: expect.toBeNullableOf(expect.any(String)),
countryCode: expect.toBeNullableOf(expect.any(String)),
fixed: expect.any(Boolean),
global: expect.any(Boolean),
counties: expect.toBeNullableOf(expect.toBeArrayOfStrings()),
launchYear: expect.toBeNullableOf(expect.any(Number)),
types: expect.toBeNullableOf(expect.toBeArrayOfStrings()),
})
});
});
test("Should return 404 code",async () => {
const url = `/PublicHolidays/${2023}/${"wrong"}`;
const {status} = await request(PUBLIC_HOLIDAYS_API_URL).get(url);
expect(status).toEqual(404);
});
test("Should return 400 code",async () => {
const url = `/PublicHolidays/${202}/${"GB"}`;
const {status} = await request(PUBLIC_HOLIDAYS_API_URL).get(url);
expect(status).toEqual(400);
})
});

describe("/IsTodayPublicHoliday", ()=>{
test("Should return 200 or 204", async ()=>{
const url = `/IsTodayPublicHoliday/GB`;
const {status} = await request(PUBLIC_HOLIDAYS_API_URL).get(url);
expect([200, 204]).toContain(status);
});
test("Should return 200 or 204 with offset query", async ()=>{
const url = `/IsTodayPublicHoliday/GB/?offset=4`;
const {status} = await request(PUBLIC_HOLIDAYS_API_URL).get(url);
expect([200, 204]).toContain(status);
});
test("Should return 404 code",async () => {
const url = `/PublicHolidays/wrong`;
const {status} = await request(PUBLIC_HOLIDAYS_API_URL).get(url);
expect(status).toEqual(404);
});
test("Should return 400 code",async () => {
const url = `/PublicHolidays/202/GB`;
const {status} = await request(PUBLIC_HOLIDAYS_API_URL).get(url);
expect(status).toEqual(400);
})
})
})
73 changes: 73 additions & 0 deletions src/tests/integration/public-holidays.service.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { checkIfTodayIsPublicHoliday, getListOfPublicHolidays, getNextPublicHolidays } from "../../services/public-holidays.service";


const YEAR = 2023;
const COUNTRY = 'GB';
const SHORT_HOLIDAY = {
"date": "2023-09-21",
"localName": "string",
"name": "string",
}

describe("Public holidays service", () => {

it("should get all holidays", async () => {
const holidaysResponseBefore = await getListOfPublicHolidays(YEAR, COUNTRY);

expect(holidaysResponseBefore).toBeInstanceOf(Array);

const holidaysResponseAfter = await getListOfPublicHolidays(YEAR, COUNTRY);
const responseKeys = Object.keys(holidaysResponseAfter[0]);
const expectedKeysKeys = Object.keys(SHORT_HOLIDAY);

expect(holidaysResponseAfter.length).toBe(holidaysResponseBefore.length);

expect(responseKeys).toEqual(expect.arrayContaining(expectedKeysKeys));
expect(holidaysResponseAfter[0]).toEqual(expect.objectContaining({
date: expect.any(String),
localName: expect.any(String),
name: expect.any(String)
}));
});


it('all holidays should throw error for wrong county', async () => {
await expect(getListOfPublicHolidays(YEAR, "wrong")).rejects.toThrow(`Country provided is not supported, received: wrong`);
});

it('all holidays should throw error for wrong year', async () => {
await expect(getListOfPublicHolidays(1000, COUNTRY)).rejects.toThrow(`Year provided not the current, received: 1000`);
});

it('check today should throw error for wrong county', async () => {
await expect(checkIfTodayIsPublicHoliday("wrong")).rejects.toThrow(`Country provided is not supported, received: wrong`);
});

it("check today should return boolean", async () => {
const todayResponce = await checkIfTodayIsPublicHoliday(COUNTRY);
expect(typeof todayResponce === 'boolean').toBeTruthy();
});

it('next holidays should throw error for wrong county', async () => {
await expect(getNextPublicHolidays("wrong")).rejects.toThrow(`Country provided is not supported, received: wrong`);
});

it("should get next holidays", async () => {
const holidaysResponseBefore = await getNextPublicHolidays(COUNTRY);

expect(holidaysResponseBefore).toBeInstanceOf(Array);

const holidaysResponseAfter = await getNextPublicHolidays(COUNTRY);
const responseKeys = Object.keys(holidaysResponseAfter[0]);
const expectedKeysKeys = Object.keys(SHORT_HOLIDAY);

expect(holidaysResponseAfter.length).toBe(holidaysResponseBefore.length);

expect(responseKeys).toEqual(expect.arrayContaining(expectedKeysKeys));
expect(holidaysResponseAfter[0]).toEqual(expect.objectContaining({
date: expect.any(String),
localName: expect.any(String),
name: expect.any(String)
}));
});
});
Loading