Skip to content

Commit

Permalink
add and fix and refactor _globals-test.js file in the api test/api fo…
Browse files Browse the repository at this point in the history
…lder. Use global beforeEach and after hook to generate test overview table in api-test-overview.md file
  • Loading branch information
NickOvt committed Sep 14, 2023
1 parent 0459279 commit 496957a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions test/_globals-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ 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 () {
// console.log('GLOBAL ############################')
// console.log(this.test.ctx.currentTest.title); // eslint-disable-line no-invalid-this
// console.log(await server.get("/api-methods/args"));

titles.push(this.test.ctx.currentTest.title); // eslint-disable-line no-invalid-this
const currentTestTitle = this.test.ctx.currentTest.title; // eslint-disable-line no-invalid-this
if (/POST|PUT|DELETE|GET|post|put|delete|get/.test(currentTestTitle) && /success|failure/.test(currentTestTitle)) {
titles.push(currentTestTitle);
} else {
unsupportedTitles.push(currentTestTitle);
}
});

// eslint-disable-next-line no-undef
Expand All @@ -26,8 +28,8 @@ after("Generate test overview table after all tests", async () => {
const routes = data.body;

const mapApiMethodToSpec = {};
let content = "| API path | API method | Test count | Has positive test? | Has Negative test? |\n";
content += "| ------------------------------ | :--------: | ---------- | ------------------ | ------------------ | \n"
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];
Expand All @@ -49,7 +51,6 @@ after("Generate test overview table after all tests", async () => {
}

const data = mapApiMethodToSpec[`${method.toLowerCase()}_${path.replace(/{/g, ":").replace(/}/g, "")}`];
// console.log(data, `${method.toLowerCase()}_${path.replace("{", ":").replace("}", "")}`);

// wront path or wrong data etc. (no such route, can't construct route from test title)
if (!data) {
Expand Down Expand Up @@ -87,11 +88,13 @@ after("Generate test overview table after all tests", async () => {
content += `| \`${data.path}\` | \`${data.method}\` | ${data.testCount} | ${data.positiveTestCount > 0 ? "✅" : "❌"} (${data.positiveTestCount}) | ${data.negativeTestCount > 0 ? "✅" : "❌"} (${data.negativeTestCount}) |\n`;
}

fs.writeFile("./test.md", content, (err) => {
fs.writeFile("./api-tests-overview.md", content, (err) => {
if (err) {
console.log(err);
}
})

console.log("These titles were not included in the overview as they are wrong format:", unsupportedTitles);
} catch (error) {
console.log(error);
}
Expand Down

0 comments on commit 496957a

Please sign in to comment.