Skip to content

Commit ebbd2ef

Browse files
committed
use supertest
1 parent e7e6499 commit ebbd2ef

6 files changed

Lines changed: 133 additions & 14 deletions

File tree

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ compose-bash:
1212
compose-lint:
1313
docker-compose run app make lint
1414

15-
# FIXME: Починить тесты при запуске в контейнере
1615
compose-test:
1716
docker-compose run app make test
1817

@@ -35,4 +34,4 @@ lint:
3534
npx eslint .
3635

3736
test:
38-
npm test -s
37+
npm test

__tests__/index.test.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1-
const appUrl = 'http://localhost:5000';
1+
const supertest = require('supertest');
2+
const _ = require('lodash');
3+
4+
const getApp = require('../server/index.js');
25

36
describe('app', () => {
4-
it('main page opens', async () => {
5-
await page.goto(appUrl);
6-
await page.waitForSelector('p[class="lead"]');
7-
await expect(page).toMatch('Привет от Хекслета!');
8-
await expect(page).toMatch('Приложение запущено на сервере: 10.10.10.10');
7+
let app;
8+
9+
beforeAll(async () => {
10+
app = getApp();
11+
await app.ready();
12+
});
13+
14+
beforeEach(() => {
15+
_.unset(process.env, 'SERVER_IP');
16+
});
17+
18+
it('main page without environment variable SERVER_IP', async () => {
19+
const res = await supertest(app.server)
20+
.get('/')
21+
.expect(200);
22+
23+
expect(res.text).toMatch('Привет от Хекслета!');
24+
expect(res.text).toMatch('Приложение запущено на сервере с IP: не определен');
25+
});
26+
27+
it('main page with environment variable SERVER_IP', async () => {
28+
process.env.SERVER_IP = '10.11.12.13';
29+
30+
const res = await supertest(app.server)
31+
.get('/')
32+
.expect(200);
33+
34+
expect(res.text).toMatch('Привет от Хекслета!');
35+
expect(res.text).toMatch(`Приложение запущено на сервере с IP: ${process.env.SERVER_IP}`);
36+
});
37+
38+
afterAll(() => {
39+
app.close();
940
});
1041
});

package-lock.json

Lines changed: 87 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"fastify": "^3.15.1",
1515
"fastify-reverse-routes": "^3.0.0",
1616
"fastify-static": "^4.0.1",
17+
"lodash": "^4.17.21",
1718
"point-of-view": "^4.14.0",
1819
"pug": "^3.0.2"
1920
},
@@ -22,6 +23,7 @@
2223
"eslint-config-airbnb-base": "^14.2.1",
2324
"eslint-plugin-import": "^2.22.1",
2425
"eslint-plugin-jest": "^24.3.6",
25-
"jest": "^26.6.3"
26+
"jest": "^26.6.3",
27+
"supertest": "^6.1.3"
2628
}
2729
}

server/routes.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
/* eslint no-param-reassign: ["error", { "props": false }] */
1+
// @ts-check
2+
3+
const _ = require('lodash');
24

35
module.exports = (app) => {
46
app.get('/', { name: 'root' }, (req, reply) => {
5-
const serverIp = process.env.SERVER_IP || 'IP не определен';
7+
const serverIp = _.get(process.env, 'SERVER_IP', 'не определен');
68
reply.view('index', { serverIp });
79
});
810
};

server/views/index.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ block content
66
.card-body.p-5.bg-light
77
.display-4 Привет от Хекслета!
88
hr
9-
p.lead Приложение запущено на сервере: !{serverIp}
9+
p.lead Приложение запущено на сервере с IP: !{serverIp}

0 commit comments

Comments
 (0)