Skip to content

Commit c9fea8d

Browse files
authored
Merge pull request #156 from flow-build/fix/tests
Fix unit tests execution
2 parents 6cae78e + c0bb138 commit c9fea8d

17 files changed

+124
-143
lines changed

.github/workflows/pr-validation.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ jobs:
4040
run: npm run migrations:local
4141
- name: run seeds
4242
run: npm run seeds:local
43+
- name: start tests
44+
run: npm run tests
4345
- name: Upload coverage to Codecov
4446
uses: codecov/codecov-action@v3

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"tests": "jest --coverage --runInBand --forceExit",
7+
"tests": "ENGINE_LOG_LEVEL=error KOA_LOG_LEVEL=error jest --coverage --runInBand --forceExit",
88
"test:single": "jest -i --detectOpenHandles --forceExit",
99
"seeds": "knex --env ${KNEX_ENV} --knexfile knexfile.js seed:run",
1010
"seeds:local": "knex --env dockerLocal --knexfile knexfile.js seed:run",

src/tests/activityManager.test.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const { startServer } = require("../app");
66
const workflowSamples = require("../samples/workflows");
77
const { delay, cleanDb } = require("./utils/auxiliar");
88
const { config } = require("./utils/requestConfig");
9-
const { setEngine, setCockpit } = require("../engine");
10-
const { Engine, Cockpit } = require("@flowbuild/engine");
11-
const engine = new Engine("knex", db);
12-
const cockpit = new Cockpit("knex", db);
13-
setEngine(engine);
14-
setCockpit(cockpit);
9+
const { World } = require("./utils/world");
10+
11+
const { tearDownEnvironment, createTestEngine, createTestCockpit } = require("./utils/fixtures");
12+
13+
const logger = (...args) => process.env.TESTS_VERBOSE ? logger(...args) : undefined
14+
1515
const world = new World({
1616
baseUrl: config.baseURL,
1717
headers: config.headers
@@ -24,6 +24,9 @@ let activityManagerId;
2424

2525
beforeAll(async () => {
2626
process.env.ENGINE_HEARTBEAT=false
27+
createTestEngine(db);
28+
createTestCockpit(db);
29+
2730
server = startServer(3001);
2831
axios.defaults.baseURL = config.baseURL;
2932
axios.defaults.headers = config.headers;
@@ -41,19 +44,15 @@ beforeEach(async () => {
4144
await world.waitProcessStop(processId);
4245
//OBTER O ID DO ACTIVITY_MANAGER
4346
const activityManager = await axios.get(`/processes/${processId}/activity`);
44-
console.log(`AMID ${activityManager.data.id}`);
47+
logger(`AMID ${activityManager.data.id}`);
4548
activityManagerId = activityManager.data.id;
4649
});
4750

48-
afterAll(async () => {
49-
cleanDb();
50-
db.destroy();
51-
server.close();
52-
});
51+
afterAll(async () => tearDownEnvironment(server, db));
5352

5453
describe("POST /:id/commit", () => {
5554
test("should return 200 for existing id and should not affect the process status", async () => {
56-
console.log("should return 200 for existing id and should not affect the process status");
55+
logger("should return 200 for existing id and should not affect the process status");
5756
const commitCall = await axios.post(`${prefix}/${activityManagerId}/commit`);
5857
expect(commitCall.status).toBe(200);
5958

@@ -62,7 +61,7 @@ describe("POST /:id/commit", () => {
6261
});
6362

6463
test("should keep returning 200 for an existing id previous committed", async () => {
65-
console.log("should keep returning 200 for an existing id previous committed");
64+
logger("should keep returning 200 for an existing id previous committed");
6665
const firstCall = await axios.post(`${prefix}/${activityManagerId}/commit`);
6766
expect(firstCall.status).toBe(200);
6867

@@ -71,15 +70,15 @@ describe("POST /:id/commit", () => {
7170
});
7271

7372
test("should return 404 for an random non-existing id", async () => {
74-
console.log("should return 404 for an random non-existing id");
73+
logger("should return 404 for an random non-existing id");
7574
const randomId = uuid();
7675
const commitCall = await axios.post(`${prefix}/${randomId}/commit`);
7776
expect(commitCall.status).toBe(404);
7877
});
7978
});
8079
describe("POST /:id/submit", () => {
8180
test("should return 202 for existing id and should affect the process status", async () => {
82-
console.log("should return 202 for existing id and should affect the process status");
81+
logger("should return 202 for existing id and should affect the process status");
8382
const submitCall = await axios.post(`${prefix}/${activityManagerId}/submit`);
8483
expect(submitCall.status).toBe(202);
8584
await delay(1500);
@@ -88,7 +87,7 @@ describe("POST /:id/submit", () => {
8887
});
8988

9089
test("should return 422 for an existing id already submitted", async () => {
91-
console.log("should return 422 for an existing id already submitted");
90+
logger("should return 422 for an existing id already submitted");
9291
const firstCall = await axios.post(`${prefix}/${activityManagerId}/submit`);
9392
expect(firstCall.status).toBe(202);
9493

@@ -97,14 +96,14 @@ describe("POST /:id/submit", () => {
9796
});
9897

9998
test("should return 422 for an id from a interrupted process", async () => {
100-
console.log("should return 422 for an id from a interrupted process");
99+
logger("should return 422 for an id from a interrupted process");
101100
await axios.post(`/processes/${processId}/abort`);
102101
const firstCall = await axios.post(`${prefix}/${activityManagerId}/submit`);
103102
expect(firstCall.status).toBe(422);
104103
});
105104

106105
test("should return 404 for an random non-existing id", async () => {
107-
console.log("should return 404 for an random non-existing id");
106+
logger("should return 404 for an random non-existing id");
108107
const randomId = uuid();
109108
const commitCall = await axios.post(`${prefix}/${randomId}/submit`);
110109
expect(commitCall.status).toBe(404);

src/tests/activityManagerValidation.test.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ const { db } = require("./utils/db");
44
const { startServer } = require("../app");
55
const { delay, cleanDb } = require("./utils/auxiliar");
66
const { config } = require("./utils/requestConfig");
7+
const { tearDownEnvironment, createTestEngine, createTestCockpit } = require("./utils/fixtures");
78

8-
//ENGINE DEPS
9-
const { setEngine, setCockpit } = require("../engine");
10-
const { Engine, Cockpit } = require("@flowbuild/engine");
11-
const engine = new Engine("knex", db);
12-
const cockpit = new Cockpit("knex", db);
13-
setEngine(engine);
14-
setCockpit(cockpit);
9+
const logger = (...args) => process.env.TESTS_VERBOSE ? logger(...args) : undefined
1510

1611
//SAMPLES
1712
const activitySchemaValidation = require("../samples/blueprints/activitySchemaValidation");
@@ -20,6 +15,9 @@ let server;
2015
let activityManagerId;
2116

2217
beforeAll(async () => {
18+
createTestEngine(db);
19+
createTestCockpit(db);
20+
2321
server = startServer(3001);
2422
axios.defaults.baseURL = config.baseURL;
2523
axios.defaults.headers = config.headers;
@@ -38,19 +36,15 @@ beforeEach(async () => {
3836
{}
3937
);
4038
const processId = process.data.process_id;
41-
await delay(1500);
42-
console.log(`PID ${processId}`);
39+
await delay(200)
40+
logger(`PID ${processId}`);
4341
//OBTER O ID DO ACTIVITY_MANAGER
4442
const activityManager = await axios.get(`/processes/${processId}/activity`);
45-
console.log(`AMID ${activityManager.data.id}`);
43+
logger(`AMID ${activityManager.data.id}`);
4644
activityManagerId = activityManager.data.id;
4745
});
4846

49-
afterAll(async () => {
50-
cleanDb();
51-
db.destroy();
52-
server.close();
53-
});
47+
afterAll(async () => tearDownEnvironment(server, db));
5448

5549
describe("Validation @ POST activity_manager/:id/submit", () => {
5650
test.each([
@@ -76,7 +70,7 @@ describe("Validation @ POST activity_manager/:id/submit", () => {
7670
const payload = { date: '2021-09-15' };
7771

7872
let response = await axios.post(`activity_manager/${activityManagerId}/submit`, payload);
79-
console.log(response)
73+
logger(response)
8074
expect(response.status).toBe(202);
8175
});
8276
});
@@ -105,7 +99,7 @@ describe("Validation @ POST activity_manager/:id/commit", () => {
10599
const payload = { date: '2021-09-15' };
106100

107101
let response = await axios.post(`activity_manager/${activityManagerId}/commit`, payload);
108-
console.log(response)
102+
logger(response)
109103
expect(response.status).toBe(200);
110104
});
111105
});

src/tests/cockpitBlueprintValidation.test.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@ const workflowSamples = require("../samples/workflows");
77
const { cleanDb } = require("./utils/auxiliar");
88
const { config } = require("./utils/requestConfig");
99

10-
const { setEngine, setCockpit } = require("../engine");
11-
const { Engine, Cockpit } = require("@flowbuild/engine");
10+
const { tearDownEnvironment, createTestEngine, createTestCockpit } = require("./utils/fixtures");
1211
const { setDbConnection } = require("../services/cockpit");
1312

14-
const engine = new Engine("knex", db);
15-
const cockpit = new Cockpit("knex", db);
16-
setEngine(engine);
17-
setCockpit(cockpit);
18-
1913
let server;
2014

2115
const prefix = "/cockpit";
2216

2317
beforeAll(async () => {
18+
createTestEngine(db);
19+
createTestCockpit(db);
20+
2421
server = startServer(3001);
2522
axios.defaults.baseURL = `${config.baseURL}`;
2623
axios.defaults.headers = config.headers;
@@ -33,11 +30,7 @@ beforeAll(async () => {
3330
await axios.post("/workflows", workflowSamples.singleUserTask);
3431
});
3532

36-
afterAll(async () => {
37-
await cleanDb();
38-
await db.destroy();
39-
await server.close();
40-
});
33+
afterAll(async () => tearDownEnvironment(server, db));
4134

4235
describe("POST /workflows/validate", () => {
4336
const route = `${prefix}/workflows/validate/`;

src/tests/cockpitProcesses.test.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const { v1: uuid } = require("uuid");
33
const axios = require("axios");
44
const { db } = require("./utils/db");
55
const { startServer } = require("../app");
6-
const { setEngine, setCockpit } = require("../engine");
7-
const { Engine, Cockpit } = require("@flowbuild/engine");
6+
const { tearDownEnvironment, createTestEngine, createTestCockpit } = require("./utils/fixtures");
87

98
const { setDbConnection } = require("../services/cockpit");
109
const { cleanDb, delay } = require("./utils/auxiliar");
@@ -20,10 +19,7 @@ const { validateDataWithSchema } = require("../validators/base");
2019
const processExecution = require("../validators/schemas/processExecution");
2120
const { World } = require("./utils/world");
2221

23-
const engine = new Engine("knex", db);
24-
const cockpit = new Cockpit("knex", db);
25-
setEngine(engine);
26-
setCockpit(cockpit);
22+
const logger = (...args) => process.env.TESTS_VERBOSE ? logger(...args) : undefined
2723

2824
let server;
2925
let basicProcessId;
@@ -37,6 +33,9 @@ const world = new World({
3733
const prefix = "/cockpit";
3834

3935
beforeAll(async () => {
36+
createTestEngine(db);
37+
createTestCockpit(db);
38+
4039
server = startServer(3001);
4140
axios.defaults.baseURL = `${config.baseURL}`;
4241
axios.defaults.headers = config.headers;
@@ -52,20 +51,16 @@ beforeAll(async () => {
5251

5352
response = await axios.post(`/workflows/name/${basic.name}/start`, {});
5453
basicProcessId = response.data.process_id;
55-
console.log('basicProcessId', basicProcessId)
54+
logger('basicProcessId', basicProcessId)
5655
response = await axios.post(`/workflows/name/${singleUserTask.name}/start`, {});
5756
singleUserProcessId = response.data.process_id;
58-
console.log('singleUserProcessId', singleUserProcessId)
57+
logger('singleUserProcessId', singleUserProcessId)
5958
response = await axios.post(`/workflows/name/${notifyUserTask.name}/start`, {});
6059
notifyProcessId = response.data.process_id;
61-
console.log('notifyProcessId', notifyProcessId)
60+
logger('notifyProcessId', notifyProcessId)
6261
});
6362

64-
afterAll(async () => {
65-
await cleanDb();
66-
await db.destroy();
67-
await server.close();
68-
});
63+
afterAll(async () => tearDownEnvironment(server, db));
6964

7065
describe("GET /processes/:id/execution", () => {
7166
test("Should return 200", async () => {

src/tests/cockpitWorkflows.test.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ const { startServer } = require("../app");
77
const { cleanDb } = require("./utils/auxiliar");
88
const { config } = require("./utils/requestConfig");
99

10-
const { setEngine, setCockpit } = require("../engine");
11-
const { Engine, Cockpit } = require("@flowbuild/engine");
1210
const { validateDataWithSchema } = require("../validators/base");
1311
const { setDbConnection } = require("../services/cockpit");
12+
const { tearDownEnvironment, createTestEngine, createTestCockpit } = require("./utils/fixtures");
1413

1514
//Samples
1615
const workflowSamples = require("../samples/workflows");
@@ -21,11 +20,6 @@ const processesStats = require("../validators/schemas/processesStats");
2120
const cockpitListProcesses = require("../validators/schemas/cockpitListProcesses");
2221
const processStateFromNode = require("../validators/schemas/processStateFromNode");
2322

24-
const engine = new Engine("knex", db);
25-
const cockpit = new Cockpit("knex", db);
26-
setEngine(engine);
27-
setCockpit(cockpit);
28-
2923
let server;
3024
const numProcesses = 2;
3125

@@ -34,6 +28,9 @@ let singleUserTaskWorkflowId;
3428
const prefix = "/cockpit";
3529

3630
beforeAll(async () => {
31+
createTestEngine(db);
32+
createTestCockpit(db);
33+
3734
server = startServer(3001);
3835
axios.defaults.baseURL = `${config.baseURL}`;
3936
axios.defaults.headers = config.headers;
@@ -57,11 +54,7 @@ beforeAll(async () => {
5754
}
5855
});
5956

60-
afterAll(async () => {
61-
cleanDb();
62-
db.destroy();
63-
server.close();
64-
});
57+
afterAll(async () => tearDownEnvironment(server, db));
6558

6659
describe("GET /workflows/stats", () => {
6760
const route = `${prefix}/workflows/stats`;
@@ -202,7 +195,7 @@ describe("GET /workflows/name/:name/states/:node_id", () => {
202195
nodeId = "1";
203196
response = await axios.get(`${route}/${workflowSamples.singleUserTask.name}/states/${nodeId}`, {});
204197
expect(response.status).toEqual(200);
205-
expect(response.data).toHaveLength(9);
198+
expect(response.data).toHaveLength(10);
206199
});
207200

208201
test("Should return an empty list for a non-existant node_id", async () => {

0 commit comments

Comments
 (0)