wildduck/test/_globals-test.js
NickOvt c60373bbfe
ZMS-85 (#495)
* added new file to test git repo setup

* remove test.txt
Add api-tests-table.md file to keep track of api endpoints, method and their test count + test types (and their count)

* add js script to parse files in the /lib/api folder and retrieve a map of apiPath -> apiMethod

* delete parseApiFiles.js as it is unneeded

* add global (per test run) beforeEach and after hooks to collect test data and create an overview table

* update tests to conform to new test title structure

* add expectations to tests

* remove old api-tests-table.md that was handmade

* add expectations to tests in the api-test.js file

* add GET endpoint /api-methods/:arg to fetch all api endpoints for testing purposes only

* add and fix and refactor _globals-test.js file in the api test/api folder. Use global beforeEach and after hook to generate test overview table in api-test-overview.md file

* add missing newline at the end of file

* fix with prettier

* api.js fix style, remove unnecessary comments, remove :arg

* make first post,put,delete,get regex case insensitive, fix call to /api-methods

* _globals-test.js fixes

* fix some test titles

---------

Co-authored-by: Nikolai Ovtsinnikov <nikolai@zone.ee>
2023-09-14 12:49:55 +03:00

105 lines
3.3 KiB
JavaScript

'use strict';
const supertest = require('supertest');
const fs = require('fs');
const config = require('wild-config');
const server = supertest.agent(`http://127.0.0.1:${config.api.port}`);
const titles = [];
const unsupportedTitles = [];
// global beforeEach to run before EVERY test
beforeEach('Get test data before each test', async function () {
const currentTestTitle = this.test.ctx.currentTest.title; // eslint-disable-line no-invalid-this
if (/\b(POST|PUT|DELETE|GET)\b/i.test(currentTestTitle) && /success|failure/.test(currentTestTitle)) {
titles.push(currentTestTitle);
} else {
unsupportedTitles.push(currentTestTitle);
}
});
// eslint-disable-next-line no-undef
after('Generate test overview table after all tests', async () => {
const data = await server.get('/api-methods');
const routes = data.body;
const mapApiMethodToSpec = {};
let content = '| API path | API method | Test count | Has positive test? | Has Negative test? |\n';
content += '| --- | :---: | --- | --- | --- | \n';
for (const routeName in routes) {
const route = routes[routeName];
const method = route.spec.method;
const path = route.spec.path;
mapApiMethodToSpec[`${method.toLowerCase()}_${path}`] = {
method,
path,
name: route.spec.name || route.name,
testCount: 0,
positiveTestCount: 0,
negativeTestCount: 0
};
}
for (const title of titles) {
const titleSplit = title.split(/\s+/);
const method = titleSplit[1].toLowerCase();
const path = titleSplit[2];
const expectedResult = titleSplit[4];
// missing method or path (string is too short to be accepted as valid test title)
if (!method || !path) {
continue;
}
const data = mapApiMethodToSpec[`${method}_${path.replace(/{/g, ':').replace(/}/g, '')}`];
// wrong path or wrong data etc. (no such route, can't construct route from test title)
if (!data) {
unsupportedTitles.push(title);
continue;
}
data.testCount++;
if (expectedResult) {
if (expectedResult === 'success') {
data.positiveTestCount++;
} else if (expectedResult === 'failure') {
data.negativeTestCount++;
}
}
}
const sortedData = Object.values(mapApiMethodToSpec).sort((a, b) => {
// 1) sort by test count
// 2) sort by path
if (a.testCount < b.testCount) {
return 1;
} else if (a.testCount > b.testCount) {
return -1;
} else if (a.path > b.path) {
return 1;
} else if (a.path < b.path) {
return -1;
} else {
return 0;
}
});
for (const data of sortedData) {
content += `| \`${data.path}\` | \`${data.method}\` | ${data.testCount} | ${data.positiveTestCount > 0 ? '✅' : '❌'} (${data.positiveTestCount}) | ${
data.negativeTestCount > 0 ? '✅' : '❌'
} (${data.negativeTestCount}) |\n`;
}
console.log(__dirname);
await fs.promises.writeFile(__dirname + '/../api-tests-overview.md', content);
console.log('These titles were not included in the overview as they are wrong format:', unsupportedTitles);
});