Skip to content

Commit b47695d

Browse files
chore: edit test
1 parent 12f05c6 commit b47695d

File tree

6 files changed

+78
-30
lines changed

6 files changed

+78
-30
lines changed

src/utils/converters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const formatDateToTimestamp = (
2929
date: string,
3030
format: string = 'YYYY-MM-DD HH:mm:ss',
3131
timeZone: string = `${serverConfig['tz']}`,
32-
): number => moment(date, format).tz(timeZone).unix();
32+
): any => moment(date, format).tz(timeZone).unix();
3333

3434
export const toStringify = (data: any, replace: any = null, space: number = 2): string => {
3535
return JSON.stringify(data, replace, space);

test/unit/character.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import request from 'supertest';
2-
import app from '../../src/app';
3-
41
const config = require('config');
2+
import app from '../../src/app';
3+
import request from 'supertest';
54

6-
const serverConfig = config['server'];
5+
const { context } = config['server'];
76

87
describe('Endpoint Character', () => {
98
it('Should be return 200 and all characters', async () => {
10-
const result = await request(app).get(`${serverConfig['context']}/character`).send();
9+
const result = await request(app).get(`${context}/character`).send();
1110
expect(result.status).toBe(200);
1211
expect(result.body).not.toBeNull();
1312
});
1413

1514
it('Should dont be return status 200', async () => {
16-
const result = await request(app).get(`${serverConfig['context']}/character?name=asdsa`).send();
15+
const result = await request(app).get(`${context}/character?name=asdsa`).send();
1716
expect(result.status).not.toBe(200);
1817
});
1918
});

test/unit/converters.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
formatDate,
3+
formatDateUTC,
4+
formatTimestampToDate,
5+
formatDateToTimestamp,
6+
} from '../../src/utils/converters';
7+
8+
describe('UTILS - CONVERTERS', () => {
9+
it('Should be convert date to YYYY-MM-DD HH:mm:ss', async (done) => {
10+
const currentDate = formatDate('2021-02-01T12:34:05.077Z');
11+
expect(currentDate).toEqual('2021-02-01 09:34:05');
12+
done();
13+
});
14+
15+
it('Should be convert date to UTC', async (done) => {
16+
const currentDate = formatDateUTC('2021-02-01T12:34:05.077Z');
17+
expect(currentDate).toEqual('2021-02-01T09:34:05.077-03:00');
18+
done();
19+
});
20+
21+
it('Should be convert timestamp to date', async (done) => {
22+
const currentDate = formatTimestampToDate(1612182845);
23+
expect(currentDate).toEqual('2021-02-01 09:34:05');
24+
done();
25+
});
26+
27+
it('Should be convert date to timestamp', async (done) => {
28+
const currentDate = formatDateToTimestamp('2021-02-01T12:34:05');
29+
expect(currentDate).not.toBeNull();
30+
done();
31+
});
32+
});

test/unit/health.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import request from 'supertest';
2-
import app from '../../src/app';
3-
41
const config = require('config');
2+
import app from '../../src/app';
3+
import request from 'supertest';
54

6-
const serverConfig = config['server'];
7-
8-
describe('Endpoint Health', () => {
9-
it('Should be return status 200', async () => {
10-
const result = await request(app).get(`${serverConfig['context']}/health`).send();
11-
expect(result.status).toBe(200);
12-
});
5+
const { context } = config['server'];
136

14-
it('Should be return status up', async () => {
15-
const result = await request(app).get(`${serverConfig['context']}/health`).send();
16-
expect(result.body.status).toEqual('UP');
7+
describe('HEALTH', () => {
8+
it('Should be return status message UP and status code 200 in healthcheck', async (done) => {
9+
const { status, body } = await request(app)
10+
.get(`${context}/health`)
11+
.set('Content-Type', 'application/json')
12+
.set('Accept', 'application/json')
13+
.send();
14+
expect(status).toBe(200);
15+
expect(body.status).toEqual('UP');
16+
done();
1717
});
1818
});

test/unit/home.spec.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import request from 'supertest';
21
import app from '../../src/app';
2+
import request from 'supertest';
33

44
const pjson = require('../../package.json');
55

6-
describe('Endpoint root', () => {
7-
it('Should be return status 200', async () => {
8-
const result = await request(app).get(`/`).send();
9-
expect(result.status).toBe(200);
10-
});
11-
12-
it('Should be return message and version', async () => {
13-
const result = await request(app).get(`/`).send();
14-
expect(result.text).toEqual(`Welcome to server express - v${pjson['version']}`);
6+
describe('APP SERVER', () => {
7+
it('Should be return status code 200 and welcome message in root', async (done) => {
8+
const { status, text } = await request(app).get(`/`).send();
9+
expect(status).toBe(200);
10+
expect(text).toEqual(`Welcome to server express - v${pjson['version']}`);
11+
done();
1512
});
1613
});

test/utils/general-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
https://www.genbeta.com/desarrollo/da-potencia-flexibilidad-tus-tests-jest
3+
beforeEach( () => console.log('antes de cada prueba') );
4+
beforeAll( () => console.log('antes de todas las pruebas') );
5+
afterEach( () => console.log('Despues de cada prueba') );
6+
afterAll( () => console.log('Despues de todas las pruebas') );
7+
*/
8+
9+
const expectStatusCodeAndDataNotBeNull = (
10+
statusCode: number,
11+
data: any,
12+
expectedCode: number = 200,
13+
) => {
14+
expect(statusCode).toBe(expectedCode);
15+
expect(data).not.toBeNull();
16+
};
17+
18+
module.exports = {
19+
expectStatusCodeAndDataNotBeNull,
20+
};

0 commit comments

Comments
 (0)