Node.js based forum software built for the modern web
feat: use separate bootstrap file to build schema data before api schema tests
Julian Lam8e818fd9f565
diff --git a/test/api.js b/test/api.jsindex 6c456ed520..960ba08c8c 100644--- a/test/api.js+++ b/test/api.js@@ -29,6 +29,10 @@ const notifications = require('../src/notifications');const utils = require('../src/utils');const api = require('../src/api');+// Bootstrap schema test data+const { execSync } = require('child_process');+execSync('node ./test/api/schema-bootstrap.mjs');+describe('API', async () => {before(async function () {this.readApiPath = path.resolve(__dirname, '../public/openapi/read.yaml');@@ -55,7 +59,9 @@ describe('API', async () => {it('subfolder tests', () => {files.forEach((filePath) => {- require(filePath);+ if (filePath.endsWith('.js')) {+ require(filePath);+ }});});});diff --git a/test/api/schema-bootstrap.mjs b/test/api/schema-bootstrap.mjsnew file mode 100644index 0000000000..f7819c059f--- /dev/null+++ b/test/api/schema-bootstrap.mjs@@ -0,0 +1,19 @@+'use strict';++import { fileURLToPath } from 'url';+import { resolve, dirname } from 'path';+import { writeFile } from 'fs/promises';+import SwaggerParser from '@apidevtools/swagger-parser';++const __filename = fileURLToPath(import.meta.url);+const __dirname = dirname(__filename);++const readApiPath = resolve(__dirname, '../../public/openapi/read.yaml');+const writeApiPath = resolve(__dirname, '../../public/openapi/write.yaml');+const readApi = await SwaggerParser.dereference(readApiPath);+const writeApi = await SwaggerParser.dereference(writeApiPath);++await writeFile(resolve(__dirname, '../files/readApi.json'), JSON.stringify(readApi, null));+await writeFile(resolve(__dirname, '../files/writeApi.json'), JSON.stringify(writeApi, null));++process.stdout.write('[test/api/schema] Schema bootstrap complete.\n');\ No newline at end of filediff --git a/test/api/schema.js b/test/api/schema.jsindex 857303db22..ab932ea9e4 100644--- a/test/api/schema.js+++ b/test/api/schema.js@@ -31,12 +31,21 @@ const helpers = require('../helpers');const ajv = new Ajv({ allErrors: true, strict: false });const { baseDir } = require('../../src/constants').paths;const wait = util.promisify(setTimeout);-let readApi;-let writeApi;let jar;let csrfToken;const unauthenticatedRoutes = ['/api/login', '/api/register']; // Everything else will be called with the admin user+let readApi;+let writeApi;+const readApiPath = path.resolve(__dirname, '../files/readApi.json');+const writeApiPath = path.resolve(__dirname, '../files/writeApi.json');+try {+ readApi = JSON.parse(fs.readFileSync(readApiPath, 'utf8'));+ writeApi = JSON.parse(fs.readFileSync(writeApiPath, 'utf8'));+} catch (e) {+ throw new Error('Run test/api/schema-bootstrap.mjs first.');+}+async function dummySearchHook(data) {return [1];}@@ -368,11 +377,6 @@ async function setupData() {describe('schema', () => {before(async function () {- const readApiPath = path.resolve(__dirname, '../../public/openapi/read.yaml');- const writeApiPath = path.resolve(__dirname, '../../public/openapi/write.yaml');- this.readApi = await SwaggerParser.dereference(readApiPath);- this.writeApi = await SwaggerParser.dereference(writeApiPath);-await db.setupMockDefaults();await setupData();});@@ -380,16 +384,19 @@ describe('schema', () => {after(async () => {plugins.hooks.unregister('core', 'filter:search.query', dummySearchHook);plugins.hooks.unregister('emailer-test', 'static:email.send');++ await fs.promises.unlink(readApiPath);+ await fs.promises.unlink(writeApiPath);});it('read api', async function () {// Iterate through all documented paths, make a call to it,// and compare the result body with what is defined in the specthis.timeout(0);- const paths = Object.keys(this.readApi.paths);+ const paths = Object.keys(readApi.paths);const pathLib = path; // for calling path module from inside this forEachfor(const p of paths) {- await executeTests(p, this.readApi, paths, pathLib);+ await executeTests(p, readApi, paths, pathLib);}});@@ -397,10 +404,10 @@ describe('schema', () => {// Iterate through all documented paths, make a call to it,// and compare the result body with what is defined in the specthis.timeout(0);- const paths = Object.keys(this.writeApi.paths);+ const paths = Object.keys(writeApi.paths);const pathLib = path; // for calling path module from inside this forEachfor(const p of paths) {- await executeTests(p, this.writeApi, paths, pathLib, this.writeApi.servers[0].url);+ await executeTests(p, writeApi, paths, pathLib, writeApi.servers[0].url);};});